您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 其它文档 > fortran90例子
例1、输入M个实数,将其相加,并输出其和。PROGRAMexample_1ImplicitnoneInteger::n,mReal::t=0,a=0Read*,mDoRead*,aT=t+aN=n+1If(n=m)exitEnddoPrint*,tEndprogramexample_1例2、求∑I!的阶乘(I=4,8)。Functionfactor(n)result(fac_result)ImplicitnoneInteger,intent(in)::nInteger,intent(out)::fac_resultInteger::IFac_result=1DoI=1,nFac_result=fac_result*IEnddoEndfunctionfactorProgramexample_2ImplicitnoneInteger::factor,s=0,IDoI=4,8S=s+factor(i)EnddoPrint*,sEndprogramexample_2例3、输入一个数,判断他是否能被3整除,并输出相应的信息。ProgramjudgeImplicitnoneInteger::n,mRead*,nM=mod(n,3)Selectcase(m)IF(M==0)THENCase(0)Print*,’yes’Print*,’YES’CasedefaultELSEPrint*,’no’Print*,’NO’EndselectENDIFEndprogramjudge例4、判断一个整数N是否为素数PROGRAMprimeImplicitnoneInteger::n,I,mRead*,nM=sqrt(real(n))DoI=2,mIf(mod(n,i)==0)exitEnddoIf(Im)thenPrint*,’yes’ElsePrint*,’no’endifendprogramprime例5、求N的阶乘PROGRAMexample_5ImplicitnoneInteger::n,I=0,fac=1Read*,nDowhile(I7)I=I+1Fac=fac*IEnddoEndprogramexample_5例6、求出全部的水仙花数。(水仙花数是个三位数,其各位数字的立方和等于该数。)programexample_6implicitnoneinteger::I,j,k,m,nii:doI=1,9jj:doj=1,9kk:dok=1,9m=I*100+j*10+kn=I**3+j**3+k**3if(m==n)print*,menddokkenddojjenddoiiendprogramexaple_6例7、牛顿迭代法求方程X**4+4*X+1=0的根programexample_7implicitnoneinteger::I=1,mreal::x0,x,eread*,x0,e,m(m控制迭代次数)dox=(-x0*x0-1)/4if(abs(x-x0)=e)exitx0=xI=I+1If(I=m)thenPrint*,’not’ExitEndifEnddoIf(Im)print*,I,xEndprogramexample_7例8、将例2写成接口块的形式主程序:Programexample_2ImplicitnoneInterfaceFunctionfactor(n)result(factor_result)Integer,intent(in)::nInteger::factor_resultEndfunctionfactorEndinterfaceImplicitnoneInteger::s=0,IDoI=4,8S=s+factor(i)EnddoPrint*,sEndprogramexample_2例9、将例2函数子程序改写成子例行子程序。Subroutineisum(n,isum_result)implicitnoneinteger,intent(in)::ninteger,intent(out)::isum_resultinteger::Iisum_result=1doI=1,nisum_result=isum_result*Ienddoendsubroutinepeogramexample_9implicitnoneinteger::x,yread*,xcallisum(x,y)print*,’y=’,yendprogramexample_9例10、子程序作为虚元(虚过程)PROGRAMexample_10ImplicitnoneInterfaceFunctionsum(x,y)result(sum_result)Integer,intent(in)::x,yInteger,intent(out)::sum_resultEndfunctionsumFunctionminu(x,y)result(minu_result)Integer,intent(in)::x,yInteger,intent(out)::minu_resultEndfunctionminuInteger::a,bRead*,a,bCallproc(a,b,sum)Callproc(a,b,minu)Endprogramexample_10Subroutineproc(a,b,fun)ImplicitnoneFunctionfun(x,y)result(fun_result)Integer,intent(in)::x,yInteger,intent(out)::fun_resultEndfunctionfunInteger,intent(in)::a,bPrint*,fun(a,b)EndsubroutineFunctionsum(x,y)result(sum_result)ImplicitnoneInteger,intent(in)::x,yInteger,intent(out)::sum_resultSum_result=x+yEndfunctionsumFunctionminu(x,y)result(minu_result)ImplicitnoneInteger,intent(in)::x,yInteger,intent(out)::minu_resultMinu_result=x-yEndfunctionminu例11、模块实现数据共享moduleexam_moduleimplicitnonereal::a,b,cendmoduleexam_modulefunctionaver3()result(aver_result)useexam_modulereal::aver_resultaver_result=(a+b+c)/3endfunctionaver3functionmax3()result(max_result)useexam_modulereal::max_resultmax_result=aif(bmax_result)max_result=bif(cmax_result)max_result=cendfunctionmax3programexample_11useExam_modulereal::aver3,max3read*,a,b,cprint*,aver3(),max3()endprogramexample_11注意:(1)USE模块名,ONLY:实体名例:useexam_module,only:a,b,此时C不再是共享变量,故C仍需通过虚实结合。(2)useexam_module,x-a将模块中A与程序单元中变量X共享。例12、递归recursivefunctionfac(n)result(fac_result)implicitnoneInteger,intent(in)::nInteger,intent(out)::fac_resultIf(n==0)thenFac_result=1ElseFac_result=fac(n-1)*nEndifEndfunctionfacProgramexample_12ImplicitnoneInterfaceRecursivefunctionfac(n)result(fac_result)Integer,intent(in)::nInteger,intent(out)::fac_resultEndfunctionfacEndinterfaceInteger::nRead*,nPrint*,fac(n)Endprogramexample_12例13、编一函数,求两数之和a)用外部过程实现programexample_131implicitnoneinteger::a,b,sumread*,a,bcalladd(a,b,sum)print*,sumendprogramexample_131subroutineadd(a,b,sum)implicitnoneinteger,intent(in)::a,binteger,intent(out)::sumsum=a+bendsubroutineaddb)用内部过程实现programexampl_132implicitnoneinteger::a,b,sumread*,a,bcalladdprint*,sumcontainssubroutineaddsum=a+bendsubroutineaddendprogramexample_132
本文标题:fortran90例子
链接地址:https://www.777doc.com/doc-1394962 .html