FORTRAN经典入门程序20例

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

对于FORTRAN的初学者。这些例子可作为小练习。1.例题:计算工资问题。每小时工资为RATE,如果工作超过40小时,加班呢部分工资是正常时间工资的1.5倍。CPayrollwithovertimeprogrampayrollrealrate,hours,payread(*,*)rate,hoursif(hours40)thenpay=40*rate+(hours-40)*1.5*rateelsepay=hours*rateENDIFprint*,rate=,rateprint*,hours=,hoursprint*,pay=,payend2.学生成绩问题。大于80为A级。大于60小于80为B级。小于60为C级。IF的嵌套。注意空格可以看清楚elseif,endif,print的内容.PROGRAMGRADESTUDENTREAD*,GRADEIF(GRADE.GE.80)THENPRINT*,GRADE,GRADE=AELSEIF(GRADE.LT.60)THENPRINT*,GRADE,GRADE=CELSEPRINT*,GRADE,GRADE=BENDIFENDIFEND3.三个数按从小到大排序。PROGRAMMAXMINREALA,B,C,TREAD*,A,B,CIF(A.GT.B)THENT=AA=BB=TELSEENDIFIF(B.GT.C)THENT=BB=CC=TELSEENDIFIF(A.GT.B)THENT=AA=BB=TENDIFPRINT*,A,B,CEND4.运用EISEIF语句。重做例子2PROGRAMex2READ(*,*)GRADEIF(GRADE.GE.80.0)THENPRINT*,GRADE,=AELSEIF(GRADE.GE.70.0)THENPRINT*,GRADE,=BELSEIF(GARDE.GE.60.0)THENPRINT*,GRADE,=CELSEPRINT*,GARDE,=DENDIFEND5.计算236,x0y28,0xxxxPROGRAMEQUATIONREAD(*,*)XIF(X.GE.0.0)Y=3*X+6IF(X.LT.0.0)Y=-X**2+2*X-8PRINT*,X=,X,Y=,YEND6.CONTINUE语句。为了使循环的起止范围清晰,使用终端语句例:求5!programex1FACT=1do10,N=1,5,1##DO语句标号,循环不变量=初值,终值,步长FACT=FACT*N10continueprint*,FACTend求1!+2!+3!+···+N!READ*,NFACT=1sum=0.0do10,I=1,N,1FACT=FACT*ISUM=SUM+FACT10continueprint*,SUMend7.求E^X=1+X+X^2/2!+X^3/3!+...+X^N/N!READ*,X,Nsum=1TERM=1do10,I=1,N,1TERM=TERM*X/ISUM=SUM+TERM10continueprint*,EXP(,X,),SUMend8.打印九九乘法表do10,I=1,9,1do20,J=1,IPRINT*,I,*,J,=,I*J20CONTINUE10CONTINUEend9.百钱买百鸡公鸡5元每只。母鸡3元每只,小鸡1元三只。求100元买一百只鸡。INTEGERX,Y,ZDO10,X=0,100DO20,Y=0,100DO30,Z=0,100IF(X+Y+Z.EQ.100)THENIF(15*X+9*Y+Z.EQ.300)PRINT*,X,Y,ZENDIF30CONTINUE20CONTINUE10CONTINUEEND10.DO循环用来处理循环次数确定的情况比较方便。在处理无法事先知道循环次数时,使用当型循环。(1)DOWHILEDO标号,WHILE(逻辑表达式)循环体CONTINUE标号例统计若干同学的得分并计算平均值。INTEGERSCORE,COUNTREAD*,SCORESUM=0COUNT=0DO10,WHILE(SCORE.GT.0.0)SUM=SUM+SCORECOUNT=COUNT+1READ*,SCORE10CONTINUEAVER=SUM/COUNTPRINT*,AVEREND11.辗转相除法求最大公约数。如26和6的最大公约数。先用27除以6,余数是3,再用6除以3,余数为0,则3就是最大公约数。命令MOD()求余数。INTEGERM,N,R,TREAD*,M,NIF(M.LT.N)THENT=MM=NN=TENDIFR=MOD(M,N)DO10,WHILE(R.NE.0)M=NN=RR=MOD(M,N)10CONTINUEPRINT*,zuidagongyueshu,NEND12.判断一个数是否是素数。SQRT()开根号READ*,NMAX=sqrt(real(N))I=2DO10,WHILE(I.LE.MAX.AND.MOD(N,I).NE.0)I=I+110CONTINUEIF(I.GT.MAX)THENPRINT*,N,ISAPRIMENUMBERELSEPRINT*,N,ISNOTAPRIMENUMVERENDIFEND13.求100和200之间能被3或7整除的自然数。I=10010IF(I.LE.200)THENIF(MOD(I,3).EQ.0.OR.MOD(I,7).EQ.0)THENPRINT*,I,ABLEELSEPRINT*,I,UNABLEENDIFI=I+1GOTO10ENDIFEND14.直到型循环,UNTIL语句实现直到型循环。格式“DOS1,UNTIL(条件)循环体标号S1CONTINUE例14用直到型循环求5!FORTRAN不支持UNTIL语句。INTEGERI,PRODUCTI=1PRODUCT=1DO10,UNTIL(I.GT.5)PRODUCT=PRODUCT*II=I+110CONTINUEPRINT*,5!=,PRODUCTENDFORTRAN不支持UNTIL语句。让我们改成DOWHILE格式。INTEGERI,PRODUCTI=1PRODUCT=1DO10,WHILE(I.LT.5)PRODUCT=PRODUCT*II=I+110CONTINUEPRINT*,5!=,PRODUCTEND15.INTEGERR,I,J,S,T,SUMDO10,I=100,999,1T=ISUM=0DO20,J=1,3,1R=MOD(T,10)SUM=SUM+R**3T=T/1020CONTINUEIF(SUM.EQ.I)PRINT*,I,isadaffodilNUMBER10CONTINUEEND15.求一元二次方程的根20AxBxC。DOUBLEPRECISIONA,B,CCOMPLEXX1,X2READ*,A,B,CIF(B**2-4*A*C.GE.0)THENX1=CMPLX((-B+SQRT(B**2-4*A*C))/(2*A),0.0)X2=CMPLX((-B-SQRT(B**2-4*A*C))/(2*A),0.0)ELSEX1=CMPLX((-B/(2*A)),SQRT(-(B**2-4*A*C))/(2*A))X2=CMPLX((-B/(2*A)),-SQRT(-(B**2-4*A*C))/(2*A))ENDIFPRINT*,X1,X2END16.打印此图像。CHARACTER*9LINEDO10,I=-4,4,1K=1DO40J=1,ABS(I),1LINE(K:K)=K=K+140CONTINUEDO20,J=1,2*(4-ABS(I))+1,1LINE(K:K)=*K=K+120CONTINUEPRINT*,LINEDO30,J=1,9,1LINE(J:J)=30CONTINUE10CONTINUEEND17.输出数组中的数值。INTEGERA(1:10)DO10,I=1,10,1READ*,A(I)10CONTINUEPRINT*,AEND18.利用DO循环对数组进行输入和输出。从键盘上输入十个数,按顺序逐个放入到A(1)到A(10)中。INTEGERA(1:10)DO10,I=1,10,1READ*,A(I)10CONTINUEDO20,I=1,9,2PRINT*,A(,I,),A(I)20CONTINUEEND19.双重循环将从终端读入的数据赋值给W数组中的各元素。REALW(1:3,1:2)##表示一个三行两列的数组,当前面数一样时,可以写出W(3,2)DO10,I=1,3,1DO20,J=1,2,1READ*,W(I,J)20CONTINUE10CONTINUEPRINT*,WEND20。输入10名学生的成绩,统计各个分数段的人数。INTEGERGRADE(1:10),COUNTER(0:10)READ*,(GRADE(I),I=1,10,1)DO10,I=1,10,1COUNTER(GRADE(I)/10)=COUNTER(GRADE(I)/10)+110CONTINUEDO20,I=0,10,1PRINT*,COUNTER(I),=,I*10,(I+1)*10-120CONTINUEEND21.顺序查找。一组数据从第一个开始找,折半查找,顺序查找适用数据少的情况,折半查找用于数据多的情况,但数据必须按顺序排列。INTEGERA(1:7)DATAA/55,62,10,58,22,33,66/READ*,XDO10,I=1,7,1IF(A(I).EQ.X)GOTO2010CONTINUE20IF(I7)THENPRINT*,X,itisnothereELSEPRINT*,X,itisintheAthe,I,positionENDIFEND

1 / 8
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功