資源描述:
《FORTRAN經(jīng)典入門程序20例.docx》由會員上傳分享,免費在線閱讀,更多相關內(nèi)容在行業(yè)資料-天天文庫。
1、對于FORTRAN的初學者。這些例子可作為小練習。1.例題:計算工資問題。每小時工資為RATE,如果工作超過40小時,加班呢部分工資是正常時間工資的1.5倍。CPayrollwithovertimeprogrampayrollrealrate,hours,payread(*,*)rate,hoursif(hours>40)thenpay=40*rate+(hours-40)*1.5*rateelsepay=hours*rateENDIFprint*,"rate=",rateprint*,"hours=",hoursprint*,"pay
2、=",payend2.學生成績問題。大于80為A級。大于60小于80為B級。小于60為C級。IF的嵌套。注意空格可以看清楚elseif,endif,print的內(nèi)容.PROGRAMGRADESTUDENTREAD*,GRADEIF(GRADE.GE.80)THENPRINT*,GRADE,"GRADE=>A"ELSEIF(GRADE.LT.60)THENPRINT*,GRADE,"GRADE=>C"ELSEPRINT*,GRADE,"GRADE=>B"ENDIFENDIFEND3.三個數(shù)按從小到大排序。PROGRAMMAXMINREAL
3、A,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,"=>A"ELSEIF(GRADE.GE.70.0)THENPRINT*,GRADE,"=>B"ELSEIF(GARDE.GE.60.0)T
4、HENPRINT*,GRADE,"=>C"ELSEPRINT*,GARDE,"=>D"ENDIFEND5.計算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語句。為了使循環(huán)的起止范圍清晰,使用終端語句例:求5!programex1FACT=1do10,N=1,5,1##DO語句標號,循環(huán)不變量=初值,終值,步長FACT=FACT*N10continueprint*,FACTend求
5、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,"=",
6、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循環(huán)用來處理循環(huán)次數(shù)確定的情況比較方便。在處理無法事先知道循環(huán)次數(shù)時,使用當型循環(huán)。(1)DOWHILEDO標號,WHILE
7、(邏輯表達式)循環(huán)體CONTINUE標號例統(tǒng)計若干同學的得分并計算平均值。INTEGERSCORE,COUNTREAD*,SCORESUM=0COUNT=0DO10,WHILE(SCORE.GT.0.0)SUM=SUM+SCORECOUNT=COUNT+1READ*,SCORE10CONTINUEAVER=SUM/COUNTPRINT*,AVEREND11.輾轉(zhuǎn)相除法求最大公約數(shù)。如26和6的最大公約數(shù)。先用27除以6,余數(shù)是3,再用6除以3,余數(shù)為0,則3就是最大公約數(shù)。命令MOD()求余數(shù)。INTEGERM,N,R,TREAD*,M
8、,NIF(M.LT.N)THENT=MM=NN=TENDIFR=MOD(M,N)DO10,WHILE(R.NE.0)M=NN=RR=MOD(M,N)10CONTINUEPRINT*,"zuidagongyu