fortran习题4答案

合集下载

fortran编程习题答案

fortran编程习题答案

fortran编程习题答案Fortran编程习题答案Fortran是一种古老而强大的编程语言,广泛应用于科学计算和工程领域。

在学习Fortran编程的过程中,解决习题是一种非常有效的方法。

本文将为您提供一些Fortran编程习题的答案,帮助您更好地理解和掌握这门语言。

1. 习题一:编写一个Fortran程序,计算并输出1到100之间所有整数的平方。

程序代码如下:```fortranprogram squareimplicit noneinteger :: ido i = 1, 100print *, i, i**2end doend program square```2. 习题二:编写一个Fortran程序,计算并输出斐波那契数列的前20个数。

程序代码如下:```fortranprogram fibonacciimplicit noneinteger :: i, n, fib(20)fib(1) = 0fib(2) = 1do i = 3, 20fib(i) = fib(i-1) + fib(i-2)end dodo i = 1, 20print *, fib(i)end doend program fibonacci```3. 习题三:编写一个Fortran程序,计算并输出一个给定整数的阶乘。

程序代码如下:```fortranprogram factorialimplicit noneinteger :: i, n, resultprint *, "请输入一个整数:"read *, nresult = 1do i = 1, nresult = result * iend doprint *, n, "的阶乘为:", resultend program factorial```4. 习题四:编写一个Fortran程序,计算并输出一个给定整数是否为素数。

程序代码如下:```fortranprogram primeimplicit noneinteger :: i, n, countprint *, "请输入一个整数:"read *, ncount = 0do i = 2, n-1if (mod(n, i) == 0) thencount = count + 1end ifend doif (count == 0) thenprint *, n, "是素数"elseprint *, n, "不是素数"end ifend program prime```5. 习题五:编写一个Fortran程序,计算并输出一个给定整数的所有因子。

FORTRAN习题答案

FORTRAN习题答案

FORTRAN习题答案习题⼆⼀、问答题1. 给出下⾯变量名称,哪些是合法变量?哪些是⾮法变量?说明原因。

Count 、num_2、x&y 、4x+5y 、china-suzhou 、$us 、AbCdE 、Mr.bai 、t5、_another 、school_class_25、#125、2002Y 、π、β、A01/02、alpha 、date(1) 1. 判定下⾯整数,指出哪些是合法整数,哪些是⾮法整数?说明原因。

-0、+ 215、$125、3,245,895、5.3245、5#384、-524_3、#5DFE 、23-345、16#1A2B 、38#ABCD 、8#275_2、+327890、4 #3212. 判定下⾯实数,指出哪些是合法实数,哪些是⾮法实数?说明原因。

-0E2、45.2345E3.5、-5489E25_8、-.2345E-35、$185.45E 、+ 2.753425E24_3、 58D85、+0.E-0、-00000.001E5、5,443,223.44、-12 34E+2、+ 18.5E 18、2.5E42习题三⼀、选择题1.下⾯是V isual Fortran 中正确的表达式是。

(A )A*COS(X)+∣B ∣(B )2*EXP(2*X)/SQRT(16.0)(C )B 2-4AC (D )MOD (24.5,0.5)2.下⾯算术赋值语句中正确的语句是。

(A )M*N=(X-Y)/Z (B )+R=A+B/C(C )X=Y=Z-1.0 (D )Y=A*B/C/D3.算术表达式1/3+2/3的值为。

(A )0 (B ) 1 (C ) 0.99999999 (D )值不确定⼆、问答题1. 将下列代数式⽤Visual Fortran 表达式描述:①②③ 4sin 3A-3sinA+sin3A ④ 2.执⾏下列赋值语句后,变量中的值。

变量的类型遵循I —N 规则。

fortran课后习题

fortran课后习题

5.3 program main implicit none integer nian,shouru real suijin read(*,*) nian read(*,*) shouru if (nian<50.and.nian>0) then if (shouru>=1000 .and. shouru<=5000) then suijin=shouru*0.1 end if if (shouru>5000) then suijin=shouru*0.15 end if if (shouru<1000) then suijin=shouru*0.03 end if else if (nian>50) then if (shouru>=1000 .and. shouru<=5000) then suijin=shouru*0.07 end if if (shouru>5000) then suijin=shouru*0.1 end if if (shouru<1000) then suijin=shouru*0.05 end if end if write(*,*)"应缴纳的税金金额为:" write(*,*) suijin end 5.4 program main implicit none integer day write(*,*) "请输入一个公元的年份:" read(*,*) day if (mod(day,4)==0) then
write(*,*)"一年有366天" else if(mod(day,100)==0.and.mod(day,400)==0) then write(*,*)"一年有366天" else write(*,*)"一年有365天" end if End 6.1 method one: program main implicit none integer counter do counter=1,5 write(*,*)"Fortran" end do end method two: program main implicit none integer counter counter=1 do while(counter<=5) write(*,*)"Fortran" counter=counter+1 end do end 6.2 program main implicit none integer counter integer::ans=0 counter=1 do while(counter<100) ans=ans+counter counter=counter+2 end do

Fortran95程序设计习题答案

Fortran95程序设计习题答案

