(完整word版)fortran程序实例

合集下载

fortran程序案例题汇编(14道)-推荐下载

fortran程序案例题汇编(14道)-推荐下载
6.用函数子程序的方法设计一个判断某个数是否是素数的程序,统计 100~1000 内的素数 的个数。 logical function prime(n) logical t t=.true. do i=2,n-1 if(mod(n,i)==0)then
t=.false. exit endif enddo prime=t end
1
对全部高中资料试卷电气设备,在安装过程中以及安装结束后进行高中资料试卷调整试验;通电检查所有设备高中资料电试力卷保相护互装作置用调与试相技互术关,系电通,力1根保过据护管生高线产中0不工资仅艺料可高试以中卷解资配决料置吊试技顶卷术层要是配求指置,机不对组规电在范气进高设行中备继资进电料行保试空护卷载高问与中题带资2负料2,荷试而下卷且高总可中体保资配障料置2试时32卷,3各调需类控要管试在路验最习;大题对限到设度位备内。进来在行确管调保路整机敷使组设其高过在中程正资1常料中工试,况卷要下安加与全强过,看度并25工且52作尽22下可护都能1关可地于以缩管正小路常故高工障中作高资;中料对资试于料卷继试连电卷接保破管护坏口进范处行围理整,高核或中对者资定对料值某试,些卷审异弯核常扁与高度校中固对资定图料盒纸试位,卷置编工.写况保复进护杂行层设自防备动腐与处跨装理接置,地高尤线中其弯资要曲料避半试免径卷错标调误高试高等方中,案资要,料求编试技5写、卷术重电保交要气护底设设装。备备置管4高调、动线中试电作敷资高气,设料中课并技3试资件且、术卷料中拒管试试调绝路包验卷试动敷含方技作设线案术,技槽以来术、及避管系免架统不等启必多动要项方高方案中式;资,对料为整试解套卷决启突高动然中过停语程机文中。电高因气中此课资,件料电中试力管卷高壁电中薄气资、设料接备试口进卷不行保严调护等试装问工置题作调,并试合且技理进术利行,用过要管关求线运电敷行力设高保技中护术资装。料置线试做缆卷到敷技准设术确原指灵则导活:。。在对对分于于线调差盒试动处过保,程护当中装不高置同中高电资中压料资回试料路卷试交技卷叉术调时问试,题技应,术采作是用为指金调发属试电隔人机板员一进,变行需压隔要器开在组处事在理前发;掌生同握内一图部线纸故槽资障内料时,、,强设需电备要回制进路造行须厂外同家部时出电切具源断高高习中中题资资电料料源试试,卷卷线试切缆验除敷报从设告而完与采毕相用,关高要技中进术资行资料检料试查,卷和并主检且要测了保处解护理现装。场置设。备高中资料试卷布置情况与有关高中资料试卷电气系统接线等情况,然后根据规范与规程规定,制定设备调试高中资料试卷方案。

(完整word版)fortran程序实例

(完整word版)fortran程序实例

1) 实例3 —求多个半径下的圆周长 ! z3.f90 --Fortran95 FUNCTIONS:-Entry point of con sole application.I*************************************************************************PROGRAM: z3PURPOSE: En try point for the con sole applicati on.I************************************************************************program z3 !求多个半径下的圆周长 !主程序! PROGRAM Z3PRINT *, 'R=',1.2,'C=',C(1.2) PRINT *, 'R=',3.4,'C=',C(3.4) PRINT *, 'R=',15.6,'C=',C(15.6) PRINT *, 'R=',567.3,'C=',C(567.3) END program z3 !子程序FUNCTION C(R) PI=3.1415926 C=2*PI*R RETURN ! Body of z3 endz3•■Alta• JSwFta.■■ TTjn* FMIIH-.T jd - |叭■(Lld4ilh■ Hi li tii.IW M H MvfriiMfeai Iwi Ji JJII+M4Tml+n««I*界rFHtlE- HfrtpliK * W mita itflLuLfl-PRINT* ,'以上为计算机的计算结果,注意 B 的值'ZXZ I O.f90 FUNCTIONS: ZXZ_I_O- Entry point of con sole applicati on.PROGRAM: ZXZ_I_OPURPOSE: En try point for the con sole applicati on.program ZXZ_I_O implicit none!变量声明的位置INTEGER(2) i; INTEGER(4) j; INTEGER(4) m; REAL n INTEGER A,B PRINT*,'输入整数 A'; READ*, A PRINT*,'输入整数 B'; READ*, B B=A+BWRITE(*,*) 'A*B=',A*B实例4 —键盘与显示器输入/输出2)a) Fortra n 基本操作1 B Li PlLu- i \ JfiL ■ I■' hi -IJBL . n :»'匹:"b )程序指令BIE1A J~. C ■* «I*************** 输入、输出样式种种 **************************!系统默认的输出样式 PRINT* ,'系统默认的输出样式' !人为控制的的输出样式--格式化输出 i=21; j=53; m=5 n=(i+j*m*i**m) WRITE(*,*) 'i,j,m 是常量,程序赋初值PRINT*, i,j,m WRITE(*,*) 'i,j,m 的计算结果:'PRINT*,'i+j*m*i**m=' ,nPRINT* ,''! Body of ZXZ_I_O end program ZXZ_I_O 程序说明: 程序赋值一初始化 i=21; j=53; m=5 键盘无格式输入 READ*, A 键盘有格式输入 READ ( *, 100)A,B,C 100 FORMA T( 2F5.2,F5.3) 显示器无格式输出PRINT* ,'系统默认的输出样式' WRITE(*,*) 'A*B=',A*B 显示器有格式输出 PRINT 100 ,A+B WRITE(*,100) 'A*B=',A*B 100 FORMA T( F5.2)C )调试运行d )程序指令pruqrdiri ZX2_l_0 iflnpllclt iioiiv 陝量声明的立置IHTEGERfZ) i; IMTEGEimi ]; IHTEGER(^)叭 REAL nA.B I Uairidbles■HIKE J 播入整該■打1E.D 屯A PRINT •,-KM«a■ e-fltBFHJHIv T -H-Ai-B-1 ,ti' Axb- 1 T A I UHU NA 「臥存为计負机药计貝韭呆*妊憲L1的低・1-21; j-b3;«R[TEC-,«) l,j,n 是常最.程序Itt 前值・FHIHTa, 1 J ,■WR1TF(-F -J l p j,n 的tt 幣结黑,.FHim* /■r u 讪o#畑」」Ffirl prnni'an ZK? I o! ZXZ丄O.f90! FUNCTIONS:ZXZ_I_O - Entry point of con sole applicati on.PROGRAM: ZXZ_I_OPURPOSE: En try point for the con sole applicati on.1 *************** 输^入 ^输出样式种不中program ZXZ_I_Oimplicit none!变量声明的位置INTEGER(2) i; INTEGER(4) j; INTEGER(4) m; REAL nINTEGER A,BREAL X,Y ,Z ! VariablesPRINT*,'输入整数A'; READ*, APRINT*,'输入整数B'; READ*, B PRINT*,'计算结果为:'B=A+BPRINT*,'B=A+B=',BWRITE(*,*) 'A*B=',A*BPRINT* ,'以上为计算机的计算结果,注意B的值'!系统默认的输出样式PRINT* ,'系统默认的输出样式'PRINT*,'输入实数X'; READ(*,100) XPRINT*,'输入实数Y:READ(*,100) Y100 FORMA T(F5.2)PRINT*,'计算结果为:'Z=X+YPRINT 200,Z200 FORMA T(4X,'Z=X+Y=',F8.3)WRITE(*,*)WRITE(*,300) X*Y300 FORMA T(4X,'Z=X*Y=',F8.3)!人为控制的的输出样式--格式化输出PRINT* ,'程序为常量赋了初值'i=21; j=53; m=5n=(i+j*m*i**m)WRITE(*,*) 'i,j,m 是常量,程序赋初值PRINT*, i,j,mWRITE(*,*) 'i,j,m 的计算结果:'PRINT*,'i+j*m*i**m=' ,nPRINT* ,' '! Body of ZXZ_I_Oend program ZXZ_I_Oe)调试运行rRIHT* JPRTNi*. ■讦薛结果为.・ E^A*BPRTNT*,,R=A+R=,,8 URrfEfw,*) 'ft-D-1t A-DPRINT * J 以上药i1■宴利的iT 畀结果.傢金默认囱馥出择畫PRINT*「系唏默认册葆出样式・ 阳IHH. •揃扎斓 W : WIHZJ 输入宪数V;1B0 F0RHAKF5.2)PRINT -,■廿尊结杲为FREAD(*,1ua ) K R 匚flD(*,100) VPRINT 2Q0左2(JU FUKMfil(4i(t ; = «+/= \FB »J) unrrEf*.*)WRITE(*r 30O) X*V FORMfiT(UX /? = X*V=* ,F8 .3}认为扌前函的输世#戎一逼占化输出PRTHT- ”囉序対常豊瓯了初值・ i^Z» i j=53;I1=5 n=(i +j*m*i**n)WRITEf"^) 'i t j t n 是常量,程序赋初值 PRTHTw, i r j F nMRlTEe 』)・i J,n 的计具结衆:,PRI HI*.'i+j *R*i**m=',n。

