fortran90例子_第1页
fortran90例子_第2页
fortran90例子_第3页
fortran90例子_第4页
fortran90例子_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、例1、 输入M个实数,将其相加,并输出其和。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、 输入一个数,判断他是否能被3整除,并输出相应的信息。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、 判断一个整数N是否为素数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、 求出全部的水仙花数。(水仙花数是个三位数,其各位数字的立方和等于该数。)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控制迭代次数)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函数子程序改写成子例行子程序。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、 子程序作为虚元(虚过程)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、 模块实现数据共享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 :实体名例: use exam_module,only:a,b,此时C不再是共享变量,故C仍需通过虚实结合。 (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、 编一函数,求两数之和a) 用外部过程实现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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论