Fortran95程序设计习题答案第四章 1.program main implicit none write(*,*) "Have a good time." write(*,*) "That's not bad." write(*,*) '"Mary" isn''t my name.' end program 2.program main real, parameter :: PI=3 implicit none.14159real radius write(*,*) "请输入半径长" read(*,*) radius write(*,"(' 面积='f8. 3)") radius*radius*PI end program 3.program main implicit none real grades write(*,*) "请输入成绩" read(*,*)grades write(*,"(' 调整后成绩为 'f8.3)") SQRT(grades)*10.0 end program 4.integer a,b real ra,rb a=2 b=3 ra=2.0 rb=3.0 write(*,*) b/a ! 输出1, 因为使用整数计算, 小数部分会无条件舍去 write(*,*) rb/ra ! 输出1.5 5.program 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 program 2.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 program 3.program main implicit none integer age, money real tax write(*,*) "请输入年龄"write(*,*) "请输入月收入" read(*,*) money if ( age<50 ) thenread(*,*) ageif ( 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 ifwrite(*,"(' 税金为 'I8)") nint(money*tax) end program 4.program main implicit none integer year, days logical mod_4, mod_100, mod_400write(*,*) "请输入年份" 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+item end 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 mainimplicit 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 integerc(3,4,5,6) ! 3*4*5*6=360 integer d(-5:5) ! 11 integer e(-3:3, -3:3) ! 7*7=49 3.program main implicit none integer, parameter :: max=10integer 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 program 4.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 subroutine 2.program main implicit nonereal 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 returnend function 3.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 subroutine 4.program main implicit none integer, external :: add write(*,*)add(100) end program recursive integer function add(n)integer, intent(in) :: n if ( n<0 ) then sum=0 return elseresult(sum) implicit noneif ( n<=1 ) then sum=n return end if sum = n + add(n-1) return end function 5.program main implicit none integer, external :: gcdwrite(*,*) 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) dowhile( SMALL /= 1 )TEMP=mod(BIG,SMALL) if ( TEMP==0 ) exit BIG=SMALL SMALL=TEMP enddo gcd=SMALL return end function 6.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 call PutChar(px,py) x=x+xinc end docall UpdateScreen() stop end program 第九章 1.program main implicitnone character(len=79) :: filename character(len=79) :: buffer integer, parameter :: fileid = 10 integer count integer :: status = 0 logical alive write(*,*) "Filename:" read (*,"(A79)") filenameinquire( file=filename, exist=alive) if ( alive ) then open(unit=fileid, file=filename, & access="sequential", status="old") count = 0 dowhile(.true.) read(unit=fileid, fmt="(A79)", iostat=status ) bufferif ( status/=0 ) exit ! 没有资料就跳出循环 write(*,"(A79)") buffercount = count+1 if ( count==24 ) then pause count = 0 end if end do else write(*,*) TRIM(filename)," doesn't exist." end if stop end2.program main implicit none character(len=79) :: filenamecharacter(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 enddo 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 iopen(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%scienceread(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%englishtotal%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 end 4.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) pen(unit=fileid, file=filename, & access="sequential", if ( alive ) then ostatus="old") do while(.true.) read(unit=fileid, fmt="(A79)",iostat=status ) buffer if ( status/=0 ) exit ! 没有数据就跳出循环 doi=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 end 5.module typedef typestudent 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) :: tempstrtype(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)%Englishtotal%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 ntype(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) ::4 bytes real(kind=4) :: b ! 4 bytes real(kind=8) :: c ! 8 bytes character(len=10) :: a !str ! 10 bytes integer(kind=4), pointer :: pa ! 4 bytesreal(kind=4), pointer :: pb ! 4 bytes real(kind=8), pointer :: pc ! 4 bytes character(len=10), pointer :: pstr ! 4 bytes type studentinteger Chinese, English, Math end type type(student) :: s ! 12 bytes type(student), pointer :: ps ! 4 bytes 2.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=>headnullify(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 nonecharacter(len=20) :: filename character(len=80) :: tempstrtype(datalink), pointer :: head type(datalink), pointer :: ptype(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%itemp=>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 program 4.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, nextinteger :: 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 endif p=>p%next p%i=i end do nullify(p%next) p=>head dowhile(associated(p)) write(*, "(i5)" ) p%i p=>p%next end do ! 释放链表的存储空间 p=>head do while(associated(p)) next => p%nextdeallocate(p) p=>next end do stop end program 第十一章 1.moduleutility implicit none interface area module procedure CircleArea module procedure RectArea end interface contains real function CircleArea(r) real, parameter :: PI=3.14159 real rCircleArea = 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 program 2.module time_utility implicit none type :: timeinteger :: hour,minute,second end type time interface operator(+) module procedure add_time_time end interface contains functionadd_time_time( a, b ) implicit none type(time) :: add_time_timetype(time), intent(in) :: a,b integer :: seconds,minutes,carryseconds=a%second+b%second carry=seconds/60minutes=a%minute+b%minute+carry carry=minutes/60add_time_time%second=mod(seconds,60)add_time_time%minute=mod(minutes,60)add_time_time%hour=a%hour+b%hour+carry return end functionadd_time_time subroutine input( a ) implicit none type(time),intent(out) :: a write(*,*) " Input hours:" read (*,*) a%hourwrite(*,*) " 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 usetime_utility implicit none type(time) :: a,b,c call input(a) callinput(b) c=a+b call output(c) stop end program main 3.modulerational_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 interfaceoperator(*) module procedure rat__rat_times_rat end interfaceinterface operator(/) module procedure rat__rat_div_rat end interface interface assignment(=) module procedure rat_eq_rat module procedureint_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 containsfunction rat_gt_rat(a,b) implicit none logical :: rat_gt_rattype(rational), intent(in) :: a,b real :: fa,fbfa=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_ratfunction rat_lt_rat(a,b) implicit none logical :: rat_lt_rattype(rational), intent(in) :: a,b real :: fa,fbfa=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 nonelogical :: rat_compare_rat type(rational), intent(in) :: a,btype(rational) :: c c=a-b if ( c%num == 0 ) thenrat_compare_rat=.true. else rat_compare_rat=.false. end if returnend function rat_compare_rat function rat_ne_rat(a,b) implicit none logical :: rat_ne_rat type(rational), intent(in) :: a,btype(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_ratsubroutine rat_eq_rat( rat1, rat2 ) implicitnone 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 subroutinereal_eq_rat( float, rat ) implicit none real, intent(out) :: floattype(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 :: btype(rational) :: reduse b=gcv_interface(a%num,a%denom) reduse%num =a%num/b reduse%denom = a%denom/b return end function reduse functiongcv_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 ) thengcv_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 returncase(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 functionrat__rat_minus_rat( rat1, rat2 ) implicit none type(rational) ::rat__rat_minus_rat type(rational), intent(in) :: rat1, rat2type(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_ratfunction rat__rat_times_rat( rat1, rat2 ) implicit nonetype(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_rattype(rational), intent(in) :: rat1, rat2 type(rational) :: temptemp%denom = rat1%denom* rat2%num temp%num = rat1%num * rat2%denomrat__rat_div_rat = reduse(temp) return end function rat__rat_div_rat subroutine input(a) implicit none type(rational), intent(out) :: awrite(*,*) "分子:" 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-bwrite(*,*) "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 main 4.module vector_utility implicit none type vector real x,y end type interface operator(+) module procedurevector_add_vector end interface interface operator(-) module procedurevector_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) functionvector_add_vector(a,b) type(vector), intent(in) :: a,bvector_add_vector = vector(a%x+b%x, a%y+b%y) end function type(vector) functionvector_sub_vector(a,b) type(vector), intent(in) :: a,bvector_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) functionvector_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,bvector_dot_vector = a%x*b%x + a%y*b%y end function subroutineoutput(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课后习题答案Fortran课后习题答案在学习Fortran编程语言时,课后习题是巩固知识、提高编程能力的重要途径。

通过解答课后习题,学生可以加深对Fortran语法和逻辑的理解,提高编程实践能力。

以下是一些Fortran课后习题答案,供大家参考。

1. 编写一个Fortran程序,计算并输出1到100的所有偶数的和。

程序代码如下:```fortranprogram sum_even_numbersimplicit noneinteger :: i, sumsum = 0do i = 2, 100, 2sum = sum + iend doprint *, 'The sum of even numbers from 1 to 100 is:', sumend program sum_even_numbers```2. 编写一个Fortran程序,找出一个整数数组中的最大值和最小值,并输出它们的位置。

程序代码如下:```fortranprogram find_max_minimplicit noneinteger :: i, n, max_val, min_val, max_pos, min_pos integer, dimension(10) :: arr! 初始化数组arr = (/3, 7, 2, 8, 5, 10, 1, 6, 4, 9/)! 初始化最大值和最小值max_val = arr(1)min_val = arr(1)max_pos = 1min_pos = 1! 找出最大值和最小值do i = 2, 10if (arr(i) > max_val) thenmax_val = arr(i)max_pos = iendifif (arr(i) < min_val) thenmin_val = arr(i)min_pos = iendifend doprint *, 'The maximum value is', max_val, 'at position', max_posprint *, 'The minimum value is', min_val, 'at position', min_posend program find_max_min```通过这些课后习题的答案,我们可以看到Fortran语言的一些基本特性和常用语法的运用。

fortran试题

fortran试题

软件技术基础(FORTRAN)笔试模拟试题(带答案)一.概念选择题(单选题,将正确的答案填入括号内。

每题1分,共15分)1. 已知整型变量L=5,M=13,N=4,表达式L*M/N 、L/N*M 和M/N*L 的值分别是(A )。

A )16 13 15B )16.25 16.25 16.25C )16.0 13.0 15.0D )16 16 162.将数学式)||(ln 215-+e x 改写为FORTRAN 表达式,正确的是( D )。

A) 1/2*(LOG(ABS(X))+E**(-5))B) 1.0/2*(LN(ABS(X))+E**-5)C) 1/2.0*(LN(ABS(X))+E -5)D) 1.0/2*(LOG(ABS(X))+EXP(-5))3. 设A=.TRUE., B=.FALSE., C=.FALSE., 逻辑表达式(A.OR.B).AND.C 与A.OR..NOT.B 的值是( B )。