FORTRAN编程演示

FORTRAN编程演示

2-4 输入输出语句 PRINT语句是Fortran中较为常用的一种输 出语句,用它来完成表控输出的语法格式如 下所示。 PRINT *[, 输出项,……] 在PRINT语句中不能指定输出设备,只能向 系统隐含指定的设备输出数据(隐含指定的 输出设备一般是指计算机屏幕);语句中的 星号“*”表示“表控格式输出”,即按照 计算机默认的格式进行数据的输出;输出项 可以有多个,每个输出项之间用逗号“,”分 隔;当语句中没有输出项时,命令将输出一 个空白行。

输入语句 使用READ语句来完成表控输入的语法形式 如下所示。 READ *[, 输入项,……] 在这种形式中,不能指定输入设备,只能由 系统隐含指定的设备上输入(系统隐含指定 的设备一般是指键盘);语句中的星号“*” 表示“表控输入”,即按照数据合法的格式 进行输入;输入项可以有多个,每个输出项 之间用逗号“,”分隔;当语句中没有输入项 时,系统将挂起程序的运行直到用户键入回 车符。
2.编写程序已知园半径分别为1、3、12时,对应的 园周长为多少? 示例4



C MAIN PROGEAM !注释下面为主程序 PROGRAM EX2 PRINT *,“R=”,1.0,“C=”,C(1.0) !显示半径周长 PRINT *,"R=",3.0,"C=",C(3.0) PRINT *,“R=”,12.0,“C=”,C(12.0) !未解释C END C SUBPROGRAM !下面为子程序 FUNCTION C(R) !C(R) 是一个函数名字 PI=3.1415926 C=2*PI*R RETURN END !编写了函数的内容
示范举例1。
2- 2 编程修改程序

FORTRAN90程序设计1[1]

FORTRAN90程序设计1[1]

程序设计举例
算法

算法是计算机软件中的一个基本概念,它是对解决实际问题的方法和步
骤的描述。设计算法是程序设计的核心,也是编写程序的基础。

正确的算法有以下几个显著特点:
(1)有穷性(flniteness)。 (2)确定性(certainty)。 (3)有效性(effcctivencss)。 (4)有零个或多个输入(input)。 (5)有一个或多个输出(output)。
简单的FORTRAN90程序分析

(6)空格并不是随处都可以使用的,一个像关键字、变量和常量名以 及操作符等的字符,其内部是不能使用空格的,空格会使字符失去其原 有的含义。 (7)FORTRAN90的注释语句是以感叹号为标志的,一行中感叹号后的 所有字符都被编译器作为注释语句而忽略。注释语句可以单独占一行, 也可在程序的其他语句后面出现。在FORTRAN90中,空行被作为注释 语句。 (8)如果遇到一条语句的长度超过了FORTRAN90所允许的行最大长度, 需要写成几行,FORTRAN90提供了一个续行符 ( & ) ,通过在语句末尾 添加续行符,编译器就会把下一行作为续行来处理。如果是把一个如关 键字这样的字符分成两行,那么需要在下行语句的开头再加一个续行符。
CHARACTER ( LEN = 10 ) :: STRING 子字符串是字符串中的若干个连续字符的集合。子字符
CHARACTER (LEN = 5 ) :: SUBSTRING 串的表示方法如下: CHARACTER (LEN = 1 ) :: CHAR STRING=’JANEJORDE’ 字符串变量名([FIRST-POSITION] : [END-POSITION]) ! SUBSTRING的值为“JANE” SUBSTRING=STRING ( :5 ) 缺省的开始位置是1,缺省的结束位置是字符串的长度。 SUBSTRING=STRING ( 5:6 ) ! SUBSTRING的值为“J” 如果开始位置大于结束位置则子字符串为空,即它的长 SUBSTRING=STRING ( 3:7 ) ! SUBSTRING的值为“NEJO” N=7 度为0。 CHAR=’ABCDEFGHIJK’ ( N:N ) ! CHAR的值为“G”

Fortran95程序设计课后习题答案(word版方便)

Fortran95程序设计课后习题答案(word版方便)

