fortran90例子.docx

上传人:b****6 文档编号:13824016 上传时间:2023-06-17 格式:DOCX 页数:10 大小:16.25KB
下载 相关 举报
fortran90例子.docx_第1页
第1页 / 共10页
fortran90例子.docx_第2页
第2页 / 共10页
fortran90例子.docx_第3页
第3页 / 共10页
fortran90例子.docx_第4页
第4页 / 共10页
fortran90例子.docx_第5页
第5页 / 共10页
fortran90例子.docx_第6页
第6页 / 共10页
fortran90例子.docx_第7页
第7页 / 共10页
fortran90例子.docx_第8页
第8页 / 共10页
fortran90例子.docx_第9页
第9页 / 共10页
fortran90例子.docx_第10页
第10页 / 共10页
亲,该文档总共10页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

fortran90例子.docx

《fortran90例子.docx》由会员分享,可在线阅读,更多相关《fortran90例子.docx(10页珍藏版)》请在冰点文库上搜索。

fortran90例子.docx

fortran90例子

例1、输入M个实数,将其相加,并输出其和。

PROGRAMexample_1

Implicitnone

Integer:

:

n,m

Real:

:

t=0,a=0

Read*,m

Do

Read*,a

T=t+a

N=n+1

If(n>=m)exit

Enddo

Print*,t

Endprogramexample_1

例2、求∑I!

的阶乘(I=4,8)。

Functionfactor(n)result(fac_result)

Implicitnone

Integer,intent(in):

:

n

Integer,intent(out):

:

fac_result

Integer:

:

I

Fac_result=1

DoI=1,n

Fac_result=fac_result*I

Enddo

Endfunctionfactor

Programexample_2

Implicitnone

Integer:

:

factor,s=0,I

DoI=4,8

S=s+factor(i)

Enddo

Print*,s

Endprogramexample_2

例3、输入一个数,判断他是否能被3整除,并输出相应的信息。

Programjudge

Implicitnone

Integer:

:

n,m

Read*,n

M=mod(n,3)

Selectcase(m)IF(M==0)THEN

Case(0)

Print*,’yes’Print*,’YES’

CasedefaultELSE

Print*,’no’Print*,’NO’

EndselectENDIF

Endprogramjudge

例4、判断一个整数N是否为素数

PROGRAMprime

Implicitnone

Integer:

:

n,I,m

Read*,n

M=sqrt(real(n))

DoI=2,m

If(mod(n,i)==0)exit

Enddo

If(I>m)then

Print*,’yes’

Else

Print*,’no’

endif

endprogramprime

例5、求N的阶乘

PROGRAMexample_5

Implicitnone

Integer:

:

n,I=0,fac=1

Read*,n

Dowhile(I<7)

I=I+1

Fac=fac*I

Enddo

Endprogramexample_5

例6、求出全部的水仙花数。

(水仙花数是个三位数,其各位数字的立方和等于该数。

programexample_6

implicitnone

integer:

:

I,j,k,m,n

ii:

doI=1,9

jj:

doj=1,9

kk:

dok=1,9

m=I*100+j*10+k

n=I**3+j**3+k**3

if(m==n)print*,m

enddokk

enddojj

enddoii

endprogramexaple_6

例7、牛顿迭代法求方程X**4+4*X+1=0的根

programexample_7

implicitnone

integer:

:

I=1,m

real:

:

x0,x,e

read*,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

Endif

Enddo

If(I

Endprogramexample_7

例8、将例2写成接口块的形式

主程序:

Programexample_2

Implicitnone

Interface

Functionfactor(n)result(factor_result)

Integer,intent(in):

:

n

Integer:

:

factor_result

Endfunctionfactor

Endinterface

Implicitnone

Integer:

:

s=0,I

DoI=4,8

S=s+factor(i)

Enddo

Print*,s

Endprogramexample_2

例9、将例2函数子程序改写成子例行子程序。

Subroutineisum(n,isum_result)

implicitnone

integer,intent(in):

:

n

integer,intent(out):

:

isum_result

integer:

:

I

isum_result=1

doI=1,n

isum_result=isum_result*I

enddo

endsubroutine

peogramexample_9

implicitnone

integer:

:

x,y

read*,x

callisum(x,y)

print*,’y=’,y

endprogramexample_9

例10、子程序作为虚元(虚过程)

PROGRAMexample_10

Implicitnone

Interface

Functionsum(x,y)result(sum_result)

Integer,intent(in):

:

x,y

Integer,intent(out):

:

sum_result

Endfunctionsum

Functionminu(x,y)result(minu_result)

Integer,intent(in):

:

x,y

Integer,intent(out):

:

minu_result

Endfunctionminu

Integer:

:

a,b

Read*,a,b

Callproc(a,b,sum)

Callproc(a,b,minu)

Endprogramexample_10

Subroutineproc(a,b,fun)

Implicitnone

Functionfun(x,y)result(fun_result)

Integer,intent(in):

:

x,y

Integer,intent(out):

:

fun_result

Endfunctionfun

Integer,intent(in):

:

a,b

Print*,fun(a,b)

Endsubroutine

Functionsum(x,y)result(sum_result)

Implicitnone

Integer,intent(in):

:

x,y

Integer,intent(out):

:

sum_result

Sum_result=x+y

Endfunctionsum

Functionminu(x,y)result(minu_result)

Implicitnone

Integer,intent(in):

:

x,y

Integer,intent(out):

:

minu_result

Minu_result=x-y

Endfunctionminu

例11、模块实现数据共享

moduleexam_module

implicitnone

real:

:

a,b,c

endmoduleexam_module

functionaver3()result(aver_result)

useexam_module

real:

:

aver_result

aver_result=(a+b+c)/3

endfunctionaver3

functionmax3()result(max_result)

useexam_module

real:

:

max_result

max_result=a

if(b>max_result)max_result=b

if(c>max_result)max_result=c

endfunctionmax3

programexample_11

useExam_module

real:

:

aver3,max3

read*,a,b,c

print*,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)

implicitnone

Integer,intent(in):

:

n

Integer,intent(out):

:

fac_result

If(n==0)then

Fac_result=1

Else

Fac_result=fac(n-1)*n

Endif

Endfunctionfac

Programexample_12

Implicitnone

Interface

Recursivefunctionfac(n)result(fac_result)

Integer,intent(in):

:

n

Integer,intent(out):

:

fac_result

Endfunctionfac

Endinterface

Integer:

:

n

Read*,n

Print*,fac(n)

Endprogramexample_12

例13、编一函数,求两数之和

a)用外部过程实现

programexample_131

implicitnone

integer:

:

a,b,sum

read*,a,b

calladd(a,b,sum)

print*,sum

endprogramexample_131

subroutineadd(a,b,sum)

implicitnone

integer,intent(in):

:

a,b

integer,intent(out):

:

sum

sum=a+b

endsubroutineadd

b)用内部过程实现

programexampl_132

implicitnone

integer:

:

a,b,sum

read*,a,b

calladd

print*,sum

contains

subroutineadd

sum=a+b

endsubroutineadd

endprogramexample_132

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2