fortran90例子_第1頁(yè)
fortran90例子_第2頁(yè)
fortran90例子_第3頁(yè)
fortran90例子_第4頁(yè)
fortran90例子_第5頁(yè)
已閱讀5頁(yè),還剩7頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、例1、 輸入M個(gè)實(shí)數(shù),將其相加,并輸出其和。PROGRAM example_1Implicit noneInteger :n,mReal :t=0,a=0Read *,mDo Read *, a T=t+aN=n+1If (n>=m) exitEnd doPrint*,tEnd program example_1例2、 求I!的階乘(I=4,8)。Function factor(n) result(fac_result) Implicit none Integer ,intent(in):n Integer,intent(out):fac_result Integer:I Fac_res

2、ult=1 Do I=1,nFac_result=fac_result*I End doEnd function factorProgram example_2 Implicit none Integer :factor,s=0,I Do I=4,8 S=s+factor(i) End doPrint*,sEnd program example_2例3、 輸入一個(gè)數(shù),判斷他是否能被3整除,并輸出相應(yīng)的信息。Program judge Implicit none Integer : n,m Read*,n M=mod(n,3) Select case(m) IF (M= =0) THEN Cas

3、e (0) Print*,yesPrint*,YES Case default ELSE Print*,noPrint*,NO End selectEND IF End program judge 例4、 判斷一個(gè)整數(shù)N是否為素?cái)?shù)PROGRAM prime Implicit none Integer :n,I,m Read*,n M= sqrt(real(n) Do I=2,mIf(mod(n,i)= =0) exit End do If (I>m) thenPrint*,yes ElsePrint*,no end ifend program prime例5、 求N的階乘PROGRAM

4、example_5Implicit noneInteger:n,I=0,fac=1Read*,nDo while (I<7) I=I+1 Fac=fac*IEnd doEnd program example_5例6、 求出全部的水仙花數(shù)。(水仙花數(shù)是個(gè)三位數(shù),其各位數(shù)字的立方和等于該數(shù)。)program example_6 implicit none integer :I,j,k,m,n ii: do I=1,9 jj: do j=1,9 kk: do k=1,9 m=I*100+j*10+k n=I*3+j*3+k*3 if (m= =n) print*,m end do kk end

5、 do jj end do iiend program exaple_6例7、 牛頓迭代法求方程X*4+4*X+1=0的根program example_7 implicit none integer :I=1,mreal:x0,x,eread*,x0,e,m (m控制迭代次數(shù))do x=(-x0*x0-1)/4 if (abs(x-x0)<=e) exit x0=x I=I+1 If (I>=m) then Print*,not Exit End if End do If (I<m) print*,I,x End program example_7例8、 將例2寫成接口塊的

6、形式 主程序: Program example_2 Implicit none Interface Function factor(n) result(factor_result) Integer ,intent(in):n Integer:factor_result End function factor End interface Implicit none Integer : s=0,I Do I=4,8 S=s+factor(i) End doPrint*,sEnd program example_2例9、 將例2函數(shù)子程序改寫成子例行子程序。Subroutine isum(n,isu

7、m_result)implicit noneinteger,intent(in):ninteger,intent(out):isum_resultinteger :Iisum_result=1do I=1,nisum_result=isum_result*Iend doend subroutine peogram example_9 implicit none integer:x,yread*,x call isum(x,y)print*,y=,yend program example_9例10、 子程序作為虛元(虛過(guò)程)PROGRAM example_10Implicit noneInter

8、face Function sum(x,y) result(sum_result) Integer,intent(in):x,y Integer,intent(out):sum_result End function sum Function minu(x,y) result(minu_result) Integer,intent(in):x,y Integer,intent(out):minu_result End function minu Integer :a,b Read*,a,bCall proc(a,b,sum)Call proc(a,b,minu)End program exam

9、ple_10Subroutine proc(a,b,fun) Implicit none Function fun(x,y) result (fun_result) Integer,intent(in):x,y Integer,intent(out):fun_result End function fun Integer ,intent(in):a,b Print*,fun(a,b)End subroutineFunction sum(x,y) result(sum_result) Implicit none Integer,intent(in):x,y Integer,intent(out)

10、:sum_result Sum_result=x+y End function sumFunction minu(x,y) result(minu_result) Implicit none Integer,intent(in):x,y Integer,intent(out):minu_result Minu_result=x-y End function minu例11、 模塊實(shí)現(xiàn)數(shù)據(jù)共享module exam_module implicit none real:a,b,cend module exam_modulefunction aver3() result (aver_result)

11、use exam_module real:aver_result aver_result=(a+b+c)/3end function aver3function max3() result(max_result) use exam_module real:max_result max_result=a if(b> max_result) max_result=b if (c> max_result) max_result=c end function max3program example_11 use Exam_module real :aver3,max3 read*,a,b,

12、c print*,aver3(),max3()end program example_11注意:(1)USE 模塊名,ONLY :實(shí)體名例: use exam_module,only:a,b,此時(shí)C不再是共享變量,故C仍需通過(guò)虛實(shí)結(jié)合。 (2)use exam_module ,x->a 將模塊中A與程序單元中變量X共享。例12、 遞歸recursive function fac(n) result(fac_result) implicit none Integer ,intent(in):n Integer,intent(out):fac_result If (n= =0) thenFa

13、c_result=1 Else Fac_result=fac(n-1)*n End ifEnd function facProgram example_12 Implicit none Interface Recursive function fac(n) result(fac_result) Integer ,intent(in):n Integer,intent(out):fac_resultEnd function facEnd interfaceInteger:nRead*,nPrint*,fac(n)End program example_12例13、 編一函數(shù),求兩數(shù)之和a) 用外部過(guò)程實(shí)現(xiàn)program example_131 implicit none integer:a,b,sum read*,a,b call add(a,b,sum) print*,sumend program example_131subroutine add(a,b,sum)implicit noneinteger,intent(in):

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論