Fortran作业2

合集下载

fortran编程习题答案

fortran编程习题答案

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程序,计算并输出一个给定整数的所有因子。

fortran作业

fortran作业

fortran作业第三章二、将数学式写成fortran表达式(1)a**2+4*b**3)/(a-b)(2)(-b+sqrt(b*b-4*a*c))/(2*a)(3)(6*sin((x+y)**2))/(2*a)(4)sin(y/(aqrt(x*x+y*y)))(5)sin(atan(aqrt(x*x+y*y))/(abs(c)))九、电路编程implicitreal(a-z)r1=30r2=60r3=45u=120i=(u/r1+u/r2+u/r3)print*,iend结果:8.666667十、六边形面积编程!mainprogramimplicitreal(a-z)i1=10i2=20i3=16i4=13i5=21i6=14i7=30i8=36i9=28area=x(i2,i3,i7)+x(i1,i7,i8)+x(i4,i8,i9)+x(i5,i6,i9)print*,'area=',areaend!functionprogramfunctionx(a1,a2,a3)b=(a1+a2+a3)/2x=sqrt(b*(b-a1)*(b-a2)*(b-a3))end结果:area=581.2570十一、分期付款编程read(*,*)d,p,rm=(log(p)-log(p-d*r))/log(1+r)m=m+1print*,mend十二、筹钱贷款编程!daikuanrealiread(*,*)a,r,ni=r+1d=(a*(i**n-1))/((i-1)*i**n)d=int(d*10+0.5)/10print*,dend第四章五、谋y编程!calculateyrealx,yread(*,*)xif(x.ge.0.and.x.lt.10)theny=xelseif(x.ge.10.and. x.lt.20)theny=x*x+1elseif(x.ge.20.and.x.lt.30)theny=x*x*x+x*x+1endifprint*,'x=',x,'y=',yend六、整除编程!zhengchuread(*,*)mif(mod(m,7).eq.0)print*,m,'能被7整除'if(mod(m,11).eq.0)print*,m,'能被11整除'if(mod(m,17).eq.0)print*,m,'能够被17相乘'if(mod(m,7).ne.0.and.mod(m,11).ne.0.and.mod(m,17).ne.0)print*,m,'无法被7,11,17相乘'end八、大小排序编程!programread(*,*)a,b,c,dif(a.lt.b)thentemp=aa=bb=tempendifif(a.lt.c)thentemp=aa=cc=tem pendifif(a.lt.d)thentemp=aa=dd=tempendifif(b.lt.c)thentemp=bb=cc=tempendifif(b.lt.d)thentemp=bb=dd=tempendifif(c.lt.d) thentemp=cc=dd=tempendifprint*,a,b,c,dend九、高程编程!programread(*,*)x,yif((x-2)**2+(y-2)**2.le.1)thenh=10elseif((x+2)**2+(y+2)**2.le.1)thenh=10elseif((x-2)**2+(y+2)**2.le.1)thenh=10elseif((x+2)**2+(y-2)**2.le.1)thenh=10elseh=0endifprint*,'h=',hend十、建筑规划编程!programread(*,*)x,yif(abs(x).le.10.and.abs(y).le.10)thenh=20elseif(abs(x).l e.20.and.abs(y).le.20)thenh=30elseif(abs(x).le.30.and.abs(y).le.30)thenh=50elseh=100endifprint*,'h=',hend一、求和编程!programsum=0sign=1do100,i=1,100,1sum=sum+sign*1.0/isign=sign*(-1)100continueprint*,'sum=',sumend结果为:0.6881718二、求和编程!programsum=0do100,i=1,20,1第五章sum=sum+1.0/i/(i+1)100continueprint*,'sum=',sumend结果为:0.9900995四、sinx函数编程!programsum=0read(*,*)xtemp=xsign=1i=1do100,while(abs(temp).gt.1e-10)sum=sum+sign*tempsign=sign*(-1)i=i+2temp=temp*x*x/i/(i-1)100continueprint*,sumend五、电阻编程!programread(*,*)r0,r1,r2temp=r0do100,i=1,3,1temp=(temp+r1)*r2/(temp+r1+r2)1 00continueprint*,tempend七、水解因子编程!programread(*,*)mprint*,m,'=1'20continuedo100,i=2,m,1if(mod(m,i).eq.0)thenprint*,'*',im=m/igoto20endif100continue。