A) .T. 与 .F. B).F . 与 .T. C) .T. 与 .T. D) .F. 与 .F .4. 数组REAL A (0:5,-1:5)的元素个数是 ( D )。

A )25B )30C )36D )42 5. 下列数组说明符中错误的是( A )。

A )K(3:2,1:2)B ) K(-1:1,2)C )K(-2:2,-1:1)D )K(2:2,5)6. 关于USE 语句的正确说法是( C )。

A)USE 语句是可执行语句 B)用USE 语句可以代替INCLUDE 语句C)USE 语句必须放在程序单元体的开头 D)USE 语句的位置任意7. 语句OPEN (9,FILE=‘DATA.TXT ’)打开的是 ( B ) 文件。

A )无格式顺序B )有格式顺序C )无格式直接D )有格式直接8. 阅读下列FORTRAN 程序, 程序运行时执行循环体的次数是( A )。

X=0DO L=10, 5, 1X=X+1.0ENDDOENDA) 0 B) 1 C) 6 D) 无穷9.结构化程序设计中有三种基本结构,下列不属于三种基本设计结构的是( D )。

Fortran程序设计课后习题答案方便

Fortran程序设计课后习题答案方便

第四章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 = 365end 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 ) thenwrite(*,*) "猜对了" else write(*,*) "猜错了" end if stop end program4.program main implicit none integer, parameter :: max=10 integer i realitem real ans ans = 1.0 item = 1.0 do i=2,max item = item/real(i)ans = ans+item end 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-1do 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 nonereal, parameter :: PI=3.14159 real radius, area area = radius*radius*PIreturn end subroutine2.program main implicit none real radius real, external :: CircleAreawrite(*,*) "请输入半径长" 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 integeri 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 integerA,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 dogcd=SMALL return end function 6.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=StartXdo px=1,maxx py = (maxy/2)*sin(x)+maxy/2+1 call PutChar(px,py) x=x+xincend 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 endif end do else write(*,*) TRIM(filename)," doesn't exist." end ifstop end2.p rogram main implicit none character(len=79) :: filename character(len=79) :: buffer integer, parameter :: fileid = 10 integer iinteger :: 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 ) bufferif ( status/=0 ) exit ! 没有资料就跳出循环do i=1, len_trim(buffer) buffer(i:i) = char( ichar(buffer(i:i))-3 ) end do write(*,"(A70)") bufferend 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 end 4.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 ifend 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 programex1016 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 stopend program4.module typedef implicit none type :: datalink integer :: i type(datalink), pointer :: next end type datalink end module typedef programex1012 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 procedureadd_time_time end interface contains function add_time_time( a, b ) implicit none type(time) :: add_time_time type(time), intent(in) :: a,binteger :: seconds,minutes,carry seconds=a%second+b%second carry=seconds/60 minutes=a%minute+b%minute+carry carry=minutes/60add_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 nonetype(time), intent(in) :: a write(*, "(I3,' hours',I3,' minutes',I3,'seconds')" ) a%hour,a%minute,a%second return end subroutine output endmodule time_utility program main use time_utility implicit nonetype(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 procedurerat__rat_plus_rat end interface interface operator(-) module procedurerat__rat_minus_rat end interface interface operator(*) module procedurerat__rat_times_rat end interface interface operator(/) module procedurerat__rat_div_rat end interface interface assignment(=) module procedurerat_eq_rat module procedure int_eq_rat module procedure real_eq_rat endinterface interface operator(>) module procedure rat_gt_rat endinterface interface operator(<) module procedure rat_lt_rat endinterface interface operator(==) module procedure rat_compare_rat endinterface interface operator(/=) module procedure rat_ne_rat endinterface contains function rat_gt_rat(a,b) implicit none logical ::rat_gt_rat type(rational), intent(in) :: a,b real :: fa,fbfa=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,fbfa=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) implicitnone logical :: rat_compare_rat type(rational), intent(in) :: a,btype(rational) :: c c=a-b if ( c%num == 0 ) thenrat_compare_rat=.true. else rat_compare_rat=.false. end if return end function rat_compare_rat function rat_ne_rat(a,b) implicitnone logical :: rat_ne_rat type(rational), intent(in) :: a,btype(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_ratsubroutine int_eq_rat( int, rat ) implicit none integer, intent(out):: inttype(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) implicitnone 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 ) implicitnone 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 functionrat__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 outputend 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,ca=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。

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

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

