Fortran语言编程小结_第1页
Fortran语言编程小结_第2页
Fortran语言编程小结_第3页
Fortran语言编程小结_第4页
Fortran语言编程小结_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

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

文档简介

1.单双精度

ProgramexOl

implicitnone

real(kind=4)::a

real(kind=8)::b

a=3._4!确定这个数字是使用单精度

b=3._8!确定这个数字是使用双精度

write(*,*)a,b

EndprogramexO1

2.判断kind值

programex02

Implicitnone!判断能够统计9个位数整数kind值

integer,parameter::long_int=selected_int_kind(9)!判断能够统计3个位数整数kind值

integer,piinimeter::short_int=sclcctcd_int_kind(3)

!判断能够有3个有效位数,指数能够统计到3浮点数kind值

integer,parameter::long_real=selected_real_kind(10,50)

!判断能够有10个有效位数,指数能够统计到50浮点数kind值

integer,parameter::short_real=selected_real_kind(3,3)

integer(kind=long_int)::a=123456

intcgcr(kind=short_int)::b=123

real(kind=long_real)::c=+1.23456789D45

rcal(kind=short_rcal)::d=+1230

write(*,"(I3JX,I1O)")long_int,a

write(*,"(I3JXJ10)")short_int,b

write(*,"(I3JX,E10.5)")long_real,c

write(*,"(I3,lX,E10.5)")short_real,d

END

3.TYPE

programex0434

implicitnone

Type::person!开始建立person这个类型

character(len=30)::name!人名

integer::age!年纪

integer::height!身高

INTEGER::weight!体重

character(len=80)::address!地址

Endtypeperson

type(person)::a!中明一个person类型变量

write(*.*)"NAME:"

read(*,*)a%name

wrile(*,*)"AGE:"

read(*,*)a%age

write(*,*)"HEIGHT:"

read(*,*)a%height

write(*,*)"WEIGHT:"

rcad(*,*)a%weight

write(*,*)"ADDRESS:"

read(*,*)a%address

write(*,100)a%name,a%age,a%height,a%weight

100fomiatC'Namez';AlO/,"Age",13/,"Height:M3/,"Weight:",I3/.&

"Addres:".A80)

End

4.REAL与INTEGER

Programcx0431

implicitnone

integer::a=l

integer::b=2

real::c

c=a/b!c=l/2=0,即使c是浮点数,但因为a,b是整数,计算a/b时会用整数去计算.