Fortran作业、期末复习、西南交大

Fortran作业、期末复习、西南交大

编写两个子程序,分别求两个整数的最大公约数和最小公倍数,并编写主程序调用这两个子程序,输出结果program xinteger m,n,swrite(*,*)"请输入两个数:"read(*,*)m,ns=m*ncall acb(n,m)call asd(n,m,s)endsubroutine acb(n,m)integer tempif(m<n)thentemp=m;m=n;n=tempend ifdo while(mod(m,n)/=0)temp=n;n=mod(m,n);m=tempend dowrite(*,"('最大公约数为:'i4)") nreturnendsubroutine asd(n,m,s)if(m<n)thentemp=m;m=n;n=tempend ifdo while(mod(m,n)/=0)temp=n;n=mod(m,n);m=tempend dowrite(*,"('最小公倍数为:'i4)") s/nreturnend编写一个递归子程序,求Fibonacci数列某项的值。

并编写主程序调用该递归子程序,输出结果。

program xinteger n,tempwrite(*,*)"输入Fibonacci数列某项数,n大于三"read(*,*)ncall abc(n)endsubroutine abc(n)integer i,j,temp,a(n)a(1)=1a(2)=1do i=3,na(i)=a(i-1)+a(i-2)end dotemp=a(n)write(*,"('Fibonacci数列n项的值为:',i8)")tempreturnend1:输入10名学生的学号和一门课程的考试成绩,分别存入顺序文件和直接文件中。

fortran作业

fortran作业

1、反射波走时计算:program fansheboimplicit nonereal t,sum,t1,p,xinteger i,nreal,allocatable,dimension(:)::v,h !! Define arrays typewrite(*,*)'imput Model strata n'read(*,*) nallocate(v(n),h(n)) !!For dynamic array allocate memory write(*,102)'please imput',n,' level of velocity value'read(*,*)(v(i),i=1,n)write(*,102)'please imput ',n,' level of height value'read(*,*)(h(i),i=1,n)write(*,*)'please imput shot interval x'read(*,*) xsum=0.0call tp(x,p,n,v,h) !!carry out intrinsic procedure!! calculate the time Tdo i=1,nt1=h(i)/(v(i)*sqrt(1-(p**2)*(v(i)**2)))sum=sum+t1end dot=2*sum!!output the time Twrite(*,101) 'Light through', n ,'layer needs time:'write(*,100)t,'s'100 format(1x,f10.4,1x,A)101 format(1x,A,2x,i2,2x,A)102 format(1x,A,1x,i2,1x,A)deallocate(v,h)containssubroutine tp(x1,p1,n1,v1,h1) !! Define virtual parametersimplicit nonereal sum1,x1,x2,Y,y1,p1,p2,e,a,b,piinteger i,m,n1real,dimension(n1)::v1,h1 !!Difine virtual parameters arrayspi=3.1415927m=0b=5.0a=10.0do while(m<=1000) !!The most cycle 1000 times!! calculate Equation solutione=sin(pi*(a/180.0))p2=e/real(v(1))sum1=0.0do i=1,n1if(1-(p2**2)*(v1(i)**2)>0)theny1=(h1(i)*v1(i)*p2)/sqrt(1-(p2**2)*(v1(i)**2))sum1=sum1+y1else !!Gave the initial boundary value too big,reduce the initiala=a/2.0b=b/2.0end ifend dox2=2*sum1Y=x1-x2if(abs(Y)<=1.0e-6)then !!Get equation solution ,Jump out of circulationexitelseif(Y>0)thena=a+belsea=a-b/2.0 !! Stepping too big,reduce the stepb=b/2.0end ifend ifm=m+1end dop1=p2 !! return the resule p2 to p1write(*,103)'p=',p1 !! output p1103 format(1x,A,2x,f6.4)end subroutine tpend二、编写矩阵相乘的外部子程序,用于对任意大小的两个矩阵进行相乘。