第四章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=3implicit none.14159real 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.program 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.54write(*,"(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.03else if ( money<5000) then tax=0.1else tax=0.15end 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.03else if ( money<5000 )then tax = 0.10else tax = 0.15end if else if(money<1000) then tax= 0.5else if( money<5000 )then tax = 0.7else tax = 0.10end 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 = 366else days = 365end if write(*,"('这一年有 'I3' 天 ')") days stop end program第六章1.program main implicit none integer i do i=1,5write(*,*)"Fortran"end do stop end program2.program main implicit none integer i,sum sum = 0do i=1,99,2sum = sum+i end do write(*,*) sum stop end program3.program main implicit none integer,parameter:: answer= 45integer,parameter :: max = 5integer 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=10integer i real item real ans ans = 1.0item = 1.0do i=2,max item = item/real(i)ans = ans+itemend do write(*,*) ans stop end program5.program main implicit none integer, parameter :: length = 79character(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+1end if end do write(*,"(A79)") output stop end program第七章1.program main implicit none integer,parameter:: max= 10integer 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=25integer b(2,3,4)!2*3*4=24integer 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=10integer f(max)integer i f(1)=0f(2)=1do 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=10integer:: a(size)=(/ 5,3,6,4,8,7,1,9,2,10/)integer::i,j integer::t do i=1, size-1do 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)=7a(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.program 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=0return 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=20real,parameter:: StartX=0.0,EndX=3.14159*2.0real,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+1callPutChar(px,py)x=x+xinc end docall UpdateScreen()stop end program第九章1.program main implicit none character(len=79)::character(len=79)::buffer integer,parameter:: fileid = 10integer count integer::status=0logical alive write(*,*)":"read(*,"(A79)")inquire(,exist=alive)if(alive)then open(unit=fileid, , &access="sequential", status="old")count = 0do while(.true.) read(unit=fileid, fmt="(A79)", iostat=status ) buffer if ( status/=0 ) exit!没有资料就跳出循环write(*,"(A79)")buffer count=count+1if( count==24)then pause count=0end if end do else write(*,*)TRIM(),"doesn't exist." end if stop end2.program main implicit none character(len=79)::character(len=79)::buffer integer, parameter :: fileid = 10integer i integer :: status = 0logical alive write(*,*) ":" read(*,"(A79)")inquire(, exist=alive)if (alive )then open(unit=fileid,,& 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()," 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=5integer 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)::character(len=79)::buffer integer, parameter :: fileid = 10integer i integer :: status = 0logical alive write(*,*) ":"read (*,"(A79)")inquire( , exist=alive)if ( alive ) then open(unit=fileid, , & 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()," 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::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 -1do 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! 4bytes real(kind=4):: b! 4 bytes real(kind=8):: c!8bytes character(len=10):: str!10bytes 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=>awrite(*,*) 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 ex1016use linklist implicit none character(len=20)::character(len=80):: tempstr type(datalink),pointer::head type(datalink),pointer:: p type(student), allocatable :: s(:)integer i,error,size write(*,*) ":"read(*,*)open(10, , status="old", iostat=error)if ( error/=0 ) then write(*,*) "Open !"stop end if allocate(head) nullify(head%next)p=>head size=0read(10, "(A80)") tempstr !读入第一行字符串 ,不需要处理它!读入每一位学生的成绩do while(.true.)read(10,fmt=*,iostat=error) p%item if( error/=0) exit size=size+1allocate(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 ex1012use typedef implicit none type(datalink) , pointer :: p, head, next integer :: i,n,err write(*,*) 'Input N:'read(*,*) n allocate( head )head%i=1nullify(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.14159real 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 procedure rat__rat_div_rat end interface interface assignment(=)module procedure rat_eq_rat module procedure int_eq_rat module procedure real_eq_rat endinterface 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)::rat1type(rational),intent(in)::rat2rat1%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=1return 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=1return 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,rat2type(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, rat2type(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复习Fortran 复习题⽬类型:1、选择题2、填空题3、简答题4、编程题样题⼀、选择题1.下列名称中,符合FORTRAN 90命名规则的是。

A. a-b-cB. $123C. length_1D. date.year 2.关于FORTRAN 90程序单元,以下四种说法中正确的是。

A.⼀个FORTRAN 90程序由多个程序单元组成,其中有⼀个且只能有⼀个主程序单元B. ⼀个FORTRAN 90程序由多个程序单元组成,其中有⼀个且只能有⼀个模块单元C. ⼀个FORTRAN 90程序允许有多个程序单元,但每类程序单元只能有⼀个D. ⼀个FORTRAN 90程序允许有多个程序单元,但主程序单元和模块单元只能有⼀个3.关于FORTRAN 90的CASE结构,以下说法中正确的是。

A. CASE结构内必须取DEFAULT作为情况选择器的最终值结构的情况表达式不能是字符类型结构的情况表达式和情况选择器的取值类型必须相同结构的不同情况选择器取值可以重复4.已知数组说明REAL A(20:22,0:9,-3:-1),数组A的数组元素个数是。

A.164 B.360 C.450 D.90 5.⼦程序的形式参数(虚拟变元或哑元)可以是。

A.变量名、数组名、过程名B.变量名、数组名、过程名和符号常量名C.变量名、数组名、数组⽚段、过程名D.任意符号名6. 语句OPEN(3,FILE=’ABC’,ACCESS=’DIRECT’,RECL=40)打开的是⽂件。

A.有格式顺序存取B.⽆格式顺序存取C.有格式直接存取D.⽆格式直接存取7、下列关于"SUBROUTINE MAP(X,Y)"语句⾏的叙述中,不正确的是( )A) 这是⼦程序的第⼀个语句 B) 字符串"MAP"是⼦程序名C) 变量X是⼦程序的形参D)⼦程序执⾏后,MAP将返回整型数据8、 FORTRAN表达式"2/4+0.5"的值是( )A) 0.5 B) 1 C) 1.0 D) 09、阅读下列FORTRAN程序:PI=3.14159265WRITE(*,'(F7.4) ')PIEND程序运⾏后输出结果是( )A) 3.142 B) 3.1415 C) 0.31416 D) 3.141610、圆的直径存放在整型变量K之中,下列计算圆⾯积的表达式中正确的是( )A) 3.1415926*K*K/4 B) 3.1415926*(K*K/4)C) 3.1415926*(K/2)**2 D) 3.1415926*(K/2)*(K/2)11、要判断"⽉收⼊M在2000元以上(含2000元)且5000元以下)不含5000元)"的职⼯,应该使⽤的逻辑表达式是( )A) M .GE. 2000 .AND. M .LE. 5000 B) M .GE. 2000 .OR. M .LE. 5000C) M .GE. 2000 .AND. M .LT. 5000 D) M .GE. 2000 .OR. M .LT. 500012、下列不是FORTRAN常量的是( )A) (3.0,4.0) B) 3.1416D+00 C) 2/3 D) 'Very good!'13、阅读下列FORTRAN程序:F=1.0DO 20 K=3, 7, 3DO 20 L=K-1, K20 WRTTE(*,'(I3, F8.1)'K, F*KEND程序运⾏结果的最后⼀⾏是( )A) 3 6.0 B) 6 6.0 C) 6 180.0 D) 7 180.014、在FORTRAN中,下列运算符的优先顺序是( )A) 逻辑运算,关系运算,算术运算 B) 关系运算,逻辑运算,算术运算C)算术运算,关系运算,逻辑运算 D) 关系运算,算术运算,逻辑运算15、使⽤FORTRAN内部函数时,所给的⾃变量( )A) 只能是常量 B) 只能是变量C) 只能是常量或者变量D)可以是常量、变量或者表达式16、设X=2.0, Y=8.0, Z=6.0, L=.TRUE.,则下列FORTRAN表达式中值为.TRUE.的是( )A) X+Z.GT.Y.AND.L B) .NOT.(Y.LT.Z+X).NEQV..NOT.LC) .NOT.L.OR.(L.EQV.Y+X.EQ.Z) D) Y+X.GE.Z+X.AND.(L.AND..FALSE.)17、下列输出语句⾏中正确的是( )A) WRITE(*,*)(A(K,L),K=1,3,L=1,3)B) WRITE(*,*)A(K,L),K=1,3,L=1,3C) WRITE(*,*)((A(K,L),K=1,3), L=1,3)D) WRITE(*,*)(A(K,L),K=1,3),L=1,318、给定下列FORTRAN⼦程序如下:SUBROUTINE SUB(K,A)A=SQRT(K*K+1.0)END下列调⽤语句中正确的是( )A) CALL SUB(N,N) B) CALL SUB(X,X)C) CALL SUB(N,X*X) D) CALL SUB(11,X)19、阅读下列FORTRAN程序:INTEGER AA(2,3)DATA AA/1,2,3,4,5,6/CALL S2D19(AA)CALL S2D19(AA(1,2))ENDSUBROUTINE S2D19(BB)INTEGER BB(2,2)WRITE(*,'(1X,2I3)') BB(1,1),BB(2,2)END程序运⾏结果是( )A) 1 4 B) 1 5 C) 1 4 D) 1 23 6 2 6 2 5 3 420、阅读下列FORTRAN程序:INTEGER A(3,3)DO 10 K=1,3DO 10 L=1,310 A(K,L)=K*10+LWRITE(*,'(1X,3I3)')AEND程序运⾏结果是: ( )A) 11 21 31 B) 11 12 13 C) 31 32 33 D) 33 32 3112 22 32 21 22 23 21 22 23 32 22 2113 23 33 31 32 33 11 12 13 31 21 11⼆、填空题1、下列FORTRAN函数⼦程序的功能是⽤以下公式计算⼀组数据Z1,Z2,…,Zn的标准差σ:σ2=(Z12 +Z22+…+Zn2)/n-[(Z1+Z2+…+Zn)/n]2请在程序中的下划线处填⼊合适的内容。

Fortran模拟试卷1-8

Fortran模拟试卷1-8

FORTRAN90模拟测验一(笔试部分)一.选择题(从4个可选答案中选择一个正确答案添入空白处)1.FORTRAN 90规定程序中名称的长度不能超过个字符。

A.8 B.15 C.31 D.632.关于FORTRAN 90程序单元,以下四种说法中正确的是。

A.一个FORTRAN 90程序由多个程序单元组成,其中有一个且只能有一个主程序单元B.一个FORTRAN 90程序由多个程序单元组成,其中有一个且只能有一个模块单元C.一个FORTRAN 90程序允许有多个程序单元,但每类程序单元只能有一个D.一个FORTRAN 90程序允许有多个程序单元,但主程序单元和模块单元只能有一个3.FORTRAN 90程序中允许使用的三个通用标志符是。

A.!% & B.@ , ; C.! ; & D.; & *4. 下列中,所指的FORTRAN 90表达式都是正确的。