第四章1.program main implicit none write(*,*) "Have a good time." write(*,*) "That's not bad." write(*,*) '"Mary" isn''t my name.' end program2.program main real, parameter :: PI=3 implicit none.14159 real radius write(*,*) "请输入半径长" read(*,*) radius write(*,"(' 面积='f8. 3)") radius*radius*PI end program3.program main implicit none real grades write(*,*) "请输入成绩" read(*,*) grades write(*,"(' 调整后成绩为'f8.3)") SQRT(grades)*10.0 end program4.integer a,b real ra,rb a=2 b=3 ra=2.0 rb=3.0 write(*,*) b/a ! 输出1, 因为使用整数计算, 小数部分会无条件舍去write(*,*) rb/ra ! 输出1.55.p rogram main implicit none type distance real meter, inch, cm end type type(distance) :: d write(*,*) "请输入长度:" read(*,*) d%meter d%cm = d%meter*100 d%inch = d%cm/2.54 write(*,"(f8.3'米='f8.3'厘米='f8.3'英寸')") d%meter, d%cm, d%inch end program第五章1.program main implicit none integer money real tax write(*,*) "请输入月收入" read(*,*) money if ( money<1000 ) then tax = 0.03 else if ( money<5000) then tax = 0.1 else tax = 0.15 end if write(*,"(' 税金为'I8)") nint(money*tax) end program2.program main implicit none integer day character(len=20) :: tv write(*,*) "请输入星期几" read(*,*) day select case(day) case(1,4) tv = "新闻" case(2,5) tv = "电视剧" case(3,6) tv = "卡通" case(7) tv = "电影" case default write(*,*) "错误的输入" stop end select write(*,*) tv end program3.program main implicit none integer age, money real tax write(*,*) "请输入年龄" read(*,*) age write(*,*) "请输入月收入" read(*,*) money if ( age<50 ) then if ( money<1000 ) then tax = 0.03 else if ( money<5000 )then tax = 0.10 else tax = 0.15 end if else if ( money<1000 ) then tax = 0.5 else if ( money<5000 )then tax = 0.7 else tax = 0.10 end if end if write(*,"(' 税金为'I8)") nint(money*tax) end program4.program main implicit none integer year, days logical mod_4, mod_100, mod_400 write(*,*) "请输入年份" read(*,*) year mod_4 = ( MOD(year,4) == 0 ) mod_100 = ( MOD(year,100) == 0 ) mod_400 = ( MOD(year,400) == 0 ) if ( (mod_4 .NEQV. mod_100) .or. mod_400 ) then days = 366 else days = 365 end if write(*,"('这一年有'I3'天')") days stop end program第六章1.program main implicit none integer i do i=1,5 write(*,*) "Fortran" end do stop end program2.program main implicit none integer i,sum sum = 0 do i=1,99,2 sum = sum+i end do write(*,*) sum stop end program3.program main implicit none integer, parameter :: answer = 45 integer, parameter :: max = 5 integer weight, i do i=1,max write(*,*) "请输入体重" read(*,*) weight if ( weight==answer ) exit end do if ( i<=max ) then write(*,*) "猜对了" else write(*,*) "猜错了" end if stop end program4.program main implicit none integer, parameter :: max=10 integer i real item real ans ans = 1.0 item = 1.0 do i=2,max item = item/real(i) ans = ans+itemend do write(*,*) ans stop end program5.program main implicit none integer, parameter :: length = 79 character(len=length) :: input, output integer i,j write(*,*) "请输入一个字串" read(*,"(A79)") input j=1 do i=1, len_trim(input) if ( input(i:i) /= ' ' ) then output(j:j)=input(i:i) j=j+1 end if end do write(*,"(A79)") output stop end program第七章1.program main implicit none integer, parameter :: max = 10 integer i integer :: a(max) = (/ (2*i, i=1,10) /) integer :: t ! sum()是fortran库函数write(*,*) real(sum(a))/real(max) stop end program2.integer a(5,5) ! 5*5=25 integer b(2,3,4) ! 2*3*4=24 integer c(3,4,5,6) ! 3*4*5*6=360 integer d(-5:5) ! 11 integer e(-3:3, -3:3) ! 7*7=493.program main implicit none integer, parameter :: max=10 integer f(max) integer i f(1)=0 f(2)=1 do i=3,max f(i)=f(i-1)+f(i-2) end do write(*,"(10I4)") f stop end program4.program main implicit none integer, parameter :: size=10 integer :: a(size) = (/ 5,3,6,4,8,7,1,9,2,10 /) integer :: i,j integer :: t do i=1, size-1 do j=i+1, size if ( a(i) < a(j) ) then ! a(i)跟a(j)交换t=a(i) a(i)=a(j) a(j)=t end if end do end do write(*,"(10I4)") a stop end5.a(2,2) ! 1+(2-1)+(2-1)*(5) = 7 a(3,3) ! 1+(3-1)+(3-1)*(5) = 13第八章1.program main implicit none real radius, area write(*,*) "请输入半径长" read(*,*) radius call CircleArea(radius, area) write(*,"(' 面积= 'F8.3)") area stop end program subroutine CircleArea(radius, area) implicit none real, parameter :: PI=3.14159 real radius, area area = radius*radius*PI return end subroutine2.program main implicit none real radius real, external :: CircleArea write(*,*) "请输入半径长" read(*,*) radius write(*,"(' 面积= 'F8.3)") CircleArea(radius) stop end program real function CircleArea(radius) implicit none real, parameter :: PI=3.14159 real radius CircleArea = radius*radius*PI return end function3.program main implicit none call bar(3) call bar(10) stop end program subroutine bar(length) implicit none integer, intent(in) :: length integer i character(len=79) :: string string=" " do i=1,length string(i:i)='*' end do write(*,"(A79)") string return end subroutine4.p rogram main implicit none integer, external :: add write(*,*) add(100) end program recursive integer function add(n) result(sum) implicit none integer, intent(in) :: n if ( n<0 ) then sum=0 return else if ( n<=1 ) then sum=n return end if sum = n + add(n-1) return end function5.program main implicit none integer, external :: gcd write(*,*) gcd(18,12) end program integer function gcd(A,B) implicit none integer A,B,BIG,SMALL,TEMP BIG=max(A,B) SMALL=min(A,B) do while( SMALL /= 1 ) TEMP=mod(BIG,SMALL) if ( TEMP==0 ) exit BIG=SMALL SMALL=TEMP end do gcd=SMALL return end function6.program main use TextGraphLib implicit none integer, parameter :: maxx=60, maxy=20 real, parameter :: StartX=0.0, EndX=3.14159*2.0 real, parameter :: xinc = (EndX-StartX)/(maxx-1) real x integer i,px,py call SetScreen(60,20) call SetCurrentChar('*') x=StartX do px=1,maxx py = (maxy/2)*sin(x)+maxy/2+1 callPutChar(px,py) x=x+xinc end docall UpdateScreen() stop end program第九章1.program main implicit none character(len=79) :: filename character(len=79) :: buffer integer, parameter :: fileid = 10 integer count integer :: status = 0 logical alive write(*,*) "Filename:" read (*,"(A79)") filename inquire( file=filename, exist=alive) if ( alive ) then open(unit=fileid, file=filename, & access="sequential", status="old") count = 0 do while(.true.) read(unit=fileid, fmt="(A79)", iostat=status ) buffer if ( status/=0 ) exit ! 没有资料就跳出循环write(*,"(A79)") buffer count = count+1 if ( count==24 ) then pause count = 0 end if end do else write(*,*) TRIM(filename)," doesn't exist." end if stop end2.p rogram main implicit none character(len=79) :: filename character(len=79) :: buffer integer, parameter :: fileid = 10 integer i integer :: status = 0 logical alive write(*,*) "Filename:" read (*,"(A79)") filename inquire( file=filename, exist=alive) if ( alive ) then open(unit=fileid, file=filename, & access="sequential", status="old") do while(.true.) read(unit=fileid, fmt="(A79)", iostat=status ) buffer if ( status/=0 ) exit ! 没有资料就跳出循环do i=1, len_trim(buffer) buffer(i:i) = char( ichar(buffer(i:i))-3 ) end do write(*,"(A70)") buffer end do else write(*,*) TRIM(filename)," doesn't exist." end if stop end3.program main implicit none type student integer chinese, english, math, science, social, total end type type(student) :: s, total integer, parameter :: students=20, subjects=5 integer i open(10,file="grades.bin",access="direct",recl=1) write(*,"(7A10)") "座号","中文","英文","数学","自然","社会","总分" total = student(0,0,0,0,0,0) do i=1, students read(10,rec=(i-1)*subjects+1) s%chinese read(10,rec=(i-1)*subjects+2) s%english read(10,rec=(i-1)*subjects+3) s%math read(10,rec=(i-1)*subjects+4) s%science read(10,rec=(i-1)*subjects+5) s%social s%total = s%chinese+s%english+s%math+s%science+s%social total%chinese = total%chinese+s%chinese total%english = total%english+s%english total%math = total%math+s%math total%science = total%science+s%science total%social = total%social+s%social total%total = total%total+s%total write(*,"(7I10)") i, s end do write(*,"(A10,6F10.3)") "平均", & real(total%chinese)/real(students),& real(total%english)/real(students),& real(total%math)/real(students),& real(total%science)/real(students),& real(total%social)/real(students),& real(total%total)/real(students) stop end4.program main implicit none character(len=79) :: filename character(len=79) :: buffer integer, parameter :: fileid = 10 integer i integer :: status = 0 logical alive write(*,*) "Filename:" read (*,"(A79)") filename inquire( file=filename, exist=alive) if ( alive ) then open(unit=fileid, file=filename, & access="sequential", status="old") do while(.true.) read(unit=fileid, fmt="(A79)", iostat=status ) buffer if ( status/=0 ) exit ! 没有数据就跳出循环do i=1, len_trim(buffer) buffer(i:i) = char( ichar(buffer(i:i))-(mod(i-1,3)+1) ) end do write(*,"(A70)") buffer end do else write(*,*) TRIM(filename)," doesn't exist." end if stop end5.module typedef type student integer :: num integer :: Chinese, English, Math, Natural, Social integer :: total integer :: rank end type end module program main use typedef implicit none integer, parameter :: fileid=10 integer, parameter :: students=20 character(len=80) :: tempstr type(student) :: s(students) ! 储存学生成绩type(student) ::total ! 计算平均分数用integer i, num, error open(fileid, file="grades.txt",status="old", iostat=error) if ( error/=0 ) then write(*,*) "Open grades.txt fail." stop end if read(fileid, "(A80)") tempstr ! 读入第一行文字total=student(0,0,0,0,0,0,0,0) ! 用循环读入每位学生的成绩do i=1,students read(fileid,*) s(i)%num, s(i)%Chinese, s(i)%English, & s(i)%Math, s(i)%Natural, s(i)%Social ! 计算总分s(i)%Total = s(i)%Chinese + s(i)%English + & s(i)%Math + s(i)%Natural + s(i)%Social ! 累加上各科的分数, 计算各科平均时使用total%Chinese = total%Chinese + s(i)%Chinese total%English = total%English + s(i)%English total%Math = total%Math + s(i)%Math total%Natural = total%Natural + s(i)%Natural total%Social = total%Social + s(i)%Social total%Total = total%Total + s(i)%Total end do call sort(s,students) ! 重新输出每位学生成绩write(*,"(8A7)") "座号","中文","英文","数学","自然","社会","总分","名次" do i=1,students write(*,"(8I7)") s(i) end do ! 计算并输出平圴分数write(*,"(A7,6F7.1)") "平均", & real(total%Chinese)/real(students),& real(total%English)/real(students),& real(total%Math) /real(students),& real(total%Natural)/real(students),& real(total%Social) /real(students),& real(total%Total) /real(students) stop end program subroutine sort(s,n) use typedef implicit none integer n type(student) :: s(n), t integer i,j do i=1,n-1 do j=i+1,n if ( s(i)%total < s(j)%total ) then t = s(i) s(i)=s(j) s(j) = t end if end do end do forall(i=1:n) s(i)%rank = i end forall end subroutine第十章1.integer(kind=4) :: a ! 4 bytes real(kind=4) :: b ! 4 bytes real(kind=8) :: c ! 8 bytes character(len=10) :: str ! 10 bytes integer(kind=4), pointer :: pa ! 4 bytes real(kind=4), pointer :: pb ! 4 bytes real(kind=8), pointer :: pc ! 4 bytes character(len=10), pointer :: pstr ! 4 bytes type student integer Chinese, English, Math end type type(student) :: s ! 12 bytes type(student), pointer :: ps ! 4 bytes2.integer, target :: a = 1 integer, target :: b = 2 integer, target :: c = 3 integer, pointer :: p p=>a write(*,*) p ! 1 p=>b write(*,*) p ! 2 p=>c p=5 write(*,*) c ! 53.module linklist type student integer :: num integer :: Chinese, English, Math, Science, Social end type type datalink type(student) :: item type(datalink), pointer :: next end type contains function SearchList(num, head) implicit none integer :: num type(datalink), pointer :: head, p type(datalink), pointer :: SearchList p=>head nullify(SearchList) do while( associated(p) ) if ( p%item%num==num ) then SearchList => p return end if p=>p%next end do return end function end module linklist program ex1016 use linklist implicit none character(len=20) :: filename character(len=80) :: tempstr type(datalink), pointer :: head type(datalink), pointer :: p type(student), allocatable :: s(:) integer i,error,size write(*,*) "filename:" read(*,*) filename open(10, file=filename, status="old", iostat=error) if ( error/=0 ) then write(*,*) "Open file fail!" stop end if allocate(head) nullify(head%next) p=>head size=0 read(10, "(A80)") tempstr ! 读入第一行字符串, 不需要处理它! 读入每一位学生的成绩do while(.true.) read(10,fmt=*, iostat=error) p%item if ( error/=0 ) exit size=size+1 allocate(p%next, stat=error) ! 新增下一个数据if ( error/=0 ) then write(*,*) "Out of memory!" stop end if p=>p%next ! 移动到链表的下一个数据nullify(p%next) end do write(*,"('总共有',I3,'位学生')") size allocate( s(size) ) p=>head do i=1,size s(i)=p%item p=>p%next end do do while(.true.)write(*,*) "要查询几号同学的成绩?" read (*,*) i if ( i<1 .or. i>size ) exit ! 输入不合理的座号write(*,"(5(A6,I3))") "中文",s(i)%Chinese,& "英文",s(i)%English,& "数学",s(i)%Math,& "自然",s(i)%Science,& "社会",s(i)%Social end do write(*,"('座号',I3,'不存在, 程序结束.')") i stop end program4.module typedef implicit none type :: datalink integer :: i type(datalink), pointer :: next end type datalink end module typedef program ex1012 use typedef implicit none type(datalink) , pointer :: p, head, next integer :: i,n,err write(*,*) 'Input N:' read(*,*) n allocate( head ) head%i=1 nullify(head%next) p=>head do i=2,n allocate( p%next, stat=err ) if ( err /= 0 ) then write(*,*) 'Out of memory!' stop end if p=>p%next p%i=i end do nullify(p%next) p=>head do while(associated(p)) write(*, "(i5)" ) p%i p=>p%next end do ! 释放链表的存储空间p=>head do while(associated(p)) next => p%next deallocate(p) p=>next end do stop end program第十一章1.module utility implicit none interface area module procedure CircleArea module procedure RectArea end interface contains real function CircleArea(r) real, parameter :: PI=3.14159 real r CircleArea = r*r*PI return end function real function RectArea(a,b) real a,b RectArea = a*b return end function end module program main use UTILITY implicit none write(*,*) area(1.0) write(*,*) area(2.0,3.0) stop end program2.module time_utility implicit none type :: time integer :: hour,minute,second end type time interface operator(+) module procedure add_time_time end interface contains function add_time_time( a, b ) implicit none type(time) :: add_time_time type(time), intent(in) :: a,b integer :: seconds,minutes,carry seconds=a%second+b%second carry=seconds/60 minutes=a%minute+b%minute+carry carry=minutes/60 add_time_time%second=mod(seconds,60) add_time_time%minute=mod(minutes,60) add_time_time%hour=a%hour+b%hour+carry return end function add_time_time subroutine input( a ) implicit none type(time), intent(out) :: a write(*,*) " Input hours:" read (*,*) a%hour write(*,*) " Input minutes:" read (*,*) a%minute write(*,*) " Input seconds:" read (*,*) a%second return end subroutine input subroutine output( a ) implicit none type(time), intent(in) :: a write(*, "(I3,' hours',I3,' minutes',I3,' seconds')" ) a%hour,a%minute,a%second return end subroutine output end module time_utility program main use time_utility implicit none type(time) :: a,b,c call input(a) call input(b) c=a+b call output(c) stop end program main3.module rational_utility implicit none private public :: rational, & operator(+), operator(-), operator(*),& operator(/), assignment(=),operator(>),& operator(<), operator(==), operator(/=),& output, input type :: rational integer :: num, denom end type rational interface operator(+) module procedure rat__rat_plus_rat end interface interface operator(-) module procedure rat__rat_minus_rat end interface interface operator(*) module procedure rat__rat_times_rat end interface interface operator(/) module procedurerat__rat_div_rat end interface interface assignment(=) module procedure rat_eq_rat module procedure int_eq_rat module procedure real_eq_rat end interface interface operator(>) module procedure rat_gt_rat end interface interface operator(<) module procedure rat_lt_rat end interface interface operator(==) module procedure rat_compare_rat end interface interface operator(/=) module procedure rat_ne_rat end interface contains function rat_gt_rat(a,b) implicit none logical :: rat_gt_rat type(rational), intent(in) :: a,b real :: fa,fb fa=real(a%num)/real(a%denom) fb=real(b%num)/real(b%denom) if ( fa > fb ) then rat_gt_rat=.true. else rat_gt_rat=.false. end if return end function rat_gt_rat function rat_lt_rat(a,b) implicit none logical :: rat_lt_rat type(rational), intent(in) :: a,b real :: fa,fb fa=real(a%num)/real(a%denom) fb=real(b%num)/real(b%denom) if ( fb > fa ) then rat_lt_rat=.true. else rat_lt_rat=.false. end if return end function rat_lt_rat function rat_compare_rat(a,b) implicit none logical :: rat_compare_rat type(rational), intent(in) :: a,b type(rational) :: c c=a-b if ( c%num == 0 ) then rat_compare_rat=.true. else rat_compare_rat=.false. end if return end function rat_compare_rat function rat_ne_rat(a,b) implicit none logical :: rat_ne_rat type(rational), intent(in) :: a,b type(rational) :: c c=a-b if ( c%num==0 ) then rat_ne_rat=.false. else rat_ne_rat=.true. end if return end function rat_ne_rat subroutine rat_eq_rat( rat1, rat2 ) implicit none type(rational), intent(out):: rat1 type(rational), intent(in) :: rat2 rat1%num = rat2%num rat1%denom = rat2%denom return end subroutine rat_eq_rat subroutine int_eq_rat( int, rat ) implicit none integer, intent(out):: int type(rational), intent(in) :: rat int = rat%num / rat%denom return end subroutine int_eq_rat subroutine real_eq_rat( float, rat ) implicit none real, intent(out) :: float type(rational), intent(in) :: rat float = real(rat%num) / real(rat%denom) return end subroutine real_eq_rat function reduse( a ) implicit none type(rational), intent(in) :: a integer :: b type(rational) :: reduse b=gcv_interface(a%num,a%denom) reduse%num = a%num/b reduse%denom = a%denom/b return end function reduse function gcv_interface(a,b) implicit none integer, intent(in) :: a,b integer :: gcv_interface if ( min(a,b) .eq. 0 ) then gcv_interface=1 return end if if (a==b) then gcv_interface=a return else if ( a>b ) then gcv_interface=gcv(a,b) else if ( a<b ) then gcv_interface=gcv(b,a) end if return end function gcv_interface recursive function gcv(a,b) result(ans) implicit none integer, intent(in) :: a,b integer :: m integer :: ans m=mod(a,b) select case(m) case(0) ans=b return case(1) ans=1 return case default ans=gcv(b,m) end select return end function gcv function rat__rat_plus_rat( rat1, rat2 ) implicit none type(rational) :: rat__rat_plus_rat type(rational), intent(in) :: rat1,rat2 type(rational) :: act act%denom= rat1%denom * rat2%denom act%num = rat1%num*rat2%denom + rat2%num*rat1%denom rat__rat_plus_rat = reduse(act) return end function rat__rat_plus_rat function rat__rat_minus_rat( rat1, rat2 ) implicit none type(rational) :: rat__rat_minus_rat type(rational), intent(in) :: rat1, rat2 type(rational) :: temp temp%denom = rat1%denom*rat2%denom temp%num =rat1%num*rat2%denom -rat2%num*rat1%denom rat__rat_minus_rat = reduse( temp ) return end function rat__rat_minus_rat function rat__rat_times_rat( rat1, rat2 ) implicit none type(rational) :: rat__rat_times_rat type(rational), intent(in) :: rat1, rat2 type(rational) :: temp temp%denom = rat1%denom* rat2%denom temp%num = rat1%num * rat2%num rat__rat_times_rat = reduse(temp) return end function rat__rat_times_rat function rat__rat_div_rat( rat1, rat2 ) implicit none type(rational) :: rat__rat_div_rat type(rational), intent(in) :: rat1, rat2 type(rational) :: temp temp%denom = rat1%denom* rat2%num temp%num = rat1%num * rat2%denom rat__rat_div_rat = reduse(temp) return end function rat__rat_div_rat subroutine input(a) implicit none type(rational), intent(out) :: a write(*,*) "分子:" read(*,*) a%num write(*,*) "分母:" read(*,*) a%denom return end subroutine input subroutine output(a) implicit none type(rational), intent(in) :: a if ( a%denom/=1 ) then write(*, "(' (',I3,'/',I3,')' )" ) a%num,a%denom else write(*, "(I3)" ) a%num end if return end subroutine output end module rational_utility program main use rational_utility implicit none type(rational) :: a,b,c call input(a) call input(b) c=a+b write(*,*) "a+b=" call output(c) c=a-b write(*,*) "a-b=" call output(c) c=a*b write(*,*) "a*b=" call output(c) c=a/b write(*,*) "a/b=" call output(c) if (a>b) write(*,*) "a>b" if (a<b) write(*,*) "a<b" if (a==b) write(*,*) "a==b" if (a/=b) write(*,*) "a/=b" stop end program main4.module vector_utility implicit none type vector real x,y end type interface operator(+) module procedure vector_add_vector end interface interface operator(-) module procedure vector_sub_vector end interface interface operator(*) module procedure real_mul_vector module procedure vector_mul_real module procedure vector_dot_vector end interface interface operator(.dot.) module procedure vector_dot_vector end interface contains type(vector) function vector_add_vector(a,b) type(vector), intent(in) :: a,b vector_add_vector = vector(a%x+b%x, a%y+b%y) end function type(vector) function vector_sub_vector(a,b) type(vector), intent(in) :: a,b vector_sub_vector = vector(a%x-b%x, a%y-b%y) end function type(vector) function real_mul_vector(a,b) real, intent(in) :: a type(vector), intent(in) :: b real_mul_vector = vector( a*b%x, a*b%y ) end functiontype(vector) function vector_mul_real(a,b) type(vector), intent(in) :: a real, intent(in) :: b vector_mul_real = real_mul_vector(b,a) end function real function vector_dot_vector(a,b) type(vector), intent(in) :: a,b vector_dot_vector = a%x*b%x + a%y*b%y end function subroutine output(vec) type(vector) :: vec write(*,"('('F6.2','F6.2')')") vec end subroutine end module program main use vector_utility implicit none type(vector) a,b,c a=vector(1.0, 2.0) b=vector(2.0, 1.0) c=a+b call output(c) c=a-b call output(c) write(*,*) a*b end program main。

