fortran习题3答案
- 格式:doc
- 大小:845.53 KB
- 文档页数:10
module rational_utilityimplicit noneprivatepublic :: rational, &operator(+), operator(-), operator(*),&operator(/), assignment(=),operator(>),&operator(<), operator(==), operator(/=),&output, inputtype :: rationalinteger :: num, denomend type rationalinterface operator(+)module procedure rat__rat_plus_ratend interfaceinterface operator(-)module procedure rat__rat_minus_ratend interfaceinterface operator(*)module procedure rat__rat_times_ratend interfaceinterface operator(/)module procedure rat__rat_div_ratend interfaceinterface assignment(=)module procedure rat_eq_ratmodule procedure int_eq_ratmodule procedure real_eq_ratend interfaceinterface operator(>)module procedure rat_gt_ratend interfaceinterface operator(<)module procedure rat_lt_ratend interfaceinterface operator(==)module procedure rat_compare_rat end interfaceinterface operator(/=)module procedure rat_ne_ratend interfacecontainsfunction rat_gt_rat(a,b)implicit nonelogical :: rat_gt_rattype(rational), intent(in) :: a,breal :: fa,fbfa=real(a%num)/real(a%denom)fb=real(b%num)/real(b%denom)if ( fa > fb ) thenrat_gt_rat=.true.elserat_gt_rat=.false.end ifreturnend function rat_gt_ratfunction rat_lt_rat(a,b)implicit nonelogical :: rat_lt_rattype(rational), intent(in) :: a,breal :: fa,fbfa=real(a%num)/real(a%denom)fb=real(b%num)/real(b%denom)if ( fb > fa ) thenrat_lt_rat=.true.elserat_lt_rat=.false.end ifreturnend function rat_lt_ratfunction rat_compare_rat(a,b)implicit nonelogical :: rat_compare_rat type(rational), intent(in) :: a,b type(rational) :: cc=a-bif ( c%num == 0 ) thenrat_compare_rat=.true.elserat_compare_rat=.false. end ifreturnend function rat_compare_ratfunction rat_ne_rat(a,b)implicit nonelogical :: rat_ne_rattype(rational), intent(in) :: a,b type(rational) :: cc=a-bif ( c%num==0 ) thenrat_ne_rat=.false.elserat_ne_rat=.true.end ifreturnend function rat_ne_ratsubroutine rat_eq_rat( rat1, rat2 ) implicit nonetype(rational), intent(out):: rat1 type(rational), intent(in) :: rat2rat1%num = rat2%numrat1%denom = rat2%denomreturnend subroutine rat_eq_ratsubroutine int_eq_rat( int, rat ) implicit noneinteger, intent(out):: inttype(rational), intent(in) :: ratint = rat%num / rat%denomreturnend subroutine int_eq_ratsubroutine real_eq_rat( float, rat )implicit nonereal, intent(out) :: floattype(rational), intent(in) :: ratfloat = real(rat%num) / real(rat%denom)returnend subroutine real_eq_ratfunction reduse( a )implicit nonetype(rational), intent(in) :: ainteger :: btype(rational) :: reduseb=gcv_interface(a%num,a%denom) reduse%num = a%num/breduse%denom = a%denom/breturnend function redusefunction gcv_interface(a,b)implicit noneinteger, intent(in) :: a,binteger :: gcv_interfaceif ( min(a,b) .eq. 0 ) thengcv_interface=1returnend ifif (a==b) thengcv_interface=areturnelse if ( a>b ) thengcv_interface=gcv(a,b)else if ( a<b ) thengcv_interface=gcv(b,a)end ifreturnend function gcv_interfacerecursive function gcv(a,b) result(ans)implicit noneinteger, intent(in) :: a,binteger :: minteger :: ansm=mod(a,b)select case(m)case(0)ans=breturncase(1)ans=1returncase defaultans=gcv(b,m)end selectreturnend function gcvfunction rat__rat_plus_rat( rat1, rat2 )implicit nonetype(rational) :: rat__rat_plus_rattype(rational), intent(in) :: rat1,rat2type(rational) :: actact%denom= rat1%denom * rat2%denomact%num = rat1%num*rat2%denom + rat2%num*rat1%denom rat__rat_plus_rat = reduse(act)returnend function rat__rat_plus_ratfunction rat__rat_minus_rat( rat1, rat2 )implicit nonetype(rational) :: rat__rat_minus_rattype(rational), intent(in) :: rat1, rat2type(rational) :: temptemp%denom = rat1%denom*rat2%denomtemp%num = rat1%num*rat2%denom - rat2%num*rat1%denom rat__rat_minus_rat = reduse( temp )returnend function rat__rat_minus_ratfunction rat__rat_times_rat( rat1, rat2 )implicit nonetype(rational) :: rat__rat_times_rattype(rational), intent(in) :: rat1, rat2type(rational) :: temptemp%denom = rat1%denom* rat2%denomtemp%num = rat1%num * rat2%numrat__rat_times_rat = reduse(temp)returnend function rat__rat_times_ratfunction rat__rat_div_rat( rat1, rat2 )implicit nonetype(rational) :: rat__rat_div_rattype(rational), intent(in) :: rat1, rat2type(rational) :: temptemp%denom = rat1%denom* rat2%numtemp%num = rat1%num * rat2%denomrat__rat_div_rat = reduse(temp)returnend function rat__rat_div_ratsubroutine input(a)implicit nonetype(rational), intent(out) :: awrite(*,*) "分子:"read(*,*) a%numwrite(*,*) "分母:"read(*,*) a%denomreturnend subroutine inputsubroutine output(a)implicit nonetype(rational), intent(in) :: aif ( a%denom/=1 ) thenwrite(*, "(' (',I3,'/',I3,')' )" ) a%num,a%denom elsewrite(*, "(I3)" ) a%numend ifreturnend subroutine outputend module rational_utilityprogram mainuse rational_utilityimplicit nonetype(rational) :: a,b,ccall input(a)call input(b)c=a+bwrite(*,*) "a+b="call output(c)c=a-bwrite(*,*) "a-b="call output(c)c=a*bwrite(*,*) "a*b="call output(c)c=a/bwrite(*,*) "a/b="call output(c)if (a>b) write(*,*) "a>b"if (a<b) write(*,*) "a<b"if (a==b) write(*,*) "a==b"if (a/=b) write(*,*) "a/=b"stopend program main。
fortran编程习题答案Fortran编程习题答案Fortran是一种古老而强大的编程语言,广泛应用于科学计算和工程领域。
在学习Fortran编程的过程中,解决习题是一种非常有效的方法。
本文将为您提供一些Fortran编程习题的答案,帮助您更好地理解和掌握这门语言。
1. 习题一:编写一个Fortran程序,计算并输出1到100之间所有整数的平方。
程序代码如下:```fortranprogram squareimplicit noneinteger :: ido i = 1, 100print *, i, i**2end doend program square```2. 习题二:编写一个Fortran程序,计算并输出斐波那契数列的前20个数。
程序代码如下:```fortranprogram fibonacciimplicit noneinteger :: i, n, fib(20)fib(1) = 0fib(2) = 1do i = 3, 20fib(i) = fib(i-1) + fib(i-2)end dodo i = 1, 20print *, fib(i)end doend program fibonacci```3. 习题三:编写一个Fortran程序,计算并输出一个给定整数的阶乘。
程序代码如下:```fortranprogram factorialimplicit noneinteger :: i, n, resultprint *, "请输入一个整数:"read *, nresult = 1do i = 1, nresult = result * iend doprint *, n, "的阶乘为:", resultend program factorial```4. 习题四:编写一个Fortran程序,计算并输出一个给定整数是否为素数。
程序代码如下:```fortranprogram primeimplicit noneinteger :: i, n, countprint *, "请输入一个整数:"read *, ncount = 0do i = 2, n-1if (mod(n, i) == 0) thencount = count + 1end ifend doif (count == 0) thenprint *, n, "是素数"elseprint *, n, "不是素数"end ifend program prime```5. 习题五:编写一个Fortran程序,计算并输出一个给定整数的所有因子。
FORTRAN90试题_3_计算机基础部分单项选择题(每题1分,共⼗分)(⼀)微型机的性能指标主要取决于 1 。
1.A) 内存 B) 中央处理器 C) 硬盘 D) 显⽰卡(⼆)与⼗进制数35等值的⼆进制数为 2 。
2.A) 100011 B) 110001 C) 11101 D) 101011 (三)硬盘⼯作时,应尤其注意防⽌ 3 。
3.A) 空⽓潮湿 B) 空⽓中的尘埃 C) 光线直射 D) 强烈震动(四)下列选项中, 4 不属于系统软件。
4.A) C 语⾔编译程序B)Windows C) Unix D)Office (五)计算机联⽹的主要⽬的是 5 。
5.A) 数度快 B) 资源共享 C) 精度⾼ D) 容量⼤(六)下列⽹络的IP 地址中, 6 正确的是。
6.A) 202.245.101.201 B) 204.256.9.101 C) 202.155.98.C D) 249.220.258.202(七)在多媒体计算机系统中,不能⽤于存储多媒体信息的是 7 。
7.A) 光缆 B)光盘 C) 硬盘 D) 磁带(⼋)计算机病毒不能通过 8 传播。
8.A) 硬盘 B) 优盘 C) D 打印机 D) ⽹络(九)在Windows 98中,将⼀个应⽤程序窗⼝最⼩化后,该应⽤程序 9 。
9.A) 任在后台运⾏ B) 暂时停⽌运⾏ C) 完全停⽌运⾏ D) 从任务栏退出(⼗)在Windows 环境下,若要将当前窗⼝存⼊剪贴板,则可以按 10 。
10.A) Ctrl + Print Screen B) Alt + Print Screen C) Shift + Print Screen D) Print ScreenFORTRAN90语⾔⼀、语⾔基础单项选择(每题2分,共20分)(⼀) 下⾯程序段中,错误的语句是 1 。
1. A) REAL,PARAMETER :: S = 1.73B) REAL :: X = 3 C) S = S + XD) PRINT *, S, X(⼆) ⼀个梯形的上底长为A,下底长为B,⾼为H,正确计算这个梯形⾯积的FORTRAN90表达式为 2 。
《高级语言程序设计(FORTRAN)》考试精题一、选择题(每小题2分,共40分)1. 下列标识符中不能作为FORTRAN的合法变量名的是_____D_____。
A) A_1 B) A1 C) A1A D) A+12. 在FORTRAN的变量类型说明语句中没有 BA) REAL语句B)CHAR语句C)INTEGER语句 D)LOGICAL语句3.FORTRAN90 源程序的扩展名是 C 。
A. TXTB. FC. F90D. FOR4.下列关于符号常量的说法正确的是___C____。
A.符号常量的定义应该放在所有语句之前B.符号常量不可以是字符型常量C.在一个程序单位中定义一个符号常量后,不能再改变它的值D.符号常量定义语句与赋值语句一样,是可执行语句5、在下列写法中,__D______不是FORTRAN常量。
A. .TRUE.B.1E-2C. (0.0,2.4)D.1,000,0006.表达式15/4/2.0的值是 B 。
A.整数2 B.实数1.5 C.实数2.25 D.实数1. 37. 下列不是FORTRAN赋值语句的是 _____D______。
A) X=.TRUE. B) X=X*X C) X=X**2**2 D) X=X++8. 梯形的上底为A,下底为B,高为H,计算其面积的FORTRAN表达式中错误的是____A____。
A)1/2*(AB)*H B)(A+B)*H/2 C)(A+B)/2*H D)0.5*(A+B)*H9. 要选拔身高T>1.7米且体重W<62.5公斤的人,FORTRAN的逻辑表达式是__C_____。
A)T.ce.1.7.AND.w.le.62.5 B)T.le.1.7.OR.w.ge.62.5C)T.gt.1.7. AND.w.lt.62.5D)T.gt.1.7.OR.w.lt.62.510.FORTRAN 90规定,变量类型声明的优先顺序是 C 。
A.隐含约定(I-N规则)、IMPLICIT声明、类型声明D.IMPLICIT声明、类型声明、隐含约定(I-N规则)B.类型声明、隐含约定(I-N规则)、IMPLICIT声明C.类型声明、IMPLICIT声明、隐含约定(I-N规则)11、假设CH是一个字符型变量,并且具有足够的长度,则对于CH(3:7),下面说法正确的是___D________。
fortran课本习题答案
Fortran课本习题答案
在学习Fortran编程语言的过程中,课本习题答案是非常重要的。
它们不仅可以帮助我们更好地理解和掌握知识,还可以帮助我们提高编程能力和解决问题的
能力。
首先,课本习题答案可以帮助我们检验自己的学习成果。
通过完成课本习题并
对比答案,我们可以了解自己在学习中的掌握程度,发现自己的不足之处,从
而有针对性地进行学习和提高。
其次,课本习题答案还可以帮助我们更好地理解知识点。
在完成习题的过程中,我们可能会遇到一些困难和疑惑,而课本习题答案可以为我们提供参考和解答,帮助我们更好地理解和掌握知识点。
此外,课本习题答案还可以帮助我们提高编程能力。
通过不断地完成习题并对
比答案,我们可以积累更多的编程经验,提高自己的编程能力,同时也可以学
习到一些常见的编程技巧和方法。
总之,课本习题答案对于我们学习Fortran编程语言是非常重要的。
它们不仅可以帮助我们检验学习成果,更可以帮助我们更好地理解知识点,提高编程能力,是我们学习的重要辅助工具。
希望大家能够充分利用课本习题答案,不断提高
自己的编程水平,取得更好的学习成绩。
北航数值分析报告大作业第三题(fortran)“数值分析“计算实习大作业第三题——SY1415215孔维鹏一、计算说明1、将x i=0.08i,y j=0.5+0.05j分别代入方程组(A.3)得到关于t,u,v,w的的方程组,调用离散牛顿迭代子函数求出与x i,y j对应的t i,u j。
2、调用分片二次代数插值子函数在点(t i,u j)处插值得到z(x i,y j)=f(x i,y j),得到数表(x i,y j,f(x i,y j))。
3、对于k=1,2,3,4?,分别调用最小二乘拟合子函数计算系数矩阵c rs 及误差σ,直到满足精度,即求得最小的k值及系数矩阵c rs。
4、将x i?=0.1i,y j?=0.5+0.2j分别代入方程组(A.3)得到关于t?,u?,v?,w?的的方程组,调用离散牛顿迭代子函数求出与x i?,y j?对应的t i?,u j?,调用分片二次代数插值子函数在点(t i?,u j?)处插值得到z?(x i?,y j?)=f(x i?,y j?);调用步骤3中求得的系数矩阵c rs求得p(x i?,y j?),打印数表(x i?,y j?,f(x i?,y j?),p(x i?,y j?))。
二、源程序(FORTRAN)PROGRAM SY1415215DIMENSIONX(11),Y(21),T(6),U(6),Z(6,6),UX(11,21),TY(11,21),FXY(11,21), C(6,6) DIMENSIONX1(8),Y1(5),FXY1(8,5),PXY1(8,5),UX1(8,5),TY1(8,5)REAL(8) X,Y,T,U,Z,FXY,UX,TY,C,E,X1,Y1,FXY1,PXY1,UX1,TY1OPEN (1,FILE='第三题计算结果.TXT')DO I=1,11X(I)=0.08*(I-1)ENDDODO I=1,21Y(I)=0.5+0.05*(I-1)ENDDO!*****求解非线性方程组,得到z=f(t,u)的函数*******DO I=1,11DO J=1,21CALL DISNEWTON_NONLINEAR(X(I),Y(J),UX(I,J),TY(I,J)) ENDDO ENDDO!*************分片二次插值得到z=f(x,y)***********DO I=1,11DO J=1,21CALL INTERPOLATION(UX(I,J),TY(I,J),FXY(I,J))ENDDO ENDDOWRITE (1,"('数表(x,y,f(x,y)):')")WRITE (1,"(3X,'X',7X,'Y',10X,'F(X,Y)')")DO I=1,11DO J=1,21WRITE(1,'(1X,F5.2,2X,F5.3,2X,E20.13)') X(I),Y(J),FXY(I,J) ENDDOWRITE (1,"('')")ENDDO!***********最小二乘拟合得到P(x,y)**************N=11M=21WRITE (1,'(" ","K和σ分别为:")')DO K=1,20CALL LSFITTING(X,Y,FXY,C,N,M,K,K,E) WRITE (1,'(I3,2X,E20.13)') K-1,EIF(ETA).OR.(A(L,K)==TA)) THENTA=A(L,K)TL=LDO J=K,NT(K,J)=A(K,J)A(K,J)=A(TL,J)A(TL,J)=T(K,J)ENDDOTB(K)=B(K)B(K)=B(TL)B(TL)=TB(K)ENDIF ENDDODO I=K+1,NM(I,K)=A(I,K)/A(K,K)A(I,K)=0DO J=K+1,NA(I,J)=A(I,J)-M(I,K)*A(K,J) ENDDOB(I)=B(I)-M(I,K)*B(K)ENDDOENDDO!回代过程X(N)=B(N)/A(N,N)DO K=N-1,1,-1S=0.0DO J=K+1,NS=S+A(K,J)*X(J)ENDDOX(K)=(B(K)-S)/A(K,K)ENDDORETURNEND!***********求向量的无穷数************ SUBROUTINE NORM(X,N,A) DIMENSION X(N)REAL(8) X,AA=ABS(X(1))DO I=2,NIF(ABS(X(I))>ABS(X(I-1))) THENA=ABS(X(I)) ENDIFENDDORETURNEND!**************分片二次代数插值************** SUBROUTINE INTERPOLATION(U,V,W) PARAMETER (N=6,M=6)DIMENSION X(N),Y(M),Z(M,N),LK(3),LR(3)REAL(8) X,Y,Z,H,TREAL(8) U,V,W,LK,LR !U,V分别为插值点处的坐标,W为插值结果INTEGER R!**********************数据赋值********************** DATA Y/0.0,0.2,0.4,0.6,0.8,1.0/DATA X/0.0,0.4,0.8,1.2,1.6,2.0/DATA Z/-0.5,-0.42,-0.18,0.22,0.78,1.5,&&-0.34,-0.5,-0.5,-0.34,-0.02,0.46,&&0.14,-0.26,-0.5,-0.58,-0.5,-0.26,&&0.94,0.3,-0.18,-0.5,-0.66,-0.66,&&2.06,1.18,0.46,-0.1,-0.5,-0.74,&&3.5,2.38,1.42,0.62,-0.02,-0.5/H=0.4T=0.2!******************计算K,R************************* IF(UX(N-1)-H/2) THENK=N-1ELSEDO I=3,N-2IF((U>X(I)-H/2).AND.(UY(M-1)-T/2) THENR=M-1 ELSEDO J=3,M-2IF((V>Y(J)-T/2).AND.(VN) P=N IF(P>20) P=20IF(Q>M) Q=MIF(Q>20) Q=20XX=0YY=0D1=NAPX(1)=0.0DO I=1,NAPX(1)=APX(1)+X(I)ENDDOAPX(1)=APX(1)/D1DO J=1,MV(1,J)=0.0DO I=1,NV(1,J)=V(1,J)+Z(I,J)ENDDOV(1,J)=V(1,J)/D1ENDDOIF(P>1) THEND2=0.0APX(2)=0.0DO I=1,NG=X(I)-APX(1)D2=D2+G*GAPX(2)=APX(2)+(X(I)-XX)*G*G ENDDO APX(2)=APX(2)/D2BX(2)=D2/D1DO J=1,MV(2,J)=0.0DO I=1,NG=X(I)-APX(1)V(2,J)=V(2,J)+Z(I,J)*G ENDDOV(2,J)=V(2,J)/D2ENDDOD1=D2ENDIFDO K=3,PD2=0.0APX(K)=0.0DO J=1,MV(K,J)=0.0ENDDODO I=1,NG1=1.0G2=X(I)-APX(1)DO J=3,KG=(X(I)-APX(J-1))*G2-BX(J-1)*G1 G1=G2 G2=GENDDOD2=D2+G*GAPX(K)=APX(K)+X(I)*G*GDO J=1,M V(K,J)=V(K,J)+Z(I,J)*G ENDDOENDDODO J=1,MV(K,J)=V(K,J)/D2ENDDOAPX(K)=APX(K)/D2BX(K)=D2/D1D1=D2ENDDOD1=MAPY(1)=0.0DO I=1,MAPY(1)=APY(1)+Y(I)ENDDOAPY(1)=APY(1)/D1DO J=1,PU(J,1)=0.0DO I=1,MU(J,1)=U(J,1)+V(J,I) ENDDO U(J,1)=U(J,1)/D1ENDDOIF(Q>1)THEND2=0.0APY(2)=0.0DO I=1,MG=Y(I)-APY(1)D2=D2+G*G APY(2)=APY(2)+(Y(I))*G*G ENDDO APY(2)=APY(2)/D2BY(2)=D2/D1DO J=1,PU(J,2)=0.0DO I=1,MG=Y(I)-APY(1)U(J,2)=U(J,2)+V(J,I)*GENDDOU(J,2)=U(J,2)/D2ENDDOD1=D2ENDIFDO K=3,QD2=0.0APY(K)=0.0DO J=1,PU(J,K)=0.0ENDDODO I=1,MG1=1.0G2=Y(I)-APY(1)DO J=3,KG=(Y(I)-APY(J-1))*G2-BY(J-1)*G1 G1=G2 G2=GENDDOD2=D2+G*GAPY(K)=APY(K)+Y(I)*G*G DO J=1,PU(J,K)=U(J,K)+V(J,I)*G ENDDOENDDODO J=1,PU(J,K)=U(J,K)/D2ENDDOAPY(K)=APY(K)/D2BY(K)=D2/D1D1=D2ENDDOV(1,1)=1.0V(2,1)=-APY(1)V(2,2)=1.0DO I=1,PDO J=1,QA(I,J)=0.0ENDDOENDDODO I=3,QV(I,I)=V(I-1,I-1)V(I,I-1)=-APY(I-1)*V(I-1,I-1)+V(I-1,I-2)IF(I>=4) THENDO K=I-2,2,-1V(I,K)=-APY(I-1)*V(I-1,K)+V(I-1,K-1)-BY(I-1)*V(I-2,K) ENDDO ENDIFV(I,1)=-APY(I-1)*V(I-1,1)-BY(I-1)*V(I-2,1)ENDDO DO I=1,PIF(I==1) THENT(1)=1.0T1(1)=1.0ELSEIF(I==2) THENT(1)=-APX(1)T(2)=1.0T2(1)=T(1)T2(2)=T(2)ELSET(I)=T2(I-1)T(I-1)=-APX(I-1)*T2(I-1)+T2(I-2) IF(I>=4) THENDO K=I-2,2,-1T(K)=-APX(I-1)*T2(K)+T2(K-1)-BX(I-1)*T1(K) ENDDOENDIFT(1)=-APX(I-1)*T2(1)-BX(I-1)*T1(1)T2(I)=T(I)DO K=I-1,1,-1T1(K)=T2(K)T2(K)=T(K)ENDDOENDIFDO J=1,QDO K=I,1,-1DO L=J,1,-1A(K,L)=A(K,L)+U(I,J)*T(K)*V(J,L) ENDDOENDDOENDDOENDDODT1=0.0DO I=1,NX1=X(I)DO J=1,MY1=Y(J)X2=1.0DD=0.0DO K=1,PG=A(K,Q)DO KK=Q-1,1,-1G=G*Y1+A(K,KK)ENDDOG=G*X2DD=DD+GX2=X2*X1ENDDODT=DD-Z(I,J)DT1=DT1+DT*DTENDDOENDDORETURNEND三、计算结果数表(x,y,f(x,y)): X Y UX TY F(X,Y) 0.00 0.500 1.345 0.243 0.17E+000.00 0.550 1.322 0.269 0.66E+000.00 0.600 1.299 0.295 0.35E+000.00 0.650 1.277 0.322 0.94E+000.00 0.700 1.255 0.350 0.30E-020.00 0.750 1.235 0.377 -0.87E-010.00 0.800 1.215 0.406 -0.58E+000.00 0.850 1.196 0.434 -0.72E+000.00 0.900 1.177 0.463 -0.54E+000.00 0.950 1.159 0.492 -0.86E+000.00 1.050 1.125 0.550 -0.74E+00 0.00 1.100 1.109 0.580 -0.06E+00 0.00 1.150 1.093 0.609 -0.00E+00 0.00 1.200 1.0790.639 -0.18E+00 0.00 1.250 1.064 0.669 -0.52E+00 0.00 1.3001.050 0.699 -0.19E+00 0.00 1.350 1.037 0.729 -0.48E+00 0.001.400 1.024 0.759 -0.68E+00 0.00 1.450 1.011 0.790 -0.52E+00 0.00 1.500 1.000 0.820 -0.29E+000.08 0.500 1.415 0.228 0.67E+00 0.08 0.550 1.391 0.253 0.08E+00 0.08 0.600 1.368 0.279 0.02E+00 0.08 0.650 1.346 0.306 0.47E+00 0.08 0.700 1.325 0.333 0.57E+00 0.08 0.750 1.304 0.360 0.48E-01 0.08 0.800 1.284 0.388 -0.73E-01 0.08 0.850 1.265 0.416 -0.16E+00 0.08 0.900 1.246 0.444 -0.29E+00 0.08 0.950 1.229 0.473 -0.36E+00 0.08 1.000 1.211 0.502 -0.08E+00 0.08 1.050 1.194 0.531 -0.29E+00 0.08 1.100 1.178 0.560 -0.78E+00 0.08 1.150 1.163 0.589 -0.93E+00 0.08 1.200 1.148 0.619 -0.44E+00 0.08 1.250 1.133 0.649 -0.92E+00 0.08 1.300 1.119 0.679 -0.71E+000.08 1.400 1.093 0.739 -0.37E+00 0.08 1.450 1.080 0.769-0.83E+00 0.08 1.500 1.068 0.799 -0.92E+000.16 0.500 1.483 0.214 0.31E+00 0.16 0.550 1.460 0.239 0.64E+00 0.16 0.600 1.437 0.264 0.91E+00 0.16 0.650 1.414 0.290 0.06E+00 0.16 0.700 1.393 0.316 0.70E+00 0.16 0.750 1.372 0.343 0.59E+00 0.16 0.800 1.352 0.370 0.12E+00 0.16 0.850 1.333 0.398 0.77E-02 0.16 0.900 1.315 0.426 -0.83E-01 0.16 0.950 1.297 0.454-0.58E+00 0.16 1.000 1.279 0.483 -0.20E+00 0.16 1.050 1.2620.512 -0.11E+00 0.16 1.100 1.246 0.541 -0.74E+00 0.16 1.1501.231 0.570 -0.09E+00 0.16 1.200 1.216 0.600 -0.59E+00 0.16 1.250 1.201 0.629 -0.66E+00 0.16 1.300 1.187 0.659 -0.71E+00 0.16 1.350 1.174 0.689 -0.32E+00 0.16 1.400 1.161 0.718-0.56E+00 0.16 1.450 1.148 0.748 -0.31E+00 0.16 1.500 1.136 0.778 -0.75E+000.24 0.500 1.551 0.201 0.66E+01 0.24 0.550 1.527 0.2250.03E+000.24 0.650 1.482 0.275 0.64E+00 0.24 0.700 1.460 0.3010.47E+00 0.24 0.750 1.439 0.327 0.34E+00 0.24 0.800 1.419 0.354 0.24E+00 0.24 0.850 1.400 0.381 0.69E+00 0.24 0.900 1.381 0.409 0.04E-01 0.24 0.950 1.363 0.437 -0.42E-01 0.24 1.000 1.346 0.465 -0.06E+00 0.24 1.050 1.329 0.494 -0.59E+00 0.24 1.100 1.313 0.523 -0.83E+00 0.24 1.150 1.297 0.552 -0.15E+00 0.24 1.200 1.282 0.581 -0.19E+00 0.24 1.250 1.267 0.610 -0.84E+00 0.24 1.300 1.253 0.640 -0.66E+00 0.24 1.350 1.240 0.669 -0.30E+00 0.24 1.400 1.227 0.699 -0.86E+00 0.24 1.450 1.214 0.729 -0.84E+00 0.24 1.500 1.202 0.759 -0.77E+000.32 0.500 1.617 0.188 0.28E+01 0.32 0.550 1.593 0.212 0.49E+01 0.32 0.600 1.570 0.236 0.68E+00 0.32 0.650 1.547 0.261 0.75E+00 0.32 0.700 1.526 0.286 0.60E+00 0.32 0.750 1.505 0.312 0.77E+00 0.32 0.800 1.485 0.339 0.05E+00 0.32 0.850 1.466 0.365 0.99E+00 0.32 0.900 1.447 0.393 0.27E+00 0.32 1.000 1.411 0.448 -0.01E-02 0.32 1.050 1.395 0.477-0.41E-01 0.32 1.100 1.378 0.505 -0.18E+00 0.32 1.150 1.3630.534 -0.25E+00 0.32 1.200 1.347 0.563 -0.29E+00 0.32 1.2501.333 0.592 -0.90E+00 0.32 1.300 1.319 0.621 -0.00E+00 0.32 1.350 1.305 0.650 -0.40E+00 0.32 1.400 1.292 0.680 -0.54E+00 0.32 1.450 1.279 0.710 -0.79E+00 0.32 1.500 1.267 0.739-0.91E+000.40 0.500 1.681 0.177 0.91E+01 0.40 0.550 1.658 0.1990.00E+01 0.40 0.600 1.634 0.223 0.83E+01 0.40 0.650 1.612 0.247 0.02E+01 0.40 0.700 1.591 0.272 0.94E+00 0.40 0.750 1.570 0.298 0.49E+00 0.40 0.800 1.550 0.324 0.94E+00 0.40 0.850 1.530 0.350 0.40E+00 0.40 0.900 1.512 0.377 0.33E+00 0.40 0.950 1.493 0.405 0.99E+00 0.40 1.000 1.476 0.432 0.68E+00 0.40 1.050 1.459 0.460 0.08E-01 0.40 1.100 1.443 0.488 -0.84E-01 0.40 1.150 1.427 0.517-0.98E+00 0.40 1.200 1.412 0.545 -0.27E+00 0.40 1.250 1.397 0.574 -0.06E+000.40 1.350 1.369 0.632 -0.66E+00 0.40 1.400 1.356 0.662-0.37E+00 0.40 1.450 1.343 0.691 -0.43E+00 0.40 1.500 1.331 0.721 -0.12E+000.48 0.500 1.745 0.166 0.69E+01 0.48 0.550 1.721 0.188 0.02E+01 0.48 0.600 1.698 0.211 0.74E+01 0.48 0.650 1.676 0.235 0.40E+01 0.48 0.700 1.654 0.259 0.23E+01 0.48 0.750 1.634 0.284 0.56E+00 0.48 0.800 1.613 0.310 0.28E+00 0.48 0.850 1.594 0.336 0.49E+00 0.48 0.900 1.575 0.363 0.31E+00 0.48 0.950 1.557 0.390 0.66E+00 0.48 1.000 1.539 0.417 0.30E+00 0.48 1.050 1.522 0.444 0.34E+00 0.48 1.100 1.506 0.472 0.07E-01 0.48 1.150 1.490 0.500 -0.62E-01 0.48 1.200 1.475 0.529 -0.45E+00 0.48 1.250 1.460 0.557 -0.86E+00 0.48 1.300 1.446 0.586 -0.39E+00 0.48 1.350 1.432 0.615 -0.22E+00 0.48 1.400 1.419 0.644 -0.67E+00 0.48 1.450 1.406 0.674-0.55E+00 0.48 1.500 1.394 0.703 -0.14E+000.56 0.500 1.808 0.156 0.48E+010.56 0.600 1.761 0.200 0.10E+01 0.56 0.650 1.739 0.2230.68E+01 0.56 0.700 1.717 0.247 0.94E+01 0.56 0.750 1.696 0.272 0.33E+01 0.56 0.800 1.676 0.297 0.11E+00 0.56 0.850 1.657 0.323 0.63E+00 0.56 0.900 1.638 0.349 0.97E+00 0.56 0.950 1.620 0.375 0.52E+00 0.56 1.000 1.602 0.402 0.56E+00 0.56 1.050 1.585 0.429 0.47E+00 0.56 1.100 1.568 0.457 0.20E+00 0.56 1.150 1.552 0.485 0.13E+00 0.56 1.200 1.537 0.513 0.09E-01 0.56 1.250 1.522 0.541 -0.47E-01 0.56 1.300 1.508 0.570 -0.99E+00 0.56 1.350 1.4940.599 -0.82E+00 0.56 1.400 1.481 0.627 -0.26E+00 0.56 1.4501.468 0.657 -0.71E+00 0.56 1.500 1.455 0.686 -0.98E+000.64 0.500 1.870 0.147 0.74E+01 0.64 0.550 1.846 0.1680.10E+01 0.64 0.600 1.823 0.190 0.54E+01 0.64 0.650 1.801 0.213 0.42E+01 0.64 0.700 1.779 0.236 0.56E+01 0.64 0.750 1.758 0.260 0.03E+01 0.64 0.800 1.738 0.285 0.42E+01 0.64 0.850 1.718 0.310 0.41E+010.64 0.950 1.681 0.362 0.36E+00 0.64 1.000 1.664 0.388 0.18E+00 0.64 1.050 1.646 0.415 0.28E+00 0.64 1.100 1.630 0.443 0.07E+00 0.64 1.150 1.614 0.470 0.66E+00 0.64 1.200 1.598 0.498 0.09E+00 0.64 1.250 1.584 0.526 0.50E-01 0.64 1.300 1.569 0.554 -0.88E-01 0.64 1.350 1.555 0.583 -0.76E+00 0.64 1.400 1.542 0.611 -0.66E+00 0.64 1.450 1.529 0.640 -0.33E+00 0.64 1.500 1.516 0.669 -0.56E+00 0.72 0.500 1.931 0.139 0.94E+01 0.72 0.550 1.907 0.159 0.84E+01 0.72 0.600 1.884 0.181 0.36E+01 0.72 0.650 1.862 0.203 0.40E+01 0.72 0.700 1.840 0.226 0.47E+01 0.72 0.750 1.819 0.249 0.56E+01 0.72 0.800 1.799 0.273 0.19E+01 0.72 0.850 1.779 0.298 0.37E+01 0.72 0.900 1.760 0.323 0.86E+01 0.72 0.950 1.742 0.349 0.76E+00 0.72 1.000 1.724 0.375 0.24E+00 0.72 1.050 1.707 0.402 0.55E+00 0.72 1.100 1.691 0.429 0.97E+00 0.72 1.150 1.675 0.456 0.27E+00 0.72 1.200 1.659 0.484 0.31E+000.72 1.300 1.630 0.539 0.49E+00 0.72 1.350 1.616 0.5680.72E-02 0.72 1.400 1.602 0.596 -0.69E-01 0.72 1.450 1.589 0.625 -0.67E+00 0.72 1.500 1.576 0.653 -0.20E+000.80 0.500 1.992 0.131 0.31E+01 0.80 0.550 1.968 0.1510.44E+01 0.80 0.600 1.945 0.172 0.41E+01 0.80 0.650 1.922 0.193 0.45E+01 0.80 0.700 1.900 0.216 0.00E+01 0.80 0.750 1.879 0.239 0.10E+01 0.80 0.800 1.859 0.263 0.16E+01 0.80 0.850 1.840 0.287 0.52E+01 0.80 0.900 1.821 0.312 0.02E+01 0.80 0.950 1.802 0.337 0.38E+01 0.80 1.000 1.784 0.363 0.89E+01 0.80 1.050 1.767 0.389 0.28E+00 0.80 1.100 1.751 0.416 0.09E+00 0.80 1.150 1.734 0.4430.23E+00 0.80 1.200 1.719 0.470 0.93E+00 0.80 1.250 1.704 0.498 0.15E+00 0.80 1.300 1.689 0.525 0.86E+00 0.80 1.350 1.675 0.553 0.64E+00 0.80 1.400 1.662 0.582 0.74E-01 0.80 1.450 1.649 0.610 -0.37E-01 0.80 1.500 1.636 0.638 -0.81E+00K和σ分别为:0 0.93E+031 0.61E+012 0.92E-023 0.53E-034 0.16E-055 0.77E-07系数矩阵Crs(按行)为:0.00E+01 -0.83E+01 0.56E+00 0.97E+00 -0.03E+00 0.70E-010.91E+01 -0.99E+00 -0.96E+01 0.17E+01 -0.66E+00 0.10E-01 0.77E+00 0.42E+01 -0.10E+00 -0.81E+00 0.81E+00 -0.62E-01-0.25E+00 -0.21E+00 0.97E+00 -0.18E+00 0.49E+00 -0.63E-010.34E+00 -0.56E+00 0.69E-01 0.51E+00 -0.77E-01 0.27E-01-0.94E-01 0.94E+00 -0.58E+00 0.69E-01 -0.50E-01 0.53E-02 数表(x,y,f(x,y),p(x,y)):X Y F(X,Y) P(X,Y)0.100 0.700 0.58E+00 0.05E+000.100 1.100 -0.66E+00 -0.26E+00 0.100 1.300 -0.68E+00-0.31E+00 0.100 1.500 -0.52E+00 -0.49E+000.200 0.700 0.54E+00 0.19E+00 0.200 0.900 -0.63E-01 -0.65E-01 0.200 1.100 -0.90E+00 -0.90E+00 0.200 1.300 -0.84E+00 -0.90E+00 0.200 1.500 -0.03E+00 -0.04E+000.300 0.700 0.82E+00 0.09E+00 0.300 0.900 0.48E+00 0.11E+00 0.300 1.100 -0.63E+00 -0.88E+00 0.300 1.300 -0.72E+00 -0.96E+00 0.300 1.500 -0.34E+00 -0.84E+000.400 0.700 0.79E+00 0.89E+00 0.400 0.900 0.56E+00 0.63E+00 0.400 1.100 -0.83E-01 -0.04E-01 0.400 1.300 -0.72E+00 -0.71E+00 0.400 1.500 -0.85E+00 -0.07E+000.500 0.700 0.56E+01 0.92E+01 0.500 0.900 0.51E+00 0.23E+00 0.500 1.100 0.59E+00 0.27E+00 0.500 1.300 -0.53E+00 -0.11E+00 0.500 1.500 -0.67E+00 -0.33E+000.600 0.900 0.14E+00 0.75E+00 0.600 1.100 0.19E+00 0.32E+00 0.600 1.300 -0.70E-01 -0.82E-01 0.600 1.500 -0.08E+00 -0.75E+00 0.700 0.700 0.89E+01 0.29E+01 0.700 0.900 0.91E+01 0.11E+010.700 1.100 0.60E+00 0.97E+00 0.700 1.300 0.22E-01 0.06E-01 0.7001.500 -0.53E+00 -0.80E+00 0.800 0.700 0.09E+01 0.06E+01 0.800 0.900 0.32E+01 0.50E+01 0.800 1.100 0.03E+00 0.79E+00 0.800 1.300 0.25E+00 0.50E+00 0.800 1.500 -0.14E+00 -0.28E+00。
1998~2002年计算机二级考试FORTRAN部分1998年4月二级笔试试题FORTRAN部分21、下列选项中,所指定的FORTRAN表达式都是正确的是()①D=B * B - 4 * A * C②MOD(M,N).NE.O③.NOT.FALSE..OR..NOT..TRUE④A.GT.B.EQ.A.LT.C⑤π*R**2⑥3*E-2⑦A.GT.B.EQ..TRUE.⑧′123′//′ABC′A、①、②、③B、⑥、⑦、⑧C、④、⑤、⑥D、②、⑥、⑧22、执行下列程序,输入3456.789,则输出的结果为()。
READ(8 ,100) SWRITE( 8,100)S100 FORMAT(2F4.1)ENDA.345.6B.45.6C.456.D.* * *23、下列程序执行后输出( )DIMENSION M(5)READ(*,*)(M(I),I=1,5)DO 10 I=1,510 WRITE(*,22)(M(J),J=1,I)22 FORMAT(1X,3I3)ENDA、7B、8C、9D、524、FORTRAN 77规定,变量类型的优先顺序是()。
A、隐含约定(I-N规则)、Implicit说明、类型说明B、类型说明、隐含约定(I-N规则)、Implicit说明C、类型说明、Implicit说明、隐含约定(I-N规则)D、Implicit说明、类型说明、隐含约定(I-N规则)25、设T=.TRUE.F=.FALSE.下列表达式中值为.TRUE.的是( )。
A、.T.AND.F.AND.7.GE.4B、T.AND..NOT.T.AND.TC、((T.OR.F).AND.(T.AND.5.LE.4)).EQV.(.NOT.T.OR.(5.GE.1.AND.F))D、.NOT.(T.OR.F).NEQV.(T.AND.8-2.EQ.4)26 、下面循环语句执行后,SUM的值是()。
DO 10 I=1,5SUM=0DO 10 J=I,4,-110 SUM=SUM+I+JA、0.0B、19.0C.27.0D.76.027、下列语句中,正确的语句函数定义语句是()。
课程名称:Fortran语言程序设计;试卷编号:A 卷;考试时间:120分钟学院1.Fortran语言中有:整数型逻辑型字符型实数型复数型五种基本数据类型。
2.Fortran程序的书写格式有两种固定格式自由格式。
3. 字符型常数’this”s’的字符长度为64. 下列程序运行结果为125。
Program mainImplicit noneInteger::floor=5,iDo i=1, floorIf(i==3)cycleIf(i==4)cycleWrite(*,*)iEnd doend5. 下列程序的运行结果为76.0,2.0。
Program mainImplicit noneReal :: a, b, mA=2.0B=76.0If(a<b)thenM=aA=bB=mEnd ifWrite(*,*) “a=”,a “b=”,bend6. 下列程序的运行结果是10302。
Program testImplicit nonesum=0.0do i=1,203,2sum=sum+iend dowrite(*,*) iend7.下列程序的运行结果是12000。
Program testImplicit noneInteger::iInteger::a(5)=(/ (i, i=1,5) /)Integer::b(5)=0Where (a<3)b=aEnd whereWrite(*,”(5(i3,1x))”) bend8.下列程序的运行结果是30。
Program testImplicit noneInteger,parameter:: limit=10Integer counterInteger:: ans=0Counter=2do while(counter<=limit)ans=ans+countercounter=counter+2end doWrte(*,*)ansend9.下列程序的运行结果是2,3。
软件技术基础(FORTRAN)笔试模拟试题(带答案)一.概念选择题(单选题,将正确的答案填入括号内。
每题1分,共15分)1. 已知整型变量L=5,M=13,N=4,表达式L*M/N 、L/N*M 和M/N*L 的值分别是(A )。
A )16 13 15B )16.25 16.25 16.25C )16.0 13.0 15.0D )16 16 162.将数学式)||(ln 215-+e x 改写为FORTRAN 表达式,正确的是( D )。
A) 1/2*(LOG(ABS(X))+E**(-5))B) 1.0/2*(LN(ABS(X))+E**-5)C) 1/2.0*(LN(ABS(X))+E -5)D) 1.0/2*(LOG(ABS(X))+EXP(-5))3. 设A=.TRUE., B=.FALSE., C=.FALSE., 逻辑表达式(A.OR.B).AND.C 与A.OR..NOT.B 的值是( B )。
A) .T. 与 .F. B).F . 与 .T. C) .T. 与 .T. D) .F. 与 .F .4. 数组REAL A (0:5,-1:5)的元素个数是 ( D )。
A )25B )30C )36D )42 5. 下列数组说明符中错误的是( A )。
A )K(3:2,1:2)B ) K(-1:1,2)C )K(-2:2,-1:1)D )K(2:2,5)6. 关于USE 语句的正确说法是( C )。
A)USE 语句是可执行语句 B)用USE 语句可以代替INCLUDE 语句C)USE 语句必须放在程序单元体的开头 D)USE 语句的位置任意7. 语句OPEN (9,FILE=‘DATA.TXT ’)打开的是 ( B ) 文件。
A )无格式顺序B )有格式顺序C )无格式直接D )有格式直接8. 阅读下列FORTRAN 程序, 程序运行时执行循环体的次数是( A )。
X=0DO L=10, 5, 1X=X+1.0ENDDOENDA) 0 B) 1 C) 6 D) 无穷9.结构化程序设计中有三种基本结构,下列不属于三种基本设计结构的是( D )。
第四章1.program main implicit none write(*,*) "Have a good time." write(*,*) "That's not bad." write(*,*) '"Mary" isn''t my name.' end program2.program main real, parameter :: PI=3 implicit none.14159 real radius write(*,*) "请输入半径长" read(*,*) radius write(*,"(' 面积='f8. 3)") radius*radius*PI end program3.program main implicit none real grades write(*,*) "请输入成绩" read(*,*) grades write(*,"(' 调整后成绩为 'f8.3)") SQRT(grades)*10.0 end program4.integer a,b real ra,rb a=2 b=3 ra=2.0 rb=3.0 write(*,*) b/a ! 输出1, 因为使用整数计算, 小数部分会无条件舍去 write(*,*) rb/ra ! 输出1.55.p rogram main implicit none type distance real meter, inch, cm end type type(distance) :: d write(*,*) "请输入长度:" read(*,*) d%meter d%cm =d%meter*100 d%inch = d%cm/2.54 write(*,"(f8.3'米 ='f8.3'厘米 ='f8.3'英寸')")d%meter, d%cm, d%inch end program第五章1.program main implicit none integer money real tax write(*,*) "请输入月收入" read(*,*) money if ( money<1000 ) then tax = 0.03 else if( money<5000) then tax = 0.1 else tax = 0.15 end if write(*,"(' 税金为 'I8)") nint(money*tax) end program2.program main implicit none integer day character(len=20) :: tv write(*,*) "请输入星期几" read(*,*) day select case(day) case(1,4) tv= "新闻" case(2,5) tv = "电视剧" case(3,6) tv = "卡通" case(7) tv = "电影" case default write(*,*) "错误的输入" stop end select write(*,*) tv end program3.program main implicit none integer age, money real tax write(*,*) "请输入年龄" read(*,*) age write(*,*) "请输入月收入" read(*,*) money if( age<50 ) then if ( money<1000 ) then tax = 0.03 else if ( money<5000 )then tax = 0.10 else tax = 0.15 end if else if ( money<1000 ) then tax =0.5 else if ( money<5000 )then tax = 0.7 else tax = 0.10 end if end if write(*,"(' 税金为 'I8)") nint(money*tax) end program4.program main implicit none integer year, days logical mod_4, mod_100,mod_400 write(*,*) "请输入年份" read(*,*) year mod_4 = ( MOD(year,4) ==0 ) mod_100 = ( MOD(year,100) == 0 ) mod_400 = ( MOD(year,400) == 0 ) if( (mod_4 .NEQV. mod_100) .or. mod_400 ) then days = 366 else days = 365end if write(*,"('这一年有'I3'天')") days stop end program第六章1.program main implicit none integer i do i=1,5 write(*,*) "Fortran" end do stop end program2.program main implicit none integer i,sum sum = 0 do i=1,99,2 sum =sum+i end do write(*,*) sum stop end program3.program main implicit none integer, parameter :: answer = 45 integer, parameter :: max = 5 integer weight, i do i=1,max write(*,*) "请输入体重" read(*,*) weight if ( weight==answer ) exit end do if ( i<=max ) thenwrite(*,*) "猜对了" else write(*,*) "猜错了" end if stop end program4.program main implicit none integer, parameter :: max=10 integer i realitem real ans ans = 1.0 item = 1.0 do i=2,max item = item/real(i)ans = ans+item end do write(*,*) ans stop end program5.program main implicit none integer, parameter :: length = 79 character(len=length) :: input, output integer i,j write(*,*) "请输入一个字串" read(*,"(A79)") input j=1 do i=1, len_trim(input) if ( input(i:i)/= ' ' ) then output(j:j)=input(i:i) j=j+1 end if end do write(*,"(A79)") output stop end program第七章1.program main implicit none integer, parameter :: max = 10 integer i integer :: a(max) = (/ (2*i, i=1,10) /) integer :: t ! sum()是fortran库函数write(*,*) real(sum(a))/real(max) stop end program2.integer a(5,5) ! 5*5=25 integer b(2,3,4) ! 2*3*4=24 integer c(3,4,5,6) !3*4*5*6=360 integer d(-5:5) ! 11 integer e(-3:3, -3:3) ! 7*7=493.program main implicit none integer, parameter :: max=10 integer f(max) integer i f(1)=0 f(2)=1 do i=3,max f(i)=f(i-1)+f(i-2) end do write(*,"(10I4)") f stop end program4.program main implicit none integer, parameter :: size=10 integer :: a(size)= (/ 5,3,6,4,8,7,1,9,2,10 /) integer :: i,j integer :: t do i=1, size-1do j=i+1, size if ( a(i) < a(j) ) then ! a(i)跟a(j)交换 t=a(i) a(i)=a(j) a(j)=t end if end do end do write(*,"(10I4)") a stop end5.a(2,2) ! 1+(2-1)+(2-1)*(5) = 7 a(3,3) ! 1+(3-1)+(3-1)*(5) = 13第八章1.program main implicit none real radius, area write(*,*) "请输入半径长"read(*,*) radius call CircleArea(radius, area) write(*,"(' 面积 = 'F8.3)")area stop end program subroutine CircleArea(radius, area) implicit nonereal, parameter :: PI=3.14159 real radius, area area = radius*radius*PIreturn end subroutine2.program main implicit none real radius real, external :: CircleAreawrite(*,*) "请输入半径长" read(*,*) radius write(*,"(' 面积= 'F8.3)") CircleArea(radius) stop end program real function CircleArea(radius) implicit none real, parameter :: PI=3.14159 real radius CircleArea =radius*radius*PI return end function3.program main implicit none call bar(3) call bar(10) stop end program subroutine bar(length) implicit none integer, intent(in) :: length integeri character(len=79) :: string string=" " do i=1,length string(i:i)='*'end do write(*,"(A79)") string return end subroutine4.p rogram main implicit none integer, external :: add write(*,*) add(100)end program recursive integer function add(n) result(sum) implicit none integer, intent(in) :: n if ( n<0 ) then sum=0 return else if ( n<=1 )then sum=n return end if sum = n + add(n-1) return end function5.program main implicit none integer, external :: gcd write(*,*) gcd(18,12)end program integer function gcd(A,B) implicit none integerA,B,BIG,SMALL,TEMP BIG=max(A,B) SMALL=min(A,B) do while( SMALL /= 1 ) TEMP=mod(BIG,SMALL) if ( TEMP==0 ) exit BIG=SMALL SMALL=TEMP end dogcd=SMALL return end function 6.program main use TextGraphLib implicit none integer, parameter :: maxx=60, maxy=20 real, parameter :: StartX=0.0,EndX=3.14159*2.0 real, parameter :: xinc = (EndX-StartX)/(maxx-1) real x integer i,px,py call SetScreen(60,20) call SetCurrentChar('*') x=StartXdo px=1,maxx py = (maxy/2)*sin(x)+maxy/2+1 call PutChar(px,py) x=x+xincend docall UpdateScreen() stop end program第九章1.program main implicit none character(len=79) :: filename character(len=79) :: buffer integer, parameter :: fileid = 10 integer count integer :: status = 0 logical alive write(*,*) "Filename:" read (*,"(A79)") filename inquire( file=filename, exist=alive) if ( alive ) then open(unit=fileid, file=filename, & access="sequential", status="old")count = 0 do while(.true.) read(unit=fileid, fmt="(A79)", iostat=status )buffer if ( status/=0 ) exit ! 没有资料就跳出循环 write(*,"(A79)")buffer count = count+1 if ( count==24 ) then pause count = 0 endif end do else write(*,*) TRIM(filename)," doesn't exist." end ifstop end2.p rogram main implicit none character(len=79) :: filename character(len=79) :: buffer integer, parameter :: fileid = 10 integer iinteger :: status = 0 logical alive write(*,*) "Filename:" read (*,"(A79)") filename inquire( file=filename, exist=alive) if ( alive ) then open(unit=fileid, file=filename, & access="sequential", status="old")do while(.true.) read(unit=fileid, fmt="(A79)", iostat=status ) bufferif ( status/=0 ) exit ! 没有资料就跳出循环do i=1, len_trim(buffer) buffer(i:i) = char( ichar(buffer(i:i))-3 ) end do write(*,"(A70)") bufferend do else write(*,*) TRIM(filename)," doesn't exist." end if stop end3.program main implicit none type student integer chinese, english, math, science, social, total end type type(student) :: s, total integer, parameter :: students=20, subjects=5 integer i open(10,file="grades.bin",access="direct",recl=1) write(*,"(7A10)") "座号","中文","英文","数学","自然","社会","总分" total = student(0,0,0,0,0,0) do i=1, students read(10,rec=(i-1)*subjects+1) s%chinese read(10,rec=(i-1)*subjects+2) s%english read(10,rec=(i-1)*subjects+3) s%math read(10,rec=(i-1)*subjects+4) s%science read(10,rec=(i-1)*subjects+5)s%social s%total = s%chinese+s%english+s%math+s%science+s%social total%chinese= total%chinese+s%chinese total%english = total%english+s%english total%math =total%math+s%math total%science = total%science+s%science total%social =total%social+s%social total%total = total%total+s%total write(*,"(7I10)") i,s end do write(*,"(A10,6F10.3)") "平均", & real(total%chinese)/real(students),&real(total%english)/real(students),& real(total%math)/real(students),& real(total%science)/real(students),&real(total%social)/real(students),& real(total%total)/real(students)stop end 4.program main implicit none character(len=79) :: filename character(len=79) :: buffer integer, parameter :: fileid = 10 integer i integer :: status = 0 logical alive write(*,*) "Filename:" read (*,"(A79)")filename inquire( file=filename, exist=alive) if ( alive ) then open(unit=fileid, file=filename, & access="sequential", status="old") do while(.true.) read(unit=fileid, fmt="(A79)", iostat=status ) buffer if ( status/=0 ) exit ! 没有数据就跳出循环do i=1, len_trim(buffer) buffer(i:i) = char( ichar(buffer(i:i))-(mod(i-1,3)+1) ) end do write(*,"(A70)") buffer end do else write(*,*) TRIM(filename)," doesn't exist." end if stop end5.module typedef type student integer :: num integer :: Chinese, English, Math, Natural, Social integer :: total integer :: rank end type end module program main use typedef implicit none integer, parameter :: fileid=10 integer, parameter :: students=20 character(len=80) :: tempstr type(student) :: s(students) ! 储存学生成绩 type(student) :: total ! 计算平均分数用integer i, num, error open(fileid,file="grades.txt",status="old", iostat=error) if ( error/=0 ) then write(*,*) "Open grades.txt fail." stop end if read(fileid, "(A80)") tempstr ! 读入第一行文字 total=student(0,0,0,0,0,0,0,0) ! 用循环读入每位学生的成绩do i=1,students read(fileid,*) s(i)%num, s(i)%Chinese,s(i)%English, & s(i)%Math, s(i)%Natural, s(i)%Social ! 计算总分 s(i)%Total = s(i)%Chinese + s(i)%English + & s(i)%Math +s(i)%Natural + s(i)%Social ! 累加上各科的分数, 计算各科平均时使用total%Chinese = total%Chinese + s(i)%Chinese total%English = total%English +s(i)%English total%Math = total%Math + s(i)%Math total%Natural = total%Natural + s(i)%Natural total%Social = total%Social + s(i)%Social total%Total = total%Total + s(i)%Total end do call sort(s,students) ! 重新输出每位学生成绩 write(*,"(8A7)") "座号","中文","英文","数学","自然","社会","总分","名次" do i=1,students write(*,"(8I7)") s(i) end do ! 计算并输出平圴分数write(*,"(A7,6F7.1)") "平均", &real(total%Chinese)/real(students),& real(total%English)/real(students),&real(total%Math) /real(students),& real(total%Natural)/real(students),&real(total%Social) /real(students),& real(total%Total) /real(students) stop end program subroutine sort(s,n) use typedef implicit none integer n type(student) :: s(n), t integer i,j do i=1,n-1 do j=i+1,n if( s(i)%total < s(j)%total ) then t = s(i) s(i)=s(j) s(j) = t end ifend do end do forall(i=1:n) s(i)%rank = i end forall end subroutine第十章1.integer(kind=4) :: a ! 4 bytes real(kind=4) :: b ! 4 bytes real(kind=8) :: c !8 bytes character(len=10) :: str ! 10 bytes integer(kind=4), pointer :: pa !4 bytes real(kind=4), pointer :: pb ! 4 bytes real(kind=8), pointer :: pc !4 bytes character(len=10), pointer :: pstr ! 4 bytes type student integer Chinese, English, Math end type type(student) :: s ! 12 bytes type(student), pointer ::ps ! 4 bytes2.integer, target :: a = 1 integer, target :: b = 2 integer, target :: c = 3 integer, pointer :: p p=>a write(*,*) p ! 1 p=>b write(*,*) p ! 2 p=>c p=5 write(*,*) c !53.module linklist type student integer :: num integer :: Chinese, English,Math, Science, Social end type type datalink type(student) :: item type(datalink), pointer :: next end type contains function SearchList(num, head) implicit none integer :: num type(datalink), pointer :: head, p type(datalink), pointer :: SearchList p=>head nullify(SearchList) do while( associated(p) ) if ( p%item%num==num ) then SearchList => p return end if p=>p%next end do return end function end module linklist programex1016 use linklist implicit none character(len=20) :: filename character(len=80) :: tempstr type(datalink), pointer :: head type(datalink), pointer :: p type(student), allocatable :: s(:) integer i,error,size write(*,*) "filename:" read(*,*) filename open(10, file=filename, status="old", iostat=error) if ( error/=0 ) then write(*,*) "Open file fail!" stop end if allocate(head) nullify(head%next) p=>head size=0 read(10, "(A80)") tempstr ! 读入第一行字符串, 不需要处理它 ! 读入每一位学生的成绩 do while(.true.) read(10,fmt=*, iostat=error) p%item if ( error/=0 )exit size=size+1 allocate(p%next, stat=error) ! 新增下一个数据 if ( error/=0 )then write(*,*) "Out of memory!" stop end if p=>p%next ! 移动到链表的下一个数据 nullify(p%next) end do write(*,"('总共有',I3,'位学生')") size allocate( s(size) ) p=>head do i=1,size s(i)=p%item p=>p%next end do do while(.true.) write(*,*) "要查询几号同学的成绩?" read (*,*) i if( i<1 .or. i>size ) exit ! 输入不合理的座号 write(*,"(5(A6,I3))") "中文",s(i)%Chinese,& "英文",s(i)%English,& "数学",s(i)%Math,& "自然",s(i)%Science,& "社会",s(i)%Social end do write(*,"('座号',I3,'不存在, 程序结束.')") i stopend program4.module typedef implicit none type :: datalink integer :: i type(datalink), pointer :: next end type datalink end module typedef programex1012 use typedef implicit none type(datalink) , pointer :: p, head, next integer :: i,n,err write(*,*) 'Input N:' read(*,*) n allocate( head ) head%i=1 nullify(head%next) p=>head do i=2,n allocate( p%next,stat=err ) if ( err /= 0 ) then write(*,*) 'Out of memory!' stop end if p=>p%next p%i=i end do nullify(p%next) p=>head do while(associated(p)) write(*, "(i5)" ) p%i p=>p%next end do ! 释放链表的存储空间p=>head do while(associated(p)) next => p%next deallocate(p) p=>next end do stop end program第十一章1.module utility implicit none interface area module procedure CircleArea module procedure RectArea end interface contains real function CircleArea(r) real, parameter :: PI=3.14159 real r CircleArea =r*r*PI return end function real function RectArea(a,b) real a,b RectArea = a*b return end function end module program main use UTILITY implicit none write(*,*) area(1.0) write(*,*) area(2.0,3.0) stop end program2.module time_utility implicit none type :: time integer :: hour,minute,second end type time interface operator(+) module procedureadd_time_time end interface contains function add_time_time( a, b ) implicit none type(time) :: add_time_time type(time), intent(in) :: a,binteger :: seconds,minutes,carry seconds=a%second+b%second carry=seconds/60 minutes=a%minute+b%minute+carry carry=minutes/60add_time_time%second=mod(seconds,60) add_time_time%minute=mod(minutes,60)add_time_time%hour=a%hour+b%hour+carry return end function add_time_time subroutine input( a ) implicit none type(time), intent(out) :: a write(*,*) " Input hours:" read (*,*) a%hour write(*,*) " Input minutes:"read (*,*) a%minute write(*,*) " Input seconds:" read (*,*) a%second return end subroutine input subroutine output( a ) implicit nonetype(time), intent(in) :: a write(*, "(I3,' hours',I3,' minutes',I3,'seconds')" ) a%hour,a%minute,a%second return end subroutine output endmodule time_utility program main use time_utility implicit nonetype(time) :: a,b,c call input(a) call input(b) c=a+b call output(c)stop end program main3.module rational_utility implicit none private public :: rational, & operator(+), operator(-), operator(*),& operator(/), assignment(=),operator(>),& operator(<), operator(==),operator(/=),& output, input type :: rational integer :: num,denom end type rational interface operator(+) module procedurerat__rat_plus_rat end interface interface operator(-) module procedurerat__rat_minus_rat end interface interface operator(*) module procedurerat__rat_times_rat end interface interface operator(/) module procedurerat__rat_div_rat end interface interface assignment(=) module procedurerat_eq_rat module procedure int_eq_rat module procedure real_eq_rat endinterface interface operator(>) module procedure rat_gt_rat endinterface interface operator(<) module procedure rat_lt_rat endinterface interface operator(==) module procedure rat_compare_rat endinterface interface operator(/=) module procedure rat_ne_rat endinterface contains function rat_gt_rat(a,b) implicit none logical ::rat_gt_rat type(rational), intent(in) :: a,b real :: fa,fbfa=real(a%num)/real(a%denom) fb=real(b%num)/real(b%denom) if ( fa > fb )then rat_gt_rat=.true. else rat_gt_rat=.false. end if return end function rat_gt_rat function rat_lt_rat(a,b) implicit none logical :: rat_lt_rat type(rational), intent(in) :: a,b real :: fa,fbfa=real(a%num)/real(a%denom) fb=real(b%num)/real(b%denom) if ( fb > fa )then rat_lt_rat=.true. else rat_lt_rat=.false. end if return end function rat_lt_rat function rat_compare_rat(a,b) implicitnone logical :: rat_compare_rat type(rational), intent(in) :: a,btype(rational) :: c c=a-b if ( c%num == 0 ) thenrat_compare_rat=.true. else rat_compare_rat=.false. end if return end function rat_compare_rat function rat_ne_rat(a,b) implicitnone logical :: rat_ne_rat type(rational), intent(in) :: a,btype(rational) :: c c=a-b if ( c%num==0 ) then rat_ne_rat=.false.else rat_ne_rat=.true. end if return end function rat_ne_rat subroutine rat_eq_rat( rat1, rat2 ) implicit none type(rational),intent(out):: rat1 type(rational), intent(in) :: rat2 rat1%num =rat2%num rat1%denom = rat2%denom return end subroutine rat_eq_ratsubroutine int_eq_rat( int, rat ) implicit none integer, intent(out):: inttype(rational), intent(in) :: rat int = rat%num / rat%denom return end subroutine int_eq_rat subroutine real_eq_rat( float, rat ) implicit none real, intent(out) :: float type(rational), intent(in) :: rat float =real(rat%num) / real(rat%denom) return end subroutine real_eq_rat function reduse( a ) implicit none type(rational), intent(in) :: a integer :: b type(rational) :: reduse b=gcv_interface(a%num,a%denom) reduse%num = a%num/b reduse%denom = a%denom/b return end function reduse function gcv_interface(a,b) implicit none integer, intent(in) ::a,b integer :: gcv_interface if ( min(a,b) .eq. 0 ) then gcv_interface=1 return end if if (a==b) then gcv_interface=a return else if ( a>b ) then gcv_interface=gcv(a,b) else if ( a<b ) then gcv_interface=gcv(b,a) end if return end function gcv_interface recursive function gcv(a,b) result(ans) implicitnone integer, intent(in) :: a,b integer :: m integer :: ans m=mod(a,b) select case(m) case(0) ans=b return case(1) ans=1 return case default ans=gcv(b,m) end select return end function gcv function rat__rat_plus_rat( rat1, rat2 ) implicitnone type(rational) :: rat__rat_plus_rat type(rational), intent(in) ::rat1,rat2 type(rational) :: act act%denom= rat1%denom * rat2%denom act%num = rat1%num*rat2%denom + rat2%num*rat1%denom rat__rat_plus_rat = reduse(act) return end function rat__rat_plus_rat functionrat__rat_minus_rat( rat1, rat2 ) implicit none type(rational) ::rat__rat_minus_rat type(rational), intent(in) :: rat1, rat2 type(rational) :: temp temp%denom = rat1%denom*rat2%denom temp%num =rat1%num*rat2%denom - rat2%num*rat1%denom rat__rat_minus_rat = reduse( temp ) return end function rat__rat_minus_rat function rat__rat_times_rat( rat1,rat2 ) implicit none type(rational) :: rat__rat_times_rat type(rational), intent(in) :: rat1, rat2 type(rational) :: temp temp%denom = rat1%denom* rat2%denom temp%num = rat1%num * rat2%num rat__rat_times_rat = reduse(temp) return end function rat__rat_times_rat function rat__rat_div_rat( rat1, rat2 ) implicit none type(rational) ::rat__rat_div_rat type(rational), intent(in) :: rat1, rat2 type(rational) :: temp temp%denom = rat1%denom* rat2%num temp%num =rat1%num * rat2%denom rat__rat_div_rat = reduse(temp) return end function rat__rat_div_rat subroutine input(a) implicit none type(rational), intent(out) :: a write(*,*) "分子:" read(*,*) a%num write(*,*) "分母:" read(*,*) a%denom return end subroutine input subroutine output(a) implicit none type(rational), intent(in) :: a if ( a%denom/=1 ) then write(*, "(' (',I3,'/',I3,')' )" ) a%num,a%denom else write(*, "(I3)" ) a%num end if return end subroutine outputend module rational_utility program main use rational_utility implicit none type(rational) :: a,b,c call input(a) call input(b) c=a+b write(*,*)"a+b=" call output(c) c=a-b write(*,*) "a-b=" call output(c) c=a*b write(*,*) "a*b=" call output(c) c=a/b write(*,*) "a/b=" call output(c)if (a>b) write(*,*) "a>b" if (a<b) write(*,*) "a<b" if (a==b) write(*,*) "a==b"if (a/=b) write(*,*) "a/=b" stop end program main4.module vector_utility implicit none type vector real x,y end type interface operator(+) module procedure vector_add_vector end interface interface operator(-) module procedure vector_sub_vector end interface interface operator(*) module procedure real_mul_vector module procedure vector_mul_real module procedure vector_dot_vector end interface interface operator(.dot.) module procedure vector_dot_vector end interface contains type(vector) function vector_add_vector(a,b) type(vector), intent(in) :: a,b vector_add_vector = vector(a%x+b%x, a%y+b%y) end function type(vector) function vector_sub_vector(a,b) type(vector), intent(in) :: a,b vector_sub_vector = vector(a%x-b%x, a%y-b%y) end function type(vector) function real_mul_vector(a,b) real, intent(in) :: a type(vector), intent(in) :: b real_mul_vector = vector( a*b%x, a*b%y ) end functiontype(vector) function vector_mul_real(a,b) type(vector), intent(in) :: a real, intent(in) :: b vector_mul_real = real_mul_vector(b,a) end function real function vector_dot_vector(a,b) type(vector), intent(in) :: a,b vector_dot_vector = a%x*b%x + a%y*b%y end function subroutine output(vec) type(vector) :: vec write(*,"('('F6.2','F6.2')')") vec end subroutine end module program main use vector_utility implicit none type(vector) a,b,ca=vector(1.0, 2.0) b=vector(2.0, 1.0) c=a+b call output(c) c=a-b call output(c) write(*,*) a*b end program main。
工程分析程序设计上机作业(五)模块化编程上机目的:掌握内部例程、主程序、外部例程、模块等功能的使用方法。
1、有一个六边形,求其面积。
为求面积,作了3条辅助线。
如图所示:(提示,三角形面* a b c积=s(s-a)(s-b)(s-c),其中s ,a、b、c为三个边长)。
要求用内部函数来计算每个三角形的面积。
program mai nimplicit nonereal :: A(9)=(/10,30,16,13,21,14,20,36,28/)in teger ireal ss=area(A (3) ,A (4) ,A(7))+area(A(2),A(7),A(8))+ &area(A(1),A(8),A(9))+area(A (5),A (6) ,A(9))prin t*,'The area is',scontainsfunction area(x,y,z)implicit nonereal x,y,zreal p,areap=(x+y+z)/2area=sqrt(p*(p-x)*(p-y)*(p-z))end functionend2、编写一个子例程子程序,SUM(S , T , N1 , N2)。
把整型数N1到N2进行求和,并把求和的结果放置到S,把整型数N1到N2进行求积,并把求积的结果放置到T。
并用这个子程序来计算:y =(1 2 3 4) (3 4 5 6 7 8) (3*4*5*6) -(1*2*3)program mai nimplicit nonein teger A(8)in teger i,b,c,d,e,f,g,m, n,yb=O;d=O;g=1; n=1 do i=1,8A(i)=iend docall sum(b,c,1,4) call sum(d,e,3,8) call sum(f,g,3,6) call sum (m,n ,1,3)y=b+d+g-nprin t*,'y=(1+2+3+4)+(3+4+5+6+7+8)+(3*4*5*6)-(1*2*3)=',ycontainssubroutine sum(s,t,N1,N2) in teger s,t,N1,N2,i do i=N1,N2 s=s+A(i) t=t*A(i)end doend subrout ineend3、编写函数子程序GDC求两个数的最大公约数。
fortran习题答案【篇一:fortran习题1答案】txt>上机目的:练习c语言的书写、循环和判断结构1. 编写程序实现摄氏度和华氏度的相互转换:f?c*9/5?32#include stdio.hint main(void){ float c,f;printf(请输入摄氏温度:\n);scanf(%f,c);f=c*9/5+32;printf(对应的华氏温度为:\n);printf(%.2f\n,f);}2. 打印出6行杨辉三角形如下图:11 11 2 11 3 3 11 4 6 4 11 5 10 10 5 1#include stdio.h#define n 6int main(){int a[n][n],i,j; for (i=0;in;i++) { a[i][0]=1; a[i][i]=1; } for(i=2;in;i++) for (j=1;ji;j++)a[i][j]=a[i-1][j-1]+a[i-1][j]; for(i=0;in;i++) {}for (j=0;j=i;j++)printf(%4d,a[i][j]); printf(\n); } return 0;3. 求出数列2/1,3/2,5/3,8/5,13/8,21/13...的前10项之和。
#include stdio.hint main(void){} float a,b,c,i,s; a=1;b=2;s=0; for (i=1;i=10;i++) { } printf(数列前10项的和为:%f\n,s); s=s+b/a; c=a+b; a=b; b=c;4. 输入若干实数,请编写程序用于统计每个正数和负数的个数。
5. 从键盘上输入三条边长,判断是否能组成三角形。
#include stdio.hint main(void){float a,b,c;printf(请输入三个边长:\n); scanf(%f%f%f,a,b,c);if (a+bca+cbb+ca) printf(这三条边可以围成三角形\n);} else printf(这三条边不可以围成三角形\n);6. 输入某个点的坐标(a, b),判断该点是否位于圆心(x, y)、半径为r的圆内。
FORTRAN习题答案习题二一、问答题1. 给出下面变量名称,哪些是合法变量?哪些是非法变量?说明原因。
Count 、num_2、x&y 、4x+5y 、china-suzhou 、$us 、AbCdE 、Mr.bai 、t5、_another 、school_class_25、#125、2002Y 、π、β、A01/02、alpha 、date(1) 1. 判定下面整数,指出哪些是合法整数,哪些是非法整数?说明原因。
-0、+ 215、$125、3,245,895、5.3245、5#384、-524_3、#5DFE 、23-345、16#1A2B 、38#ABCD 、8#275_2、+327890、4 #3212. 判定下面实数,指出哪些是合法实数,哪些是非法实数?说明原因。
-0E2、45.2345E3.5、-5489E25_8、-.2345E-35、$185.45E 、+ 2.753425E24_3、58D85、+0.E-0、-00000.001E5、5,443,223.44、-12 34E+2、+ 18.5E 18、2.5E42习题三一、选择题1.下面是Visual Fortran 中正确的表达式是。
(A )A*COS(X)+∣B ∣ (B )2*EXP(2*X)/SQRT(16.0)(C )B 2-4AC (D )MOD (24.5,0.5)2.下面算术赋值语句中正确的语句是。
(A )M*N=(X-Y)/Z (B )+R=A+B/C(C )X=Y=Z-1.0 (D )Y=A*B/C/D3.算术表达式1/3+2/3的值为。
(A )0 (B ) 1 (C ) 0.99999999 (D )值不确定二、问答题1. 将下列代数式用Visual Fortran 表达式描述:① ② ③ 4sin 3A-3sinA+sin3A ④ 2.执行下列赋值语句后,变量中的值。
变量的类型遵循I —N 规则。
工程分析程序设计上机作业(四)数组上机目的:练习数组的声明、存储、操作,以及数组参数、动态数组的使用。
1、请声明一个大小为10的一维数组,它们的初值为A(1)=2, A(2)=4, A(3)=6, ……A(I)=2*I,并计算数组中这10个数字的平均值。
program mainimplicit noneinteger A(10)integer i,sumsum=0do i=1,10A(i)=2*isum=sum+A(i)end doprint*,'The average of the ten numbers is:',sum/10.end2、编写一个程序来计算费氏数列的前10项,并把它们按顺序保存在一个一维数组当中。
费氏数列(Fibonacci Sequence)的数列规则如下:F(0)=0F(1)=1当n>1时F(n)=f(n-1)+f(n-2)program mainimplicit noneinteger iinteger(2) A(11)A(1)=0; A(2)=1do i=3,11A(i)=A(i-1)+A(i-2)end doprint*,A(2:11)End3、输入任意n个数存放在数组中(如5个数1、2、8、2、10),请在屏幕上打印如下方阵1 2 8 2 1010 1 2 8 22 10 1 2 88 2 10 1 22 8 2 10 1program mainimplicit noneinteger ,allocatable ::A(:)integer(2) i,j,n,tempprint*,'请输入数字的个数:'read*,nallocate(A(n))print*,'请输入数字:'read*,A(1:n)print*,Ado i=2,ntemp=A(1)do j=1,n-1A(j)=A(j+1)end doA(n)=tempprint*,Aend dodeallocate(A)end4、打印杨辉三角形,打印的行数由键盘输入。
11 11 2 11 3 3 11 4 6 4 1program mainimplicit noneinteger(2),allocatable::A(:,:)integer i,j,nprint*,'请输入要打印的行数:'read*,nallocate(A(n,n))do i=1,nA(i,1)=1;A(i,i)=1end doif(n>=2) thendo i=1,n-1do j=1,iA(i+1,j+1)=A(i,j)+A(i,j+1)end doend doend ifdo i=1,nprint*,A(i,1:i)end dodeallocate(A)end5、用“冒泡算法”对一个数列A(n)进行排序:若要排序的数有n个,则需要n-1轮排序。
第j轮排序中,从第一个数开始,相邻两数比较,若不符合所要求的顺序,则交换两者的位置;直到第n-j个数为止,第一个数与第二个数比较,第二个数与第三个数比较,......,第n-j-1个与第n-j个比较,共比较n-1次。
此时第n-j个位置上的数已经按要求排好,所以不参加以后的比较和交换操作。
例如:第一轮排序:第一个数与第二个数进行比较,若不符合要求的顺序,则交换两者的位置,否则继续进行二个数与第三个数比较......。
直到完成第n-1个数与第n个数的比较。
此时第n个位置上的数已经按要求排好,它不参与以后的比较和交换操作;第二轮排序:第一个数与第二个数进行比较,......直到完成第n-2个数与第n-1个数的比较;......第n-1轮排序:第一个数与第二个数进行比较,若符合所要求的顺序,则结束冒泡法排序;若不符合要求的顺序,则交换两者的位置,然后结束冒泡法排序。
共n-1轮排序处理,第j轮进行n-j次比较。
算法描述:如果共有n个数:第1个数要进行n-1次两两比较第2个数要进行n-2次两两比较第j个数要进行n-j次两两比较,j=1, n-jn个数总共要进行n-1次排序DO I = 1, N-1DO J = 1, N-J如果A(J) > A(J+1) 交换A(J) 和A(J+1)END DOEND DOprogram mainimplicit noneinteger ,allocatable ::A(:)integer(2) i,j,k,n,tempprint*,'请输入数字的个数:'read*,nallocate(A(n))print*,'请输入数字:'read*,A(1:n)do i=1,n-1do j=1,n-1if (A(j)>A(j+1)) thentemp=A(j)A(j)=A(j+1)A(j+1)=tempend ifend doend doprint*,Adeallocate(A)end6、从A、B两个数列中,把同时出现在两个数列中的数据删去。
例如:A:2 5 5 8 9 12 18B:5 8 12 12 14操作完成后:A:2 9 18B:14program mainimplicit noneinteger(2) A(7),B(5),Temp(7)external Subdata A /2,5,5,8,9,12,18/data B /5,8,12,12,14/integer m,nTemp=Aprint*,'删去相同的数后:'print*,'A'call Sub(Temp,B,7,5)print*,'B'call Sub(B,A,5,7)endsubroutine Sub(C,D,m,n) integer(2) C(m),D(n) integer i,j,k,m,n,p p=m 10 do i=1,p do j=1,n if (C(i)==D(j)) then do k=i,p-1 C(k)=C(k+1) end do p=p-1 goto 10 end if end do end do print*,C(1:p)end subroutine7、 高斯消去法求解线性代数方程组:对于一般的n 阶方程组,⎪⎪⎩⎪⎪⎨⎧=+++=+++=+++nn nn n n n n n n b x a x a x a b x a x a x a b x a x a x a 22112222212111212111.....................................................高斯消去法步骤如下:第一步:若011≠a ,令n i a a l i i ,...,3,2,/1111==,用()1i l -乘第1个方程加到第i 个方程上()n i ,...,3,2=,得同解方程组(1)(1)(1)(1)11112211(2)(2)(2)22222(2)(2)(2)22...................................n n n n n nn n n a x a x a x b a x a x b a x a x b ⎧+++=⎪++=⎪⎨⎪⎪++=⎩其中 .,,3,2,,,,3,2,,,,,,3,2,1,11)2(11)2(1)1(11)1(1n i b l b b n j i a l a a b b n j a a i i i j i ij ij j j =-==-==== 第二步:若,0)2(22≠a 令n i a a l i i ,,4,3,/)2(22)2(22 ==,用)(2i l -乘第2个方程加到第i 个方程上),,4,3(n i =,则将),,4,3()2(2n i a i =消去。
一般,设第k-1步后方程组化为如下的同解方程组(1)(1)(1)(1)11112211(2)(2)(2)22222()()()()()()............................................................................n n n n k k k kk k kn n k k k k nk n nn n n a x a x a x b a x a x b a x a x b a x a x b ⎧+++=⎪++=⎪⎪⎪⎨++=⎪⎪⎪⎪++=⎩则第k 步:若若,0)(≠k kk a 令n k k i a a l k kk k ik ik ,,2,1,/)()( ++==,用)(ik l -乘第k 个方程加到第i个方程上(,,,2,1n k k i ++=),得到如下的同解方程组⎪⎪⎪⎪⎪⎩⎪⎪⎪⎪⎪⎨⎧=++=++=++=++=++++++++++++++++)1()1(1)1(1,)1(1)1(,11)1(1,1)()()()2(2)2(22)2(22)1(1)1(12)1(121)1(11............................................................................k n n k nn k k k n k k n k n k k k k k k k n k kn k k kk n n n n b x a x a b x a x a b x a x a b x a x a b x a x a x a 其中(1)()()(1)()(),.1,,,,1,,k k k ij ij ik kj k k k ii ik k a a l a i j k n b b l b i k n++⎧=-=+⎪⎨=-=+⎪⎩ 按上述做法,做完n-1步,原方程组化为同解的上三角形方程组⎪⎪⎪⎪⎩⎪⎪⎪⎪⎨⎧==++=++=+++)()()()()()2(2)2(22)2(22)1(1)1(12)1(121)1(11....................................................................n n n n nn k k n k kn k k kk n n n n b x a b x a x a b x a x a b x a x a x a 最后,设0)(≠n nna ,逐步代回得原方程组的解()()()()1/,()/(1,2,,2,1).n n n n nn nk k kk k kj j kkj k x b a x b a x a k n n =+⎧=⎪⎪=-⎨⎪⎪=--⎩∑注意:上述公式中)()(,k i k ij b a 的上标k ,是用来区别消去过程中第k 步利用的量。