二级FORTRAN程序设计上机题集

二级FORTRAN程序设计上机题集

二级FORTRAN程序设计上机题集上机题1======================================================================试题说明 :======================================================================补充编制fortran77程序prog1.for,其功能是计算并输出级数和1 1 (-1)^ns(n)=x - ─x^3 + ─x^5 - ... + ── x^(2n+1)3 5 2n+1直到s(n)-s(n-1)<0.000001为止。

其中x=0.7。

请编写该程序,最后运行程序。

其中部分程序与输出子程序writedat在程序中已经给出,不得修改。

======================================================================程序 :======================================================================write(*,100) s100 format(1x,'s=',e12.4)write(*,*)call writedat(s)endsubroutine writedat(s)open(10,file='bc01.out',status='new')write(10,100) s100 format(1x,e12.4)end======================================================================所需数据 :======================================================================@3 $bc01.out 001|.6107e+00#e上机题2=============================================================================== 试题说明 :补充编制fortran77程序prog1.for,其功能是计算并输出级数和1 1 1s(n)=1 + x + ─x^2 + ─x^3 +... + ─x^n2! 3! n!直到s(n)-s(n-1)<0.000001为止。

1998~2002年计算机考试二级FORTRAN(含答案)

1998~2002年计算机考试二级FORTRAN(含答案)

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)

北航数值分析大作业第二题(fortran)

!计算A(r+1) DO I=1,N DO J=1,N A(I,J)=A(I,J)-W(I)*U(J)-U(I)*P(J) ENDDO ENDDO ENDIF ENDDO RETURN END
!***************符号函数子程序*****************! FUNCTION SGN(X) REAL(8) X IF(X>0) THEN SGN=1 ELSE IF(X<0) THEN SGN=-1 ELSE IF(X==0) THEN SGN=0 ENDIF END
DIMENSION A(N,N),A1(N,N),A2(N,N),C(2,N),Q(N,N),R(N,N),CR(N),CM(N)!C为存储特征值的数 组,1为实部,为虚部 REAL(8) A,A1,A2,C,Q,R,CM E=1E-12 L=1000 !精度水平 !迭代最大次数
OPEN(1,FILE='数值分析大作业第二题计算结果.TXT') DO I=1,N DO J=1,N IF(I==J) THEN A(I,J)=1.52*COS(I+1.2*J) ELSE A(I,J)=SIN(0.5*I+0.2*J) ENDIF ENDDO ENDDO A1=A WRITE(*,"('矩阵A为:')") WRITE(1,"('矩阵A为:')") DO I=1,N DO J=1,N WRITE(*,"(2X,E20.13,2X,\)") A(I,J) WRITE(1,"(2X,E20.13,2X,\)") A(I,J) ENDDO WRITE(*,"(' ')") WRITE(1,"(' ')") ENDDO !使用矩阵的拟上三角化的算法将矩阵A化为拟上三角矩阵A(n-1) CALL HESSENBERG(A,N) WRITE(*,"('拟上三角化后矩阵A(n-1)为:')") WRITE(1,"('拟上三角化后矩阵A(n-1)为:')") DO I=1,N DO J=1,N WRITE(*,"(2X,E20.13,2X,\)") A(I,J) WRITE(1,"(2X,E20.13,2X,\)") A(I,J) ENDDO WRITE(*,"('')") WRITE(1,"('')") ENDDO !计算对矩阵A(n-1)实行QR方法迭代结束后所得矩阵 A2=A CALL QRD(A2,N,Q,R)

fortran课后习题

fortran课后习题