write(*;,(F5.2)")c

End

5.DATA变量表/初值表/,变量表/初值表/,…

writc(*JOO)a.b

1(X)format(L5,L4)

End

8.Programex0415

character(len=20)string

character(Ien=5)substring

string="Haveaniceday."

substring="nice"

write(*,*)ichar('A')!输出字符AASCII码

write"*)char(65)!输出ASCII码65所代表字符,也就是A

write(*,*)len(string)!输出字符串string申明时长度

writc(*,*)lcn_trim(s(ring)!输出字符串string内容长度

write(*,*)index(string,substring)!nice在Haveaniceday第8个位置

End

9.Programex0414

character(len=6)first

character(len=10)second

character(lcn=20)add

first="Happy"

second="Birthday"

add=first//second!经由两个连续除号能够连接两个字符串

write(*,100)add

100FORMAT,生日愉快"A)

End

10.带精度计算

Programex0408

rcal(kind=8)::a.b

a=l

b=0.1

write"*)a,"+",b;'=",a+b

End

H.逻辑if语句

Programcx0504

implicitnone

integerrain,windspeed

logicalr.w

write(*,*)"Rain:"

read(*.*)rain

write"*)"Wind:"

read(*,*)windspeed

r=(rain>=500)!假如rain>=150,r=.tnie,不然r=.false.

w=(windspeed>=10)!假如windspeed>=10,w=.true,不然w=.false.

if(r.or.w)then!只要r或w有一个值是true就成立

"停顿上班上课’

else

wri【c(*,*)"照常上班上课’

Endif

End

12.SelectCase使用方法

Programex0512

implicitnone

integer::score

charactergrade

writc(*,*),,Scorc:"

read(*,*)score

selectcase(score)

case(9O:IOO)

grade="A"

case(80:89)

grade='B'

casedefault

grade='?*

Endselect

write(*,,,('Grade:;A1)")grade

End

13.IFGOTO语句

PROGRAMexO514

IMPLICITNONE

REALheight

REALweight

WRITE(*,*)"height:"

READE,*)height

WRITER,*)"weight:"

READ(*,*)weight

ir(weight>height-100)GOTO200

iOOWRITE(*,*)"Undercontrol."

GOTO300

200WRITE(*,*)"Toofat!"

300STOP

END

14.DOWHILE循环

Programcx0604

implicitnone

integer,parameter::liniii=l()

integercounter

integer::ans=0

counter=2

dowhile(counter<=limit)

ans=ans+counter

counter=counter+2

enddo

write(*,*)ans

END

15.CYCLE,EXIT语句

Programcx0609

implicitnone

integer::i,j

loop1:doi=l,3

loop2:doj=1.3

if(i==3)exitloop1!跳离loop1循环

if(j==2)cycleIoop2!术做loop2循环

write(*.)i,j

enddoloop2

enddoloop1

End

16.大小写字符

Programex0612

implicitnone

integeri

integerstrlen

characterlen=20)::string

write(*,*)"String:"

read(*,*)string

strlen=LEN_TRIM(s(ring)

doi=I,strlen

string(i:i)=char(ichar(string(i:i))+32)

enddo

write(*,"('encoded:\A20)")string

End

17.循环计算

Programex0614

implicitnone

reala.b.ans

character::key='y'!为了最少循环一次

dowhile(key=-y'.or.key==Y')

read(*,*)a

read(*,"(Al)")key

read(*,*)b

selectcase(key)

case('+')

ans=a+b

case('-')

ans=a-b

case('*')

ans=a*b

casc(7')

ans=a/b

casedefault

write(*,"('Unknownoperator\AI)")key

stop

endselect

write(*,,,(F6.2,AI,F62,=',F6.2)“)a,key,b,ans

write(*,*)"(Y/y)todoagain.(Other)toexit."

rcad(*,"(Al)")key

enddo

End

18.矩阵相加

pogramex

implicitnone

integer,parameter::row=2

integer,parameter::col=2

integer::matrixA(row.col)

integer::matrixB(row.col)

integer::matrixC(ro\v,col)

integerr

integerc

writc(*,*)"MatrixA”

dor=I,row

doc=l,col

write(*,H('A(',n,V,II,•)=')")r,c

read(*,*)matrixA(r,c)

enddo

enddo

write(*,*)"MatrixB"

dor=1,row

doc=l,col

write(*,"(,B(',n,;,n,A『)r,c

read(*,*)matrixB(r,c)

enddo

enddo

write(*,*)"MatrixA+B="

dor=l,row

doc=l,col

matrixC(r,c)=matrixB(r,c)+niatrixA(i;c)

write(久113)")r,c,matrixC(r,c)

enddo

enddo

endpogramex

[write("(I3J3J,I3,I3)")((mc[i,j),i=l,2)j=l,2)j

19.programex

implicitnone

integer,parameter::row=2

integer,parameter::col=2

integer::a(2,2)=(/1,2,3.4/)

!a(lj)=l,a(2,l)=2,a(1,2)=3,a(2,2)=4

integer::b(4)=(/5,6,7,8/)

integer::c(2)

write(**)a,2)

write(*.*)a(:,l)

c=a(:,l)!c(l)=a(l,l),c(2)=a(2,l)

write(*,*)c!c(l),c(2)

c=a(2,:)!c(l)=a(2J)tc(2)=a(2,2)

write(*,*)c!c(l),c(2)

write(*,*)c(2:l:-l)!c(2),c(l)

c=b(1:4:2)!c(l)=b(l),c(2)=b(3)

write(*,*)c!c(l),c⑵

End

20.EORALL语句

Programex

Implicitnone

integer::LJ

integer,parameter::size=5

integer::a(size,size)

forall(1=1:size,J=1:size,I>J)a(I,J)=1!上半部分

forall(I=l:size,J=l:size.I==J)a(I,J)=2!对角线

forall(I=l:size,J=l:size,l<J)a(I,J)=3!下半部分

writc(*,"(5(515/))")a

End

21.Allocatable

ProgramEX

Implicitnone

integer::size,error=0

integer,parameter::onc_mb=1024*1024!1MB

character,allocatable::a(:)

Do

size=size+one_mb!

allocate(a(size),stat=enor)

if(crror/=0)exit

write(*;,('Allocate',110,'bytes')*')size

writc(*,,,(ri0.2,'MDused')")rcal(sizc)/rc<il(onc_mb)

deallocate(a)

Enddo

End

22.Function函数

Programex

implicitnone

real::a=10

real::b=20

real::add

write(*."(f6.2)")add(a.b)

End

Functionadd(a,b)

implicitnone

real::a.b

real::add

add=a*b

End

23.SAVE语句

Programex

Implicitnone

callsub()

callsub()

callsub()

Endprogram

Subroutinesub()

Implicitnone

Integer::count=I

Savecount!指定save变量永远活着,不会忘记它内容

Write(*,*)count

count=couni+1

End[运行结果:123]

24.生成随机数

programex

implicitnone

interface!定义函数接LI

functionrandom10(1bound,ubound)

implicitnone

real二Ibound、ubound

real::random10(10)

endfunction

endinterface

real::a(IO)

CALLRANDOM_SEED()!系统依照日期和时间随机地提供种子

a=random10(1.0,10.0)

write(*;,(IOF6.2)")a

end

functionrandom10(lbound.abound)

implicitnone

real::Ibound.ubound

real::ien

real::random10(10)

real(

integeri

len=ubound-Ibound!计克范围大小

doi=1,10

callrandom_number(t)!t会

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论