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

下载本文档

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

文档简介

1.单双精度Programex01implicitnonereal(kind=4)::areal(kind=8)::ba=3._4 !确定这个数字是使用单精度b=3._8 !确定这个数字是使用双精度write(*,*)a,bEndprogramex012.判断kind值programex02Implicitnone !判断能够统计9个位数整数kind值integer,parameter::long_int=selected_int_kind(9)!判断能够统计3个位数整数kind值integer,parameter::short_int=selected_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=123456integer(kind=short_int)::b=123real(kind=long_real)::c=+1.23456789D45real(kind=short_real)::d=+1230write(*,"(I3,1X,I10)")long_int,awrite(*,"(I3,1X,I10)")short_int,bwrite(*,"(I3,1X,E10.5)")long_real,cwrite(*,"(I3,1X,E10.5)")short_real,dEND3.TYPEprogramex0434implicitnoneType::person !开始建立person这个类型character(len=30)::name!人名integer::age!年纪integer::height!身高 INTEGER::weight!体重character(len=80)::address!地址Endtypepersontype(person)::a !申明一个person类型变量write(*,*)"NAME:"read(*,*)a%namewrite(*,*)"AGE:"read(*,*)a%agewrite(*,*)"HEIGHT:"read(*,*)a%heightwrite(*,*)"WEIGHT:"read(*,*)a%weightwrite(*,*)"ADDRESS:"read(*,*)a%addresswrite(*,100)a%name,a%age,a%height,a%weight100format("Name:",A10/,"Age:",I3/,"Height:",I3/,"Weight:",I3/,&"Addres:",A80)End4.REAL与INTEGERProgramex0431implicitnoneinteger::a=1integer::b=2real::cc=a/b!c=1/2=0,即使c是浮点数,但因为a,b是整数,计算a/b时会用整数去计算.write(*,"(F5.2)")cEnd5.DATA变量表/初值表/,变量表/初值表/,…PROGRAMex0430IMPLICITNONEINTEGERAREALBCOMPLEXCCHARACTER(20)STRDATAA,B,C,STR/1,2.0,(1.0,2.0),'FORTRAN77'/WRITE(*,*)A,B,,C,STREND6.复数实虚部Programex0430complex::c=(1,2)write(*,100)real(c),'+',aimag(c),'i'100format(f5.1,a1,f5.1,a1)End7.逻辑输出Programex0416logicala,ba=.true.b=.false.write(*,100)a,b100format(L5,L4)End8.Programex0415character(len=20)stringcharacter(len=5)substringstring="Haveaniceday."substring="nice"write(*,*)ichar('A') !输出字符AASCII码write(*,*)char(65) !输出ASCII码65所代表字符,也就是Awrite(*,*)len(string) !输出字符串string申明时长度write(*,*)len_trim(string) !输出字符串string内容长度write(*,*)index(string,substring)!nice在Haveaniceday第8个位置End9.Programex0414character(len=6)firstcharacter(len=10)secondcharacter(len=20)addfirst="Happy"second="Birthday"add=first//second!经由两个连续除号能够连接两个字符串write(*,100)add100FORMAT('生日愉快',/,A)End10.带精度计算Programex0408real(kind=8)::a,ba=1b=0.1write(*,*)a,"+",b,"=",a+bEnd11.逻辑if语句Programex0504implicitnoneintegerrain,windspeedlogicalr,wwrite(*,*)"Rain:"read(*,*)rainwrite(*,*)"Wind:"read(*,*)windspeedr=(rain>=500) !假如rain>=150,r=.true,不然r=.false.w=(windspeed>=10) !假如windspeed>=10,w=.true,不然w=.false.if(r.or.w)then !只要r或w有一个值是true就成立write(*,*)"停顿上班上课"elsewrite(*,*)"照常上班上课"EndifEnd12.SelectCase使用方法Programex0512implicitnoneinteger::scorecharactergradewrite(*,*)"Score:"read(*,*)scoreselectcase(score)case(90:100)grade='A'case(80:89)grade='B'casedefaultgrade='?'Endselectwrite(*,"('Grade:',A1)")gradeEnd13.IFGOTO语句 PROGRAMex0514 IMPLICITNONE REALheight REALweight WRITE(*,*)"height:" READ(*,*)height WRITE(*,*)"weight:" READ(*,*)weight IF(weight>height-100)GOTO200100 WRITE(*,*)"Undercontrol." GOTO300200 WRITE(*,*)"Toofat!"300 STOP END14.DOWHILE循环Programex0604implicitnoneinteger,parameter::limit=10integercounterinteger::ans=0counter=2dowhile(counter<=limit)ans=ans+countercounter=counter+2enddowrite(*,*)ansEND15.CYCLE,EXIT语句Programex0609implicitnoneinteger::i,jloop1:doi=1,3loop2:doj=1,3if(i==3)exitloop1 !跳离loop1循环if(j==2)cycleloop2 !重做loop2循环write(*,"('(',i2,',',i2,')')")i,jenddoloop2enddoloop1End16.大小写字符Programex0612implicitnoneintegeriintegerstrlencharacter(len=20)::stringwrite(*,*)"String:"read(*,*)stringstrlen=LEN_TRIM(string)doi=1,strlenstring(i:i)=char(ichar(string(i:i))+32)enddowrite(*,"('encoded:',A20)")stringEnd17.循环计算Programex0614implicitnonereala,b,anscharacter::key='y' !为了最少循环一次dowhile(key=='y'.or.key=='Y')read(*,*)a read(*,"(A1)")key read(*,*)b selectcase(key) case('+') ans=a+b case('-') ans=a-b case('*') ans=a*b case('/') ans=a/b casedefault write(*,"('Unknownoperator',A1)")key stop endselectwrite(*,"(F6.2,A1,F6.2,'=',F6.2)")a,key,b,ans write(*,*)"(Y/y)todoagain.(Other)toexit." read(*,"(A1)")keyenddoEnd18.矩阵相加pogrameximplicitnoneinteger,parameter::row=2integer,parameter::col=2integer::matrixA(row,col)integer::matrixB(row,col)integer::matrixC(row,col)integerrintegercwrite(*,*)"MatrixA"dor=1,row doc=1,colwrite(*,"('A(',I1,',',I1,')=')")r,c read(*,*)matrixA(r,c) enddoenddowrite(*,*)"MatrixB"dor=1,row doc=1,colwrite(*,"('B(',I1,',',I1,')=')")r,c read(*,*)matrixB(r,c) enddoenddowrite(*,*)"MatrixA+B="dor=1,row doc=1,col matrixC(r,c)=matrixB(r,c)+matrixA(r,c) write(*,"('(',I1,',',I1,')=',I3)")r,c,matrixC(r,c) enddoenddoendpogramex[write(*,"(I3,I3,/,I3,I3)")((mc(i,j),i=1,2),j=1,2)]19.programeximplicitnoneinteger,parameter::row=2integer,parameter::col=2integer::a(2,2)=(/1,2,3,4/)!a(1,1)=1,a(2,1)=2,a(1,2)=3,a(2,2)=4integer::b(4)=(/5,6,7,8/)integer::c(2)write(*,*)a,2)write(*,*)a(:,1)c=a(:,1)!c(1)=a(1,1),c(2)=a(2,1)write(*,*)c!c(1),c(2)c=a(2,:)!c(1)=a(2,1),c(2)=a(2,2)write(*,*)c!c(1),c(2)write(*,*)c(2:1:-1)!c(2),c(1)c=b(1:4:2)!c(1)=b(1),c(2)=b(3)write(*,*)c!c(1),c(2)End20.FORALL语句ProgramexImplicitnoneinteger::I,Jinteger,parameter::size=5integer::a(size,size)forall(I=1:size,J=1:size,I>J)a(I,J)=1!上半部分forall(I=1:size,J=1:size,I==J)a(I,J)=2!对角线forall(I=1:size,J=1:size,I<J)a(I,J)=3!下半部分write(*,"(5(5I5,/))")aEnd21.AllocatableProgramEXImplicitnoneinteger::size,error=0integer,parameter::one_mb=1024*1024!1MBcharacter,allocatable::a(:)Dosize=size+one_mb!allocate(a(size),stat=error)if(error/=0)exitwrite(*,"('Allocate',I10,'bytes')")size write(*,"(F10.2,'MBused')")real(size)/real(one_mb)deallocate(a)EnddoEndFunction函数Programeximplicitnonereal::a=10real::b=20real::addwrite(*,"(f6.2)")add(a,b)EndFunctionadd(a,b)implicitnonereal::a,breal::addadd=a*bEndSAVE语句ProgramexImplicitnonecallsub()callsub()callsub()EndprogramSubroutinesub()ImplicitnoneInteger::count=1Savecount!指定save变量永远活着,不会忘记它内容Write(*,*)countcount=count+1End[运行结果:123]24.生成随机数programeximplicitnoneinterface!定义函数接口functionrandom10(lbound,ubound) implicitnone real::lbound,ubound real::random10(10) endfunctionendinterfacereal::a(10)CALLRANDOM_SEED()!系统依照日期和时间随机地提供种子a=random10(1.0,10.0)write(*,"(10F6.2)")aendfunctionrandom10(lbound,ubound)implicitnonereal::lbound,uboundreal::lenreal::random10(10)realtintegerilen=ubound-lbound!计算范围大小doi=1,10call

温馨提示

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

评论

0/150

提交评论