5.3 program main implicit none integer nian,shouru real suijin read(*,*) nian read(*,*) shouru if (nian<50.and.nian>0) then if (shouru>=1000 .and. shouru<=5000) then suijin=shouru*0.1 end if if (shouru>5000) then suijin=shouru*0.15 end if if (shouru<1000) then suijin=shouru*0.03 end if else if (nian>50) then if (shouru>=1000 .and. shouru<=5000) then suijin=shouru*0.07 end if if (shouru>5000) then suijin=shouru*0.1 end if if (shouru<1000) then suijin=shouru*0.05 end if end if write(*,*)"应缴纳的税金金额为:" write(*,*) suijin end 5.4 program main implicit none integer day write(*,*) "请输入一个公元的年份:" read(*,*) day if (mod(day,4)==0) then
write(*,*)"一年有366天" else if(mod(day,100)==0.and.mod(day,400)==0) then write(*,*)"一年有366天" else write(*,*)"一年有365天" end if End 6.1 method one: program main implicit none integer counter do counter=1,5 write(*,*)"Fortran" end do end method two: program main implicit none integer counter counter=1 do while(counter<=5) write(*,*)"Fortran" counter=counter+1 end do end 6.2 program main implicit none integer counter integer::ans=0 counter=1 do while(counter<100) ans=ans+counter counter=counter+2 end do

Fortran2

Fortran2

implicit 语 句 ( 隐 含 说 明 语 句 ) : 可 以 用 implicit语句将某一字母开头的全部变量指定为 implicit 语句将某一字母开头的全部变量指定为 所需类型。 所需类型。
IMPLICIT IMPLICIT INTEGER (A, C, T-V) INTEGER (A-C), REAL (I, K) (ALOGO
write?vc?vc?kmh?write?ve?ve?kmh?write?time??minutes?endlogo21fortran21fortran常量常量22fortran22fortran变量变量2323内部函数内部函数2424算术表达式算术表达式2525赋值语句赋值语句2626简单的输入输出语句简单的输入输出语句2727程序执行控制语句程序执行控制语句2828程序举例程序举例
LOGO
2.4 Fortran算术表达式 算术表达式
一些正误比较
LOGO
2.4 Fortran算术表达式 算术表达式
表达式运算中的类型问题: 表达式运算中的类型问题:
77允许不同类型的算术量 包括整型、 允许不同类型的算术量( FORTRAN 77允许不同类型的算术量(包括整型、 实型、双精度、复型)间的算术运算, 实型、双精度、复型)间的算术运算,但不允许 在算术量和非算术量(如逻辑型、字符型) 在算术量和非算术量(如逻辑型、字符型)之间 进行算术运算。 进行算术运算。 同类型的算术量之间运算的结果仍保持原类型, 同类型的算术量之间运算的结果仍保持原类型, 5/2=2、1/2=0。不同类型时, 如 5/2=2、1/2=0。不同类型时,编译系统遵循 低级转换成高级” 实型比整型高) “低级转换成高级”(实型比整型高)原则将它 们转换成同一类型,然后进行运算。 们转换成同一类型,然后进行运算。 类型的转换是从左而右进行的, 类型的转换是从左而右进行的,在遇到不同类型 的算术量时才进行转换。 的算术量时才进行转换。如:1/4*20.0=0.0

Fortran90_2

Fortran90_2

回顾机器人看门…... 回顾机器人看门
机器人的判断逻辑是: 机器人的判断逻辑是: 人: 口令 if_let_you_enter: & if( you == ‘human’ ) then if( passwd = ‘…’ ) then write(*,*) ‘You can enter!’ else write(*,*) “You cann’t enter!” endif endif if_let_you_enter
PROGRAM grade ! --- to judge grade based on given score ! implicit none ! real :: score1 integer :: score2 character(len=12), dimension(3) :: grade ! score1 = 76.5 score2 = nint(score1) grade(1) = ‘pass_failed’ grade(2) = ‘passed’ grade(3) = ‘excellent’
! --- judge leap year if( mod(year,4) == 0 ) then month_day(2) = 29 if( mod(year,100) == 0 ) then 理论上 month_day(2) = 28 可以无限嵌套! endif else month_day(2) = 28 endif 有个小错误! 有个小错误! ! ! --- write out month_day(:) write(*, ‘(a, i4, a,12i3)’) & ‘Month day of year ’, year, ‘:’, month_day(:) ! END PROGRAM judge_month_day

FORTRAN上机作业

