資源描述:
《fortran經(jīng)典入門程序20例》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫。
1、對于FORTRAN的初學(xué)者。這些例子可作為小練習(xí)。1.例題:計算工資問題。每小時工資為RATE,如果工作超過40小時,加班呢部分工資是正常時間工資的1.5倍。CPayrollwithovertimeprogrampayrollrealrate,hours,payread(*,*)rate,hoursif(hours>40)thenpay=40*rate+(hours-40)*1.5*rateelsepay=hours*rateENDIFprint*,"rate=",rateprint*,"hou
2、rs=",hoursprint*,"pay=",payend2.學(xué)生成績問題。大于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"E
3、NDIFENDIFEND3.三個數(shù)按從小到大排序。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,"=>A"EL
4、SEIF(GRADE.GE.70.0)THENPRINT*,GRADE,"=>B"ELSEIF(GARDE.GE.60.0)THENPRINT*,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!pro
5、gramex1FACT=1do10,N=1,5,1##DO語句標(biāo)號,循環(huán)不變量=初值,終值,步長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=T
6、ERM*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+
7、Z.EQ.300)PRINT*,X,Y,ZENDIF30CONTINUE20CONTINUE10CONTINUEEND10.DO循環(huán)用來處理循環(huán)次數(shù)確定的情況比較方便。在處理無法事先知道循環(huán)次數(shù)時,使用當(dāng)型循環(huán)。(1)DOWHILEDO標(biāo)號,WHILE(邏輯表達(dá)式)循環(huán)體CONTINUE標(biāo)號例統(tǒng)計若干同學(xué)的得分并計算平均值。INTEGERSCORE,COUNTREAD*,SCORESUM=0COUNT=0DO10,WHILE(SCORE.GT.0.0)SUM=SUM+SCORECOUNT=COU
8、NT+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,NIF(M.LT.N)THENT=MM=NN=TENDIFR=MOD(M,N)DO10,WHILE(R.NE.0)M=NN=RR=MOD(M,N)10CONTINUEPRINT*,"zuidagongyu