




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- ABA培训课件教学课件
- 知识产权管理
- 安庆市重点中学2024-2025学年高三下学期5月考试卷物理试题试卷含解析
- 甘肃省白银市白银区2025届四下数学期末复习检测模拟试题含解析
- 2016交通安全课件
- 2015食品安全课件
- 山东省滕州市2024-2025学年高考摸底测试综合能力试题含解析
- 重庆中医药学院《规划理论与规划思想》2023-2024学年第二学期期末试卷
- 宁波财经学院《建筑环境测试技术》2023-2024学年第二学期期末试卷
- 四川省蓉城名校2025年高三毕业生3月学习质量检测试题生物试题含解析
- 2025年职业指导师专业能力测试卷:职业指导服务与心理咨询
- 学校安全管理制度汇编
- 2025-2030中国化妆棉行业市场深度调研及发展策略研究报告
- 【版】(4月4日)清明守规平安同行- 清明节假期安全教育主题班会 高中主题班会课件
- 2024年安庆迎江区招聘社区工作人员考试真题
- 早产儿护理的试题及答案
- 2024年山东电力中心医院高层次卫技人才招聘笔试历年参考题库频考点附带答案
- 浙江省四校联考2023至2024学年高一下学期3月月考化学试题附参考答案(解析)
- 左心衰竭合并肺水肿的护理查房
- 重力坝毕业设计-水电站混凝土重力坝工程设计
- CFM567反推装置介绍
评论
0/150
提交评论