FORTRAN上机作业

FORTRAN上机作业第一次上机作业:1-1、编写程序计算3+5(一)完整程序integer a,b,sread*,a,bs=a+bprint*,'s=',send(二)输入数据3 5(三)输出结果s= 81-2、编写1+x+((x^2)/2!)+((x^3)/3!). x=5.0,输出实型与整型(一)完整程序real xf=1.0y=1.0read*,xdo i=1,3f=f*x/iy=y+fenddoprint*,'y=',yend(二)输入数据5.0(三)输出结果、y=39.33334如果输出是整型:输出结果y=381-3. 若c=0, 100, -40 . 求F=(9*c)/5+32 (一)完整程序integer c,Fread*,cF=(9*c)/5+32print*,'F=',Fend(二)输入数据0(100或者-40)(三)输出结果F= 32(212或者-40)第二次上机作业2-1.编写程序,能够对于输入的五个数进行判断,输出绝对值最大值和最小的数。

(一)完整程序integer x1,x2,x3,x4,x5,m,nm=x1n=x1read*,x1,x2,x3,x4,x5if(abs(x2)>abs(m)) thenm=x2elseif(abs(x3)>abs(m)) thenm=x3elseif(abs(x4)>abs(m)) thenm=x4elseif(abs(x5)>abs(m)) thenm=x5elseif(abs(x2)<="">n=x2elseif(abs(x3)<="">elseif(abs(x4)<="">n=x4elseif(abs(x5)<="">n=x5endifprint*,'绝对值最大的数是',m,'绝对值最小的数是',nend(二)输入数据50 -90 30 -60 0(三)输出结果绝对值最大的数是-90 绝对值最小的数是02-2.y=e^(3.14/2*X)+ln(abs((sin)^2x-(sinx)^2)),x=sqrt((1+tan52.25)*3.14/180) 求y.(一)完整程序real x,yx=sqrt(1+tan(52.25*3.14/180))y=exp(3.14/2*x)+log(abs(sin(x)*sin(x)-sin(x*x)))print*,'y=',yend(二)输出结果y=9.3533722-3.编程判断两位整数M是否为守形数。

FORTRAN2

FORTRAN2

六、FORTRAN过程1、语句函数:定义:语句函数名( [形参变量名1][,形参变量名2]…)=表达式引用:语句函数名( [形参变量名1][,形参变量名2]…)REAL F,X; F(X)=3*X**2+2; READ *,X;PRITN *,F(X)见YUJUHANSHU2、函数子程序(FUNCTION):定义:[类型说明] FUNCTION 函数名([形参列表] ) [RESULT (函数结果名)] 说明语句执行语句END [ FUNCTION [函数名] ]调用:函数名([实参列表])<1主程序中必须对函数子程序名进行变量定义,子程序中定义与否均可;<2函数返回值默认保存在函数名的变量里,如果返回值在其他变量中,则需用RESULT声明(但仍需在主程序中对函数子程序名进行变量定义,而对返回变量定义与否均可);见FUNCTION3、子例子程序:定义:SUBROUTINE子例行程序名([形参列表变量])说明语句执行语句END [SUBROUTINE [子例行程序名] ]调用:CALL 子例行程序名(实参列表)见ZILI ZICHENGXU<1函数子程序只能返回一个函数值或者数组;而子例子程序能求多个值或不求值(子例子程序应用更广泛);<2值传递:常量、表达式;址传递:变量、数组;<3跳出子例子程序:RETURN跳出主程序:GOTO 100 ; 100 END<4传递参数为常量时,应该在子程序中定义为变量;<5函数子程序或者子例子程序中,写说明语句时,要对形参进行说明;4、特殊参数属性:①INTENT属性:指定形参的INTENT属性;格式:类型语句,INTENT (格式说明符)::形参列表INTEGER ,INTENT (IN) :: A<1格式说明符:IN,OUT,INOUT。

IN说明形参仅用于向子程序中传入数据,在子程序中不允许被修改;OUT输出;INOUT 输入输出;<2 INTENT仅对形参有效,只能出现在子程序或接口中,其实该属性无太多应用。

FORTRAN90考试笔试题2Word版

FORTRAN90考试笔试题2Word版

FORTRAN90语言部分一、 语言基础单项选择(每题2分,共20分)(一)下面的函数计算中,错误的是 1 。

A) SQRT(9) B) REAL(9)C) ABS(9) D) MOD(9,2)(二)运行下面的程序时(P,W,X,Y 都是实型变量), READ(*,10) P,W,X,Y WRITE(*,20)X,Y10 FORMAT(1X,F3.1,3(1X,F4.2)) 20 FORMAT(1X,F4.1,3(1X,F5.2)) END如果从键盘输入数据:12345678901234567890,则输出结果为 2 。