①∣A*x+B*y+C*z∣② b*b+4*a*c③ .NOT. .TRUE. .AND. .FALSE. ④‘A’<= ch <= ‘Z’⑤ X>100 = = .TRUE. ⑥ SQRT(A2+B2)⑦π*R**2 ⑧‘HAPPY ’+‘NEW ’+‘YEAR.’A. ①②⑤B.②③④⑧C.②③⑤D.②⑤⑥⑦5. FORTRAN 90规定,变量类型声明从高到低的优先顺序是。

A.隐含约定(I-N规则)、IMPLICIT声明、类型声明B.类型声明、隐含约定(I-N规则)、IMPLICIT声明C.类型声明、IMPLICIT声明、隐含约定(I-N规则)D.IMPLICIT声明、类型声明、隐含约定(I-N规则)6.数组声明语句为:INTEGER,DIMENSION(-5:-1,-3:3,11:15) ::num 数组元素num(-2,1,13)是存储结构中第个元素。

A.70 B.85 C.90 D.947.类型声明语句为:INTEGER(2) I 数据输出语句为:PRINT *,I变量I中数据输出域宽是字符。

FORTRAN90试题_4_

FORTRAN90试题_4_

FORTRAN90语言部分一、 语言基础单项选择(每题2分,共20分)(一)下面的函数计算中,错误的是 1 。

A) SQRT(9) B) REAL(9) C) ABS(9) D) MOD(9,2)(二)运行下面的程序时(P,W,X,Y 都是实型变量), READ(*,10) P,W,X,Y WRITE(*,20)X,Y10 FORMAT(1X,F3.1,3(1X,F4.2)) 20 FORMAT(1X,F4.1,3(1X,F5.2)) END如果从键盘输入数据:12345678901234567890,则输出结果为 2 。

A) 1.23 56.78 B) 12.3 67.89 C) 12.3 45.67 D) 34.5 67.89(三)数学式a acb x 24sin22-+的FORTRAN 表达式为 3 。

A) SIN(X**2)+SQRT(B*B-4*A*C)/2/A B) SIN(X)**2+ SQRT(B*B-4*A*C)/2/AC) (SIN(X**2)+SQRT(B*B-4*A*C))/(2*A) D) (SIN(X)**2+SQRT(B*B-4*A*C))/(2*A)(四)表示条件“A 加B 大于C 并且C 大于等于D ”的FORTRAN90逻辑表达式是 4 。

A) A+B>C >=D B) (A+B)>C >=DC) A+B>C.AND. C>=DD) (A+B)>C.OR. C>=D(五)通过下面的赋值,字符变量LINE 的值是 5 。

表示空格。

CHARACTER(LEN=4)::LINE LINE=’10’ A) 10 B) 1000 C) ‘10’ D) ’10 ’(六)下面对二维数组的说明语句中,正确的语句是 6 。

A) 显式形状数组的说明:REAL,DIMENSION(10,:) ::AB) 动态数组的说明方式:REAL,DIMENSION(:,:), ALLOCATE ::A C) 假定形状数组的说明:REAL,DIMENSION(10,:) ::A D) 指针数组的说明:POINTER,DIMENSION(1:3,2:4) ::A (七)下面的模块中,错误的语句是 7 。

fortran课后习题答案解析

fortran课后习题答案解析

fortran课后习题答案解析第一章 FORTRAN程序设计基础第15页 1、21.简述程序设计的步骤。

“程序设计”:反映了利用计算机解决问题的全过程,通常要经过以下四个基本步骤:(1)分析问题,确定数学模型或方法;(2)设计算法,画出流程图;(3)选择编程工具,编写程序;(4)调试程序,分析输出结果。

2. 什么是算法?它有何特征?如何描述算法?解决问题的方法和步骤称为算法。

算法的五个特征:(1) 有穷性。

(2) 确定性。

(3) 有效性。

(4) 要有数据输入。

(5) 要有结果输出。

算法的描述有许多方法,常用的有:自然语言、一般流程图、N -S图等。

第二章顺序结构程序设计第29页 1、2、3、4、5、6、7、8、91.简述符号常量与变量的区别?符号常量在程序运行过程中其值不能改变。

变量在程序运行过程中其值可以改变。