fortran常用程序

fortran常用程序

fortran常用程序----------------------- Page 1----------------------- Fortran 简单程序集杨洋1!大小写转换program ex39implicit nonecharacter*20 strinteger iprint*,'input the string:'read*,strdo i=1,len_trim(str)if(str(i:i)>='a'.and.str(i:i)<='z') str(i:i)=char(ichar(str(i:i))-32) end doprint*,'the inverted string:'print*,strend!将字符串转化为整数program ex104implicit noneexternal fcharacter*10 strinteger fprint*,'输入由数字组成的字符串:'read*,strprint*,'转化后的整数:'print*,f(str)endfunction f(str)implicit nonecharacter*(*) strinteger f,k,if=0k=len_trim(str)do i=1,kf=f+(ichar(str(i:i))-ichar('0'))*10**(k-i)end doend function!将十进制数转化为二进制数(用字符串保存)program ex1101 作者简介:杨洋,南京信息工程大学大气科学学院海洋科学系2008 级1----------------------- Page 2-----------------------program ex110implicit noneinteger acharacter*8::b=' 'print*,'输入一个十进制整数:'read*,ado while(a>0)if (mod(a,2)==1) thenb='1'//belseb='0'//bend ifa=a/2end doprint*,'对应的二进制数为:'print*,bend!将二进制数(用字符串保存)转化为十进制数program ex111implicit nonecharacter*8 ainteger::b=0,k,iprint*,'输入一个二进制数:'read*,ak=len_trim(a)do i=1,kb=b+(ichar(a(i:i))-ichar('0'))*2**(k-i)end doprint*,'对应的十进制数为:'print*,bend!统计大写、小写、数字及其他字符的个数program ex51implicit nonecharacter*20 strinteger::i,n1=0,n2=0,n3=0,n4=0print*,'输入字符串:'read*,strdo i=1,len_trim(str)select case(str(i:i))case('a':'z')n1=n1+12----------------------- Page 3-----------------------case('A':'Z')n2=n2+1case('0':'9')n3=n3+1case defaultn4=n4+1end selectend doprint*,’大写字母个数:’,n1print*,’小写字母个数:’,n2print*,’数字个数:’,n3print*,’其他字符个数:’,n4End!判断闰年program ex24implicit noneinteger yearprint*,'input a year:'read*,yeark=k+1work(k)=num(i)end ifend docall sort(work,k)print*,'the final array:'8----------------------- Page 9-----------------------print*,(work(i),i=1,k)endfunction prime(n)implicit noneinteger i,k,nlogical primek=sqrt(real(n))do i=2,kif (mod(n,i)==0) exitend doif (i>k) thenprime=.true.elseprime=.false.end ifend functionsubroutine sort(a,n)implicit noneinteger i,j,k,n,a(n),tdo i=1,n-1k=ido j=i+1,nif (a(j)>a(k)) k=jend doif (k/=i) thent=a(k)a(k)=a(i)a(i)=tend ifend doend subroutine! 求2~999 中同时满足下列条件的数:(a) 该数各位数字之和为奇数;(b) 该数是素数。