A) 1.23 56.78 B) 12.3 67.89 C) 12.3 45.67 D) 34.5 67.89(三)数学式a acb x 24sin 22-+的FORTRAN 表达式为 3 。

A) SIN(X**2)+SQRT(B*B-4*A*C)/2/A B) SIN(X)**2+ SQRT(B*B-4*A*C)/2/AC) (SIN(X**2)+SQRT(B*B-4*A*C))/(2*A) D) (SIN(X)**2+SQRT(B*B-4*A*C))/(2*A)(四)表示条件“A 加B 大于C 并且C 大于等于D ”的FORTRAN90逻辑表达式是 4 。

A) A+B>C >=D B) (A+B)>C >=D C) A+B>C.AND. C>=DD) (A+B)>C.OR. C>=D(五)通过下面的赋值,字符变量LINE 的值是 5 。

表示空格。

CHARACTER(LEN=4)::LINE LINE=’10’ A) 10 B) 1000 C) ‘10’ D) ’10 ’ (六)下面对二维数组的说明语句中,正确的语句是 6 。

A) 显式形状数组的说明:REAL,DIMENSION(10,:) ::AB) 动态数组的说明方式:REAL,DIMENSION(:,:), ALLOCATE ::A C) 假定形状数组的说明:REAL,DIMENSION(10,:) ::A D) 指针数组的说明:POINTER,DIMENSION(1:3,2:4) ::A (七)下面的模块中,错误的语句是 7 。

北京邮电大学出版社FORTRAN程序设计课后习题答案2

北京邮电大学出版社FORTRAN程序设计课后习题答案2

北京邮电大学出版社FORTRAN程序设计课后习题答案2 PROGRAM EX3_4CHARACTER*1 LETTER!方法一print*,' 请输入一个英文字母:'READ*,LETTERif(LETTER>='A' .and. LETTER<='Z') THENwrite(*,*)' 你输入的',LETTER,'为大写字母。

'elseif(LETTER>='a' .and. LETTER<='z')THENwrite(*,*)' 你输入的',LETTER,'为小写字母。

'elsewrite(*,*)' 你输入的',LETTER,'不是字母。

'endif!方法二print*,' 请输入一个英文字母:'READ*,LETTERSELECT CASE(LETTER)CASE('A':'Z')write(*,*)' 你输入的',LETTER,'为大写字母。

'CASE('a':'z')write(*,*)' 你输入的',LETTER,'为小写字母。

'CASE DEFAULTwrite(*,*)' 你输入的',LETTER,'不是字母。

'ENDSELECT!方法三print*,' 请输入一个英文字母:'READ*,LETTERI=ICHAR(LETTER) !转化字符LETTER为对应的ASCII值if(i>=65 .and. i<=90) write(*,*)' 你输入的',LETTER,'为大写字母。

Fortran程序设计课后习题答案方便

Fortran程序设计课后习题答案方便

第四章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。

fortran习题参考答案

fortran习题参考答案

工程分析程序设计上机作业(五)模块化编程上机目的:掌握内部例程、主程序、外部例程、模块等功能的使用方法。

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
8 指 针 、格 式化 输入 /输 出、 文件 操作 8.1 格式化输入输出 . . . . . . . . . 8.1.1 整数 . . . . . . . . . . . 8.1.2 实数 . . . . . . . . . . . 8.1.3 复数 . . . . . . . . . . . 8.1.4 逻辑型 . . . . . . . . . . 8.1.5 字符串 . . . . . . . . . . 8.2 输出金字塔形状 . . . . . . . . . 8.3 整齐的杨辉三角形 . . . . . . . 8.4 龙格-库塔法求解微分方程 . . . 8.5 Shell排序 . . . . . . . . . . . . 9 参考文献 10 LICENSE