2. 下列符号中为合法的FORTRAN 90标识符的有哪些?(1) A123B (2) M%10 (3) X_C2 (4) 5YZ(5) X+Y (6) F(X) (7) COS(X) (8) A.2(9) ‘A’ONE (10) U.S.S.R.(11) min*2 (12) PRINT3. 下列数据中哪一些是合法的FORTRAN常量?(1) 9,87 (2) .0 (3) 25.82(4) -356231(5) 3.57*E2 (6) 3.57E2.1 (7) 3.57E+2(8) 3,57E-24. 已知A=2,B=3,C=5(REAL);且I=2,J=3(INTEGER),求下列表达式的值:(1) A*B+C 表达式的值: 11 (2) A*(B+C) 表达式的值: 16(3) B/C*A 表达式的值: 1.2 (4) B/(C*A) 表达式的值: 0.3(5) A/I/J 表达式的值: 0.33 (6) I/J/A 表达式的值: 0(7) A*B**I/A**J*2 表达式的值: 4.5(8) C+(B/A)**3/B*2. 表达式的值: 7.25(9) A**B**I 表达式的值: 5125. 将下列数学表达式写成相应的FORTRAN表达式:(1) 1E-2 (2)(-B+SQRT(B*B-4*A*C)/(2*A)(3) 1+X+X*X/2+X**3/2/3(4) COS(ATAN((A**3+B**3)**(1.0/3)/(C*C+1)))(5) EXP(A*X**2+B*X+C)(6) COS(X*Y/SQRT(X*X+Y*Y))**36. 用FORTRAN语句完成下列操作:(1) 将变量I的值增加1。

大学Fortran期末复习资料-Fortran程序设计_04

大学Fortran期末复习资料-Fortran程序设计_04

20XX年复习资料大学复习资料专业:班级:科目老师:日期:一、单项选择题:(每小题2分,共 40 分)1. 下列叙述中正确的是 。

A. 完整的Fortran 程序的第一个语句必须是program 语句B. 完整的Fortran 程序中只能有一个end 语句C. Fortran 程序中各类语句的位置是任意的D. Fortran 程序中所有可执行语句在程序执行时都会产生某种机器操作2. Fortran 表达式20XXXX/8*0.3/6.0的值为 。

A. 0.75B. 0C. 0.0D. 0.20XXXX 3. 下列合法的Fortran 标识符为 。

A. M%8B. A20XXXX3BC. F(x)D. 5xy4. 下列数据中是不合法的Fortran 常量的是 。

A. trueB. (1,3)C. ‘’D.4.567E-45. 在Fortran 中)2a /()x (sin e 2x 的正确的算术表达式为 。

A. exp(x)+sin(x**2)/(2*a)B. exp(x)+sin(x**2)/2*aC. e**x+sin(x**2)/(2*a)D. exp(x)+sin(x*2)/(2*a)6. 设有下面一段格式输出语句write(*,20XXXX) x20XXXX format(1x,’x=’, f5.1)如果在输出语句中直接包含格式说明,正确的写法应该是 。

A. write(*,1x, ‘x=’ , f5.1) xB. write(*,’(1x,’x=’, f5.1)’) xC. write(*,”(1x,’x=’, f5.1)”) xD. 没有这种格式7. 下面的逻辑IF 语句正确的是 。

A. if (x<0.0) then 20XXXXB. if (x>=0.0) .eqv. (y<0) y=23C. if (0.0=< y <20XXXX) y=x+20XXXXD. if (x>20XXXX.0 .or. x<0.0) y=2*x+exp(x)8. 阅读下列Fortran 程序:real :: z(2,2)=(/1,2,3,4/)p(x, y)=3*x-2*yt=z(1,2)if ( t<=1.0) s = p(1.0,2.0)if ( t<=3.0) s = p(2.0,3.0)if ( t<=4.0) s = p(3.0,4.0)write(*,*) send执行上述程序后,输出的S 值为 。

Fortran-4

Fortran-4
例:打印30个学生的学号和成绩 例:打印30个学生的学号和成绩
1⇒N
输入NUM, 输入NUM,GRADE 打印NUM, 打印NUM,GRADE N=1 100 READ (*,*) NUM, GRADE WRITE (*,*) NUM, GRADE N=N+1 IF ( N.LE. 30 ) GOTO 100 END
第四章 循环结构程序设计
4.1 用GOTO语句实现循环 GOTO语句实现循环 4.2 用DO语句实现循环 DO语句实现循环 4.3 用DO WHILE语句实现循环 WHILE语句实现循环 4.4 几种循环组织方式的比较 4.5 循环的嵌套 4.6 程序举例
2011-11-27
4.6 程序举例
从键盘输入一个整数N 从键盘输入一个整数N,然后计算并输出 S=1+21+22+…+2|N|,最后计算并输出
3、do循环执行过程 、 循环执行过程 计算e 计算e1、e2 、 e3的值 e1 ⇒i 计算循环次数: 计算循环次数:r 当r > 0 循环体语句 i + e3 ⇒i r −1⇒r
2011-11-27
4.2 用DO语句实现循环 DO语句实现循环
4、与循环有关的控制语句 、 (1)exit语句 在循环体内使用exit语句,将迫使所在循环立即终止,跳 出所在循环体,而继续执行循环结构后面的语句。通常将 exit语句与if语句配合使用: if e if(e) exit 即当e为真时,exit语句被执行,终止循环。 e exit (2)cycle语句 Cycle语句用来结束本次循环,即跳过循环体中尚未执行的 语句。在循环结构中,cycle语句将控制直接转向循环条件 测试部分,从而决定是否继续执行循环。 Cycle语句和exit语句区别在于:cycle语句只结束本次循环, 而不是终止整个循环的执行。

兰大《Fortran语言》15春在线作业4 答案

兰大《Fortran语言》15春在线作业4 答案

《Fortran语言》15春在线作业4一、单选题(共14 道试题,共70 分。

)1. 以下值为3的表达式是___A. SQRT(9.0)B. 9**0.5C. MOD(33,10)D. 1.0*3正确答案:C2. 阅读下列程序dimension s(3) webstripperattrwas webstripperlinkwas=/reeducate/com_grade/test/two/I+1 s/1.0,2.0,3.0/ write(*,*)la(3,s) end function la(n,x) dimension x(n) la=0 l=2 do 20 i=1,N la=LA+X(I)/L 20 continue end 程序运行结果是A. 1.0000000B. 2.0000000C. 1D. 2正确答案:D3. 以下用标准化指数形式表示数字10的选项是___A. 0.000000001B. 1E-8C. 0.1E-9D. 1003-1正确答案:D4. 等价语句的主要用途是___A. 节省内存空间B. 便于进行数据传递C. 使多个变量具有同一值D. 节省运算时间正确答案:A5. 下列关于FORTRAN77源程序编辑规则的叙述之中,正确的是A. 编辑时一行都必须从第7列开始B. 主程序第一行必须是PROGRAM语句C. 每个FORMAT语句必须具有标号D. 每个子程序中必须有一个RETURN语句正确答案:C6. 阅读下列FORTRAN77程序:READ(*,*) N X=1.0 IF (N.GE.0) X=2*X-2.0 IF (N.GE.5)。

FORTRAN程序设计复习题及答案

FORTRAN程序设计复习题及答案

FORTRAN程序设计复习题及答案FORTRAN程序设计复习题一、选择题B (1)下列各FORTRAN表达式中合法的是A) S+T*2P >= B) .NOT. (A*B+C)C) A2+B2/(C+D) <= D) (A+B).NOT.A*B.GT.(.NOT.只跟一个表达式)C (2)数学式(3/5)ex+y的FORTRAN表达式是A) 3*EXP(X+Y)/5 B) 3*E* *(X+Y)/C) (3/5)*EXP(X+Y)D) EXP(X+Y)D (3)下列FORTRAN77表达式中不合法的是A) A.GT.B.EQV.C.GT. D B) A.AND.B.AND.C.AND.DC) .NOT.(X.LE.D) A.LT.B.LT.C.LT.DD(4)下列叙述中不正确的是A) FORTRAN子程序可以单独编译B) 对一个FORTRAN源程序进行编译和连接无误后可生成可执行文件C) 即使编译和连接都正确无误,FORTRAN程序运行时仍可能出错D) FORTRAN连接的主要任务是把函数库中的函数翻译成机器指令(正确描述:主要任务为连接目标文件)B (5)在下列FORTRAN77运算符中,优先级最高的是A) .AND. B) .NOT. C) .OR. D) .EQ.B (6)FORTRAN表达式"6/5+9/2**3/2"的值为A) 33 B) 1 C) 5 D) 3A (7)下列FORTRAN77表达式中,合法的是:A) .AND.. B) 10.0C) D)提示:A)相当于 .AND.(.NOT.())D (8)关于编译一个FORTRAN源程序文件,下列说法中错误的是A) 允许编译只有一个主程序而没有子程序的源文件B) 允许编译有多个子程序的源文件C) 允许编译只有一个子程序而没有主程序的源文件D) 允许编译有多个主程序的源文件C (9)在FORTRAN77源程序中,续行标志符必须放在A) 第1列 B) 第1-6列C) 第6列D) 第5列D (10)下列关于"SUBROUTINE MAP(X,Y)"语句行的叙述中,不正确的是A) 这是子程序的第一个语句 B) 字符串"MAP"是子程序名C) 变量X是子程序的形参D) 子程序执行后,MAP将返回整型数据提示:子程序无返回值,自定义函数才有)A (11)FORTRAN表达式"2/4+"的值是A) B) 1 C) D) 0提示:2/4默认等于整型,=》D (12)FORTRAN表达式"MOD,"的值是A) B)0.0 C) D)A (13下列FORTRAN运算符中,优先级最低的是A)逻辑运算符.AND. B)算术运算符*C)关系运算符 >= D)算术运算符+A (14下列语句函数的定义中正确的是A)F(X,Y)=(X+Y)/(X*Y)+ B)FUNCTION FUN(I,J,K)=3*I +2*J+*K C)H(A,B,C(I))=SIN(A)+SIN(B)+C(I) D)S(A,B,C)=A*B+S(A*A,B,C)B(15下列标识符中,不属于FORTRAN常量的是A).TRUE. B)FALSE C)ˊROOT=ˊ D)ˊˊB(16)"整型变量M能被整型变量K整除"的FORTRAN77表达式是A)MOD(M,K)=0 (MOD(M,K)= =0)B)M-M/K*K .EQ. 0C)MOD(K,M)=0 D)MOD(M,K)=0 == .TRUE.C (17)设有下列数组说明语句: REAL:: A(1:10,-1:10)该数组说明语句定义了数组A 中的元素个数为 A )100 B )110 C )120 D )121 A (18)按隐含规则(默认为整型),语句IP=执行之后,IP 的值是A) 3 B) 3.1416C)D)D (19)下列数据中,不符合 FORTRAN 常量表示法的是A) B)C)‘FOOT’‘=’ D) TRUEC (20)设C 是复型变量,A 与B 为有定义的实型变量,下列赋值语句中合法的是A) C=A+BB) C=,**2) C) C=,3D-2)D) C=(A +B ,A-B )B (21)有矩阵(数组)=22221111B下列DATA 语句中正确的是A) DATA B/4*,4* B) DATA((B(I,J), J=1,4), I=1,2)/4*, 4* C) DATA B/4*,/ D) DATA((B(I,J),I=1,2), J=1,4)/4*, 4* C (21)在使用DO 循环及循环嵌套时,下列叙述中正确的是A) 不仅可以从循环体内转到循环体外,也可以从循环体外转到循环体内 B) 外循环与内循环的DO 循环控制变量可以不同.也可以相同C) 可以从循环体内转到循环体外,但不能从循环体外转到循环体内 D)DO 循环控制变量在循环体内可以再赋值,也可以被引用B (22)在FORTRAN 表达式中,各种运算的顺序是 A) 逻辑运算,关系运算,算术运算B) 算术运算,关系运算,逻辑运算C) 关系运算,逻辑运算,算术运算D) 关系运算,算术运算,逻辑运算D (23)赋值语句X=4**(6/12)=4**0=执行后,实型变量X的值是A) 2 B) 2.0 C) 1 D)D (24)为了保证正确出数据,格式编辑符中w与d的关系应该是A) w>d B) w>d+2 C) w>d+4 D) w>d+6C (25)设下列各语句中用到的数组都已定义,则下列语句中正确的是A) READ(*,*)(I,A(I),I=1,10) B)READ(*,*)(B,(I,J),I=1,10,J=1,10)C) READ(*,*)N,(A(I),I=1,N) D) READ(*,*)(I=1,10,A(I))A (26)下列DO语句中,不正确实现循环的是(I-N规则:I-N会被视为整型)A) DO 10 S=,, B) DO 10 K=,,C) DO 10 S=,, D) DO 10 K=,,D (27)下列说法中正确的是A) 块IF结构中的THEN块或ELSE块不能是空块B) 块IF结构中至少有一个ELSE IF 语句或ELSE语句C) 每个ELSE IF 语句要有一个对应的END IF语句D) 一个块 IF 结构中只能有一个END IF语句A (28)下列数组说明语句中正确的是A) INTEGER A(-1:5,0:6)B) REAL M(10:5)C) DOUBLE Y(1:N+1) D) DOUBLE Z(14)B (29)设有一个FORTRAN77子程序如下:SUBROUTINE SS(X,Y,S,T)S=X+Y(S,T为传出,即需要输出的(相当于求圆的面积中的area),只能T=X*Y 为确定的数;X,Y为传入(相当于radius),可为表达式)END调用上述子程序的正确语句是A) CALL SS,W*W,5,,Z)(多了一个)B) CALL SS,,P,Q,R) (多了一个)C) CALL SS(F,G,,D) CALL SS*I,*J,C,D)(I,J可先赋值)A (30)在下列语句函数的定义中正确的是A) F(X,Y)=(X+Y)/(X*Y)+B) FUNCTION(I,J,K)=3*I+2*J+*KC) H(A,B,C(I))=SIN(A)+SIN(B)+C(I) D) S(A,B,C)=A*B+S(A*A,B,C)B (31)COMMON语句的功能是A) 给同一程序模块中的若干变量分配同一存储单元B) 给不同程序模块中的变量分配相同的存储单元C) 给程序中任意两个变量分配相同的存储单元D) 给同一程序模块中的不同变量分配相同的存储单元B (32)下列表达式中值为整数4的是A) SQRT B)27/6 C) 16** D) 4*C(34)语句K=2+**2/2执行后,整型变量 K的值是A) B) 5 C) 6D) 7C (36)适用于字符型 I/O的 FORTRAN77格式编辑将是A) B) C) A D)D (37)给定子例行程序如下SUBROUTINE SUB(K,A)B=K+2(K为整型,B)错误;A不是整型,A)错误;K为传入,可为表达式,A=A+B A为传出,不能为表达式)END下列调用语句中正确的是A) CALL SUB(N,N) B) CALL SUB(X,X)C) CALL SUB(N+2,X) D) CALL SUB(N,X+3)C (38) 下列FORTRAN语句中,正确的是A) READ(*,*) (N,A(K),K=1,N) B) WRITE(*,*) (M(K,J),K=1,10,J=1,10)C) WRITE(*,*) (10,A(K),K=1,10) D) DATA A,B,C/2*C (39)对于下列给定的FORTRAN说明和赋初值语句:INTEGER A(2,2)DATA A/3,4,5,6/数组元数A(2,1)的初值为A) 3 B) 5 C) 4 D) 6C(40)下列对公用区的说明语句中错误的是A) COMMON A(5), B B) COMMON/AB/ A(5), BC) COMMON AB/A(5), B/ D) COMMONND. IF>=X<= Y=2*X+D) IF(X >= <= Y=2*X+二、阅读题1、阅读下列FORTRAN程序:program examplereal a,b,ca=b=c=write(*,"(3)") a,b,cend程序运行的结果是:2、阅读下列FORTRAN程序:program exampleinteger :: a=1integer :: b=2real :: cc=a/bwrite(*,"") cend程序运行结果是:3、阅读下列FORTRAN程序:program exampleimplicit noneinteger rain, windspeedwrite(*,*) "Rain:"read(*,*) rainwrite(*,*) "Wind:"read(*,*) windspeedIf ( rain>=500 .or. windspeed >=10 ) then write(*,*) "停止上班上课"elsewrite(*,*) "照常上班上课"end ifstopend运行上述程序时,如果从键盘输入Rain:505<回车>Wind:8<回车>则最后输出的结果为: 停止上班上课4、阅读下列FORTRAN程序:program exampleimplicit nonereal a,b,anscharacter operatorread(*,*) aread(*,"(A1)") operator read(*,*) bselect case(operator)case('+')ans = a+bcase('-')ans = a-bcase('*')ans = a*bcase('/')ans = a/bcase defaultwrite(*,"('Unknown operator ',A1)") operator stop end selectwrite(*,",A1,,'=',") a,operator,b,ansstopend运行上述程序时,如果从键盘输入100<回车><回车>200<回车>则最后输出的结果为: Unknown operator5、阅读下列FORTRAN程序:program exampleimplicit noneinteger iinteger strleninteger, parameter :: key = 2character(len=20) :: stringwrite(*,*) "Encoded string:"read(*,*) stringstrlen = len_trim(string)do i = 1, strlenstring(i:i) = char( ichar(string(i:i)) + key ) end dowrite(*,"('String:',A20)") stringstopendBCDIJK<回车>则最后输出的结果为: DEFKLM6、阅读下列FORTRAN程序:program exampleimplicit noneinteger i,jdo i=1, 2do j=2, 3, 2write(*, "(I2,I2)") i,jend dowrite(*,*) "another circle"end dostopend程序运行的结果是: 1 2another circle2 2another circle(按输出格式,1、2前均有一空格。

Fortran95第三章第四大题习题与答案

Fortran95第三章第四大题习题与答案

1. 计算10014131211+⋅⋅⋅++++=s 。

Program ex3_1implicit nonereal sinteger iinteger,parameter::num=100s=0do i=1,nums=s+1.0/real(i)enddoprint*,'s=',sstopEnd2. 计算121917151311++⋅⋅⋅-+-+-=n s 的前20项和。

Program ex3_2implicit nonereal sinteger i,ainteger,parameter::num=20a=1s=1.0do i=1,num-1a=-as=s+a*(1.0/real(2*i+1))enddoprint*,'s=',sstopEnd3. 计算!1!31!211n y +⋅⋅⋅+++=。

Program ex3_3implicit nonereal yinteger i,x,ny=0x=1print*,'输入一个正整数'read*,ndo i=1,nx=x*iy=y+1.0/real(x)enddoprint*,'y=',ystopEnd4. 计算333421n y ⋅⋅⋅+++=,当y 值大于1000时停止计算并输出n 和y 值。

Program ex3_4implicit noneinteger n,yn=1y=1do while(y<=1000)n=2*ny=y+n**3enddoprint*,'n=',n/2,'y=',y-n**3stopEnd5. 利用以下展开式357sin 3!5!7!x x x x x =-+-+⋅⋅⋅计算sin x 的近似值。

x 值从键盘输入,当式中一项的绝对值小于510-时结束计算并输出sin x 值和项数。

Program ex3_5implicit noneinteger i,j,k,n,signreal x,y,sum,mid,mulprint*,'请输入一个角度值:'read*,xy=x*3.14159/180.0sign=1n=1j=1k=3mul=1.0mid=1.0sum=ydodo i=j,kmid=mid*ymul=mul*real(i)enddoif((mid/mul)>1.0E-5)thensign=-signsum=sum+sign*(mid/mul)j=k+1k=2*n+1elseexitendifenddoprint*,'sin(',x,')=',sum,'n=',nstopEnd6.有一个30个数的数列,头两个数为0和1,第三个数是第一、第二个数之和,第四个数是第二、第三个数之和,以后每个数都是其前两个数之和。

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

工程分析程序设计 上机作业(五)模块化编程上机目的:掌握内部例程、主程序、外部例程、模块等功能的使用方法。

1、 有一个六边形,求其面积。

为求面积,作了3条辅助线。

如图所示:(提示,三角形面积=()()()s s a s b s c ---,其中2a b c s ++=,a 、b 、c 为三个边长)。

要求用内部函数来计算每个三角形的面积。

l 1=10 l 2=30l 3=16 l 4=13l 5=21l 6=14l 7=20l 8=36 l9=28program mainimplicit nonereal :: A(9)=(/10,30,16,13,21,14,20,36,28/)integer ireal ss=area(A(3),A(4),A(7))+area(A(2),A(7),A(8))+& area(A(1),A(8),A(9))+area(A(5),A(6),A(9))print*,'The area is',scontainsfunction area(x,y,z)implicit nonereal x,y,zreal p,areap=(x+y+z)/2area=sqrt(p*(p-x)*(p-y)*(p-z))end functionend2、编写一个子例程子程序,SUM(S,T,N1,N2)。

把整型数N1到N2进行求和,并把求和的结果放置到S,把整型数N1到N2进行求积,并把求积的结果放置到T。

并用这个子程序来计算:y=++++++++++-(1234)(345678)(3*4*5*6)(1*2*3)program mainimplicit noneinteger A(8)integer i,b,c,d,e,f,g,m,n,yb=0;d=0;g=1;n=1do i=1,8A(i)=iend docall sum(b,c,1,4)call sum(d,e,3,8)call sum(f,g,3,6)call sum(m,n,1,3)y=b+d+g-nprint*,'y=(1+2+3+4)+(3+4+5+6+7+8)+(3*4*5*6)-(1*2*3)=',ycontainssubroutine sum(s,t,N1,N2)integer s,t,N1,N2,ido i=N1,N2s=s+A(i)t=t*A(i)end doend subroutineend3、编写函数子程序GDC求两个数的最大公约数。

求最大公约数的算法如下:把两个数中大的那个数作为被除数,两数相除得到一个余数。

把余数去除除数得到新一轮的余数。

不断重复这一过程直到余数为0,这时的除数就是两个数的最大公约数。

调用此函数,求1260,198,72三个数的最大公约数。

program mainimplicit noneinteger a,b,c,m,n,tempprint*,'请输入三个数:'print*,'a b c'read*,a,b,cif (a<b) thentemp=aa=bb=tempend ifcall GDC(a,b,m)if (c<m) thentemp=cc=mm=tempend ifcall GDC(m,c,n)print*,'最大公约数为',ncontainssubroutine GDC(x,y,z)integer x,y,z,kdo while(mod(x,y)/=0)k=mod(x,y)x=yy=kend doz=yend subroutineend4、 编写一个模块程序,提供以下服务:定义出常量π、e 。

定义出子程序,实现求和21n i n=∑、求阶乘n !。

并在主程序中计算如下结果:从键盘上输入整数n 、实型数A 、R 、R 0,求21!n i n n =⎛⎫ ⎪⎝⎭∑(实型)和0202n n R R An R e R R π⎛⎫- ⎪⎝⎭⎛⎫ ⎪⎝⎭(实型)。

module mytaskimplicit nonereal,parameter :: PI=3.1415926real,parameter :: e =2.7182818containsfunction sub(n)integer i,n,subsub=0do i=1,nsub=sub+i*iend doend function subfunction fact(n)integer i,nreal factfact=1do i=1,nfact=fact*iend doend function factend module mytaskprogram mainuse mytaskimplicit noneinteger nreal A,R,R0real x,yprint*,'请输入依次 n,A,R,R0 :'read*,n,A,R,R0x=fact(n)/sub(n)y=(A*n/(2*PI*R*R))*(R/R0)**n*e**(-(R/R0)**n)print*,'n!/∑n^2(i=1,n)=',xprint*,'(A*n/(2*PI*R*R))*(R/R0)**n*e**(-(R/R0)**n)',yend program5、 编写外部函数,计算...!7!5!3sin 753+-+-=x x x x x 的值,直到最后一项的绝对值小于10-6为止。

再编写主程序,从键盘读入x ,调用该外部函数并输出sinx 的计算结果。

注意不能用Fortran 的标准函数SIN(X)。

program mainimplicit nonereal x,pexternal subprint*,'请输入角度 x :'read*,xp=x/180*3.14call sub(p)end programsubroutine sub(k)implicit nonereal k,yinteger iy=0i=1do while (abs(k**i/(fact(i)))>1.0e-6)y=y+(-1)**((i-1)/2)*k**i/(fact(i))i=i+2end doprint*,'sin(x) = ',ycontainsfunction fact(m)integer m,j,factfact=1do j=1,mfact=fact*jend doend function factend subroutine sub6、 编写程序,用欧拉法数值求解一阶微分方程。

数值解法的基本思想:用差分方程代替微分方程,然后在若干个离散点上逐点求解差分方程,得到各离散点x 0, x 1, x 2, … 等处函数(),()dy f x y x dx=的近似值y 0, y 1, y 2, … 。

其中各离散点x 0, x 1, x 2, …之间的距离称为步长h 。

欧拉法的基本思想是,一阶微分dy dx 可用向前差分代替,即11'()n n y y y h+≈-。

带入微分方程,可得 1(,)n n n n y y f x y h+-≈ 因此,1*(,)n n n n y y h f x y +≈+,其中n n x x h -=+1。

当给定x 0, y 0和步长h 后,即可按下面步骤求得数值解:0000(,)x f x y y ⎫⇒⇒⎬⎭1111,(,)y f x y x ⎫⇒⎬⎭()2222,n y f x y y x ⎫⋅⋅⋅⇒⎬⎭请按上述方法编写程序用Euler 法求解微分方程22dy y x dx=-,当x 0=0时,y 0=1.0。

取h =0.1,试求出当x i =0.1,0.2,0.3,0.4….,1.0时的y i 值。

program mainimplicit nonereal x(0:10)real y(0:10)real p(0:10)integer(2) i,jy(0)=1.0; x(0)=0.0p(0)=f(x(0),y(0))do i=1,10x(i)=x(i-1)+0.1end dodo j=1,10y(j)=y(j-1)+0.1*p(j-1)p(j)=f(x(j),y(j))print*,'x(',j,')=',x(j),'y(',j,')=',y(j)end docontainsfunction f(a,b)real a,b,ff=b*b-a*aend functionend。

相关文档
最新文档