logo资料库

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

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