fortran课后习题答案解析

fortran课后习题答案解析

fortran课后习题答案解析第一章 FORTRAN程序设计基础第15页 1、21.简述程序设计的步骤。

“程序设计”:反映了利用计算机解决问题的全过程,通常要经过以下四个基本步骤:(1)分析问题,确定数学模型或方法;(2)设计算法,画出流程图;(3)选择编程工具,编写程序;(4)调试程序,分析输出结果。

2. 什么是算法?它有何特征?如何描述算法?解决问题的方法和步骤称为算法。

算法的五个特征:(1) 有穷性。

(2) 确定性。

(3) 有效性。

(4) 要有数据输入。

(5) 要有结果输出。

算法的描述有许多方法,常用的有:自然语言、一般流程图、N -S图等。

第二章顺序结构程序设计第29页 1、2、3、4、5、6、7、8、91.简述符号常量与变量的区别?符号常量在程序运行过程中其值不能改变。

变量在程序运行过程中其值可以改变。

2. 下列符号中为合法的FORTRAN 90标识符的有哪些?(1) A123B (2) M%10 (3) X_C2 (4) 5YZ(5) X+Y (6) F(X) (7) COS(X) (8) A.2(9) ‘A’ONE (10) U.S.S.R.(11) min*2 (12) PRINT3. 下列数据中哪一些是合法的FORTRAN常量?(1) 9,87 (2) .0 (3) 25.82(4) -356231(5) 3.57*E2 (6) 3.57E2.1 (7) 3.57E+2(8) 3,57E-24. 已知A=2,B=3,C=5(REAL);且I=2,J=3(INTEGER),求下列表达式的值:(1) A*B+C 表达式的值: 11 (2) A*(B+C) 表达式的值: 16(3) B/C*A 表达式的值: 1.2 (4) B/(C*A) 表达式的值: 0.3(5) A/I/J 表达式的值: 0.33 (6) I/J/A 表达式的值: 0(7) A*B**I/A**J*2 表达式的值: 4.5(8) C+(B/A)**3/B*2. 表达式的值: 7.25(9) A**B**I 表达式的值: 5125. 将下列数学表达式写成相应的FORTRAN表达式:(1) 1E-2 (2)(-B+SQRT(B*B-4*A*C)/(2*A)(3) 1+X+X*X/2+X**3/2/3(4) COS(ATAN((A**3+B**3)**(1.0/3)/(C*C+1)))(5) EXP(A*X**2+B*X+C)(6) COS(X*Y/SQRT(X*X+Y*Y))**36. 用FORTRAN语句完成下列操作:(1) 将变量I的值增加1。

fortran2

fortran2

工程分析程序设计 上机作业(三)结构化编程——循环上机目的:练习使用选择结构编制程序。

1、 编写一个程序,判断一个整数是否素数。

program task1implicit noneinteger n,iprint*,'please input a number'read*,nif (n<2) thenprint*,'The number is not prime' !小于2的数不是素数end ifif (n==2) thenprint*,'The number is prime' ! 2是素数else ; i=2do while (i<=n/i)if (mod(n,i)==0) thenprint*,'The number is not prime'stopelse;i=i+1end ifend doprint*,'The number is prime' ! 判断大于2的数是否为素数 end if end2、 利用如下公式求π的近似值,直到最后1项的绝对值小于10-6: 1111 (4357)π=-+-+ program task2implicit noneinteger :: n=1real(8) :: ans=0do while (1.0/(2*n-1) > 1.0E-6 )ans=ans+(-1)**(n+1)*(1.0/(2*n-1))n=n+1end dowrite(*,"('pi = 4*(1-1/3+1/5-1/7+......) =',f8.5)") ans*4end3、输入一个自然数,进行因子分解并输出结果,例如24=1×2×2×2×3。

(输出格式不限)。

program task3implicit noneinteger m,n,iprint*,'请输入一个自然数:'read*,mprint*,'它的因子有:1 ' ! 1 一定是自然数的因子,直接输出10 do i=2,m,1if (mod (m,i)==0) thenwrite(*,20) im = m / i20 format (6x,I8)goto 10end ifend doEnd4、写一个程序,让用户输入一个内含空格符的字符串,然后使用循环把字符串中的空格符消除之后再重新输出。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

FORTRAN第二次实习作业
2014301610307 -D-马莉1 、根据最新个人所得税计算方法,如下面的个税税率表。

工资、薪金所得,适用七级超额累进税率,税率为百分之三(3% )至百分之四十五(45% )。

3500 元为起征点,即只需要缴纳高于3500 元部分的个税。

请根据下表编程,通过给定个人收入,计算其所需邀纳的个税。

program jiaoshui
implicit none
integer m
real n
character i
100 print*,'应缴纳所得税是否含税,请回答Y或N:'
read*,i
print*,'请输入工资所得:'
read *,m
if(i=='Y')then
select case(m)
case(0:1500)
n=m*3/100-0
case(1501:4500)
n=m*10/100-105
case(4501:9000)
n=m*20/100-555
case(9001:35000)
n=m*25/100-1005
case(35001:55000)
n=m*30/100-2775
case(55001:80000)
n=m*35/100-5505
case default
n=m*45/100-13505
end select
elseif(i==’N’)then
select case(m)
case(0:1455)
n=m*3/100-0
case(1456:4155)
n=m*10/100-105
case(4156:7755)
n=m*20/100-555
case(7756:27255)
n=m*25/100-1005
case(27256:41255)
n=m*30/100-2775
case(41256:57505)
n=m*35/100-5505
case default
n=m*45/100-13505
end select
else
print*,"输入格式不正确,请重新输入:"
goto 100
endif
print*,n
End
2、编写程序求某天在当年中是第多少天,例如2010 年1 月10 号是2010 年的第10 天。

要求分别使用IF 、CASE结构来编写。

program tianshu2
implicit none
integer M1(12),i,y,m,d,sum,n
data M1/31,28,31,30,31,30,31,31,30,31,30,31/
sum=0
100 print *,"请输入日期格式例如:2010 10 23"
read 10,y,m,d
10 format(i4,1x,i2,1x,i2)
if(m>12.or.d>31)then
print*,"输入日期格式不对,请重新输入:"
goto 100
else
do i=1,m-1
sum=sum+M1(i)
enddo
sum=sum+d
if ((mod(y,4)==0.and.mod(y,4)/=0).or.mod(y,400)==0)then
sum=sum+1
endif
print *,y,'年',m,'月',d,'日是当年的第',sum,'天'
endif
End
3.编写程序,采用二分法求如下函数的解X3-2x-5=0, x∈[2,3]
module constant
real::a=2,b=3,c,er=1e-5
end module
program erfenfa
use constant
implicit none
real f
do while(abs(a-b)>1e-6)
c=(a+b)/2
if (f(c)*f(a)<0) then
b=c
else
a=c
End if
End do
print *,"f(x)=X3-2x-5=0的解x=",c,",最终f(x)=",f(c)
end
real function f(x) !定义函数
real x
f=x**3-2*x-5
End
4 、编写程序,对于自然数m ,n ,求其最大公约数和最小公倍数。

program gongyueshu
implicit none
integer m,n,i,j
100 print*,'请输入两个自然数:'
read *,m,n
if(m>n)then
i=n
else
i=m
endif
do while(mod(m,i)/=0.or.mod(n,i)/=0)
i=i-1
end do
print *,m,'和',n,'最大公约数是:',i
if(m<n)then
j=n
else
j=m
endif
do while(mod(j,m)/=0.or.mod(j,n)/=0)
j=j+1
end do
print *,m,'和',n,'最小公倍数是:',j
goto 100
End。

相关文档
最新文档