Fortran计算实例word版本

Fortran计算实例word版本

If(dt.le.1.e-9) then输出”dt太小”并goto 888
goto 999
888 close(10) End
停止运算的 无量纲时间
Subroutine output(tim,d1,u1,t1)
…..
If(tim.eq.0)open(10,…..form=‘unformatted’)
write(10)d,u,t Return end
v u v v v T T 0 t x y y y
T u T v T ( 1)T u ( 1)T v 0
t x y
x
y
2维流体力学方程组,*号已略去
u u 0
t x x
d1 (1)= d (1)
u1(1)= u (1)
t1(1)= t (1)
d1 (im)= d (im)

u1(im)= u (im)

t1(im)= t (im)

tim=tim+dt

dt=1.d0
do 60 i=2,im-1
固定边界条件
局地无量 纲声速
由柯朗条件定 下一时间步长
60
dt=dmin1(dt,dx/(dsqrt(gamma*t1(i))+dabs(u1(i)))
u u u T T 0 t x x x
T u T ( 1)T u 0
t x
x
对1D情况
3. 确定解域、划分网格及设置边界条件

可划分为上 千个格点
1
模拟区域: 网 格:
0
1 x 1 x(i) 2(i 1) /(N 1) 1

fortran95第七章例题程序

fortran95第七章例题程序

0701一维数组program mainimplicit noneinteger,parameter::students=5integer::student(students)integer i!录入学生成绩do i=1,studentswrite(*,"('number',I2)")iread(*,*)student(i)end do!提取学生成绩,并规定读取范围do while(.true.)write(*,*)"Query"read(*,*)iif(i<=0 .or. i>students) exitwrite(*,*)student(i)end dostopend0702program mainimplicit noneinteger::student1,student2,student3,student4,student5 integer i!录入数据write(*,*)'number1'read(*,*)student1write(*,*)'number2'read(*,*)student2write(*,*)'number3'read(*,*)student3write(*,*)'number4'read(*,*)student4write(*,*)'number5'read(*,*)student5!提取数据do while(.true.)write(*,*)'Query'read(*,*)iselect case(i)case(1)write(*,*)student1case(2)write(*,*)student2case(3)write(*,*)student3case(4)write(*,*)student4case(5)write(*,*)student5case defaultexitend selectend dostopend0703二维数组program mainimplicit noneinteger,parameter::classes=5integer,parameter::students=5 integer::student(students,classes)integer sinteger cdo c=1,classesdo s=1,studentswrite(*,"('Number',I2,'of class',I2)")s,c read(*,*)student(s,c)end doend dodo while(.true.)write(*,*)'class:'read(*,*)cif (c<=0 .or. c>classes) exitwrite(*,*)'student:'read(*,*)sif(s<=0 .or. s>students) exitwrite(*,"('score',I3)")student(s,c)end dostopend!s,c不能超过声明大小,否则程序会出错0704program mainimplicit noneinteger,parameter::row=2integer,parameter::col=2integer::matrixA(row,col)integer::matrixB(row,col)integer::matrixC(row,col)integer rinteger c!读入A矩阵的内容write(*,*)'Matrix A'do r=1,rowdo c=1,colwrite(*,"('A(',I1,',',I1,')=')")r,cwrite(*,*)'Matrix B'do r=1,rowdo c=1,colwrite(*,"('B(',I1,',',I1,')=')")r,cread(*,*)matrixB(r,c) !录入,记录数据end doend do!A+Bwrite(*,*)'Matrix A+B='do r=1,rowdo c=1,colmatrixC(r,c)=matrixA(r,c)+matrixB(r,c) write(*,"('(',I1,',',I1,')=',I3)")r,c,matrixC(r,c) end doend dostopend0705program ex0705implicit noneinteger, parameter :: row = 2integer, parameter :: col = 2integer :: matrix(row, col, 3)integer minteger rinteger cdo m=1, 2write(*,"('Matrix ',I1)") mdo r=1, rowdo c=1, colwrite(*,"('(',I1,',',I1,')=')") r,cread(*,*) matrix(r,c,m)end doend doend dowrite(*,*) "Matrix 1 + Matrix 2 = "do r=1, rowdo c=1, colmatrix(r,c,3) = matrix(r,c,1)+matrix(r,c,2)write(*,"('(',I1,',',I1,')=',I3)") r,c,matrix(r,c,3) end doend dostopend0706program mainimplicit noneinteger,parameter::students=5integer::student(students)=(/80,90,85,75,95/) integer ido while(.true.)write(*,*)'Query:'read(*,*)i !读入学生序号if (i<=0.or.i>students) exitwrite(*,*)student(i) !写出学生成绩end dostopend0707program mainimplicit noneinteger,parameter::row=2integer,parameter::col=2integer::m(row,col)integer rinteger cdata((m(r,c),r=1,2),c=1,2)/1,2,3,4/write(*,"(I3,I3,/,I3,I3)")((m(r,c),c=1,2),r=1,2) stopend0708program ex0708implicit noneinteger, parameter :: row = 2integer, parameter :: col = 2integer :: ma(row, col) = 1integer :: mb(row, col) = 4integer :: mc(row, col)integer :: i,jmc = ma + mbwrite(*,"(I3,I3,/,I3,I3)") ((mc(i,j), i=1,2),j=1,2)stopend0709program ex0709implicit noneinteger, 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)=4 integer :: b(4)=(/ 5,6,7,8 /)integer :: c(2)write(*,*) a ! a(1,1), a(2,1), a(1,2), a(2,2) write(*,*) a(:,1) ! a(1,1), a(1,2)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)stopend0710program ex0710implicit noneinteger :: iinteger :: a(5)=(/ (i,i=1,5) /) integer :: b(5)=0!把b中小于3的元素赋值给a where( a<3 )b = aend wherewrite(*,"(5(I3,1X))") bstopendprogram mainimplicit noneinteger::iinteger::a(5)=(/1,2,3,4,5/) integer::b(5)=0where(a<3) b=awrite(*,"(5(I3,1x))")bstopend0711program ex0711implicit noneinteger :: iinteger :: a(5)=(/ (i,i=1,5) /) integer :: b(5)=0where( a<3 )b = 1elsewhereb = 2end wherewrite(*,"(5(I3,1X))") bstopend0712program ex0712implicit noneinteger :: ireal :: income(10)=(/ 250000, 300000, 500000, 400000, 350000, &600000, 270000, 450000, 280000, 700000 /) real :: tex(10)=0where( income < 300000.0 )tex = income*0.10elsewhere( income < 500000.0 )tex = income*0.12elsewheretex = income*0.15end wherewrite(*,"(10(F8.1,1X))") texstopend0713program ex0713implicit noneinteger iinteger :: a(5)forall(i=1:5)a(i)=5end forall! a(1)=a(2)=a(3)=a(4)=a(5)=5write(*,*) aforall(i=1:5)a(i)=iend forall! a(1)=1, a(2)=2, a(3)=3, a(4)=4, a(5)=5write(*,*) astopend0714program ex0714implicit noneinteger 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)=3write(*,"(5(5I5,/))") astopend0715program ex0715implicit noneinteger, parameter :: max = 1000 integer :: a(max)integer :: studentsinteger :: iwrite(*,*) "How many students:"read(*,*) studentsdo i=1,studentswrite(*,"('Number ',I3)") iread(*,*) a(i)end dostopend0716program ex0716implicit noneinteger :: studentsinteger, allocatable :: a(:)integer :: iwrite(*,*) "How many students:"read(*,*) studentsallocate( a(students) )do i=1,studentswrite(*,"('Number ',I3)") iread(*,*) a(i)end dostopend0717program ex0717implicit noneinteger :: size, error=0integer, parameter :: one_mb=1024*1024 character, allocatable :: a(:)do while( .true. )size=size+one_mballocate( a(size), stat=error )if ( error/=0 ) exitwrite(*,"('Allocate ',I10, ' bytes')") sizewrite(*,"(F10.2,' MB used')") real(size)/real(one_mb) deallocate( a )end dostopend0718program ex0718implicit noneinteger, parameter :: size=10integer :: a(size) = (/ 5,3,6,4,8,7,1,9,2,10 /)integer :: i,jinteger :: tdo i=1, size-1do j=i+1, sizeif ( a(i) > a(j) ) thent=a(i)a(i)=a(j)a(j)=tend ifend doend dowrite(*,"(10I4)") astopend0719program ex0719implicit noneinteger, parameter :: L=3, M=4, N=2real :: A(L,M) = (/ 1,2,3,4,5,6,7,8,9,10,11,12 /) real :: B(M,N) = (/ 1,2,3,4,5,6,7,8 /)real :: C(L,N)integer :: i,j,kdo i=1,Ldo j=1,NC(i,j) = 0.0do k=1,MC(i,j) = C(i,j)+A(i,k)*B(k,j)end doend doend dodo i=1,Lwrite(*,*) C(i,:)end dostopend。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1)实例3—求多个半径下的圆周长
! z3.f90 --Fortran95
! FUNCTIONS:
! z3 - Entry point of console application.
!************************************************************************* ! PROGRAM: z3
! PURPOSE: Entry point for the console application.
!************************************************************************ program z3
! 求多个半径下的圆周长
! 主程序
! PROGRAM Z3
PRINT *, 'R=',1.2,'C=',C(1.2)
PRINT *, 'R=',3.4,'C=',C(3.4)
PRINT *, 'R=',15.6,'C=',C(15.6)
PRINT *, 'R=',567.3,'C=',C(567.3)
END program z3
!子程序
FUNCTION C(R)
PI=3.1415926
C=2*PI*R
RETURN
! Body of z3
end
2)实例4—键盘与显示器输入/输出
a)Fortran 基本操作
b)程序指令
! ZXZ_I_O.f90
! FUNCTIONS:
! ZXZ_I_O - Entry point of console application.
! PROGRAM: ZXZ_I_O
! PURPOSE: Entry point for the console application.
!***************输入、输出样式种种************************** program ZXZ_I_O
implicit none
!变量声明的位置
INTEGER(2) i; INTEGER(4) j; INTEGER(4) m; REAL n
INTEGER A,B
! Variables
PRINT*,'输入整数A'; READ*, A
PRINT*,'输入整数B'; READ*, B
B=A+B
PRINT*,'B=A+B=',B
WRITE(*,*) 'A*B=',A*B
PRINT* ,'以上为计算机的计算结果,注意B的值'
!系统默认的输出样式
PRINT* ,'系统默认的输出样式'
!人为控制的的输出样式--格式化输出
i=21; j=53; m=5
n=(i+j*m*i**m)
WRITE(*,*) 'i,j,m 是常量,程序赋初值' PRINT*, i,j,m
WRITE(*,*) 'i,j,m 的计算结果:' PRINT*,'i+j*m*i**m=',n
PRINT* ,' '
! Body of ZXZ_I_O
end program ZXZ_I_O
程序说明:
程序赋值—初始化
i=21; j=53; m=5
键盘无格式输入
READ*, A
键盘有格式输入
READ(*, 100)A,B,C
100 FORMA T( 2F5.2,F5.3)
显示器无格式输出
PRINT* ,'系统默认的输出样式' WRITE(*,*) 'A*B=',A*B
显示器有格式输出
PRINT 100 ,A+B
WRITE(*,100) 'A*B=',A*B
100 FORMA T( F5.2)
c)调试运行
d)程序指令
带格式的输入输出
! ZXZ_I_O.f90
! FUNCTIONS:
! ZXZ_I_O - Entry point of console application.
! PROGRAM: ZXZ_I_O
! PURPOSE: Entry point for the console application.
!***************输入、输出样式种种************************** program ZXZ_I_O
implicit none
!变量声明的位置
INTEGER(2) i; INTEGER(4) j; INTEGER(4) m; REAL n
INTEGER A,B
REAL X,Y,Z
! Variables
PRINT*,'输入整数A'; READ*, A
PRINT*,'输入整数B'; READ*, B
PRINT*, '计算结果为:'
B=A+B
PRINT*,'B=A+B=',B
WRITE(*,*) 'A*B=',A*B
PRINT* ,'以上为计算机的计算结果,注意B的值'
!系统默认的输出样式
PRINT* ,'系统默认的输出样式'
PRINT*,'输入实数X'; READ(*,100) X
PRINT*,'输入实数Y'; READ(*,100) Y
100 FORMA T(F5.2)
PRINT*, '计算结果为:'
Z=X+Y
PRINT 200,Z
200 FORMA T(4X,'Z=X+Y=',F8.3)
WRITE(*,*)
WRITE(*,300) X*Y
300 FORMA T(4X,'Z=X*Y=',F8.3)
!人为控制的的输出样式--格式化输出
PRINT* ,'程序为常量赋了初值'
i=21; j=53; m=5
n=(i+j*m*i**m)
WRITE(*,*) 'i,j,m 是常量,程序赋初值'
PRINT*, i,j,m
WRITE(*,*) 'i,j,m 的计算结果:'
PRINT*,'i+j*m*i**m=',n
PRINT* ,' '
! Body of ZXZ_I_O
end program ZXZ_I_O e)调试运行。

相关文档
最新文档