最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

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

IT圈 admin 36浏览 0评论

2024年3月14日发(作者:查曼岚)

第四章

m main implicit none write(*,*) "Have a good time." write(*,*) "That's not

bad." write(*,*) '"Mary" isn''t my name.' end program

m main real, parameter :: PI=3 implicit none.14159 real radius write(*,*) "请

输入半径长" read(*,*) radius write(*,"(' 面积='f8. 3)") radius*radius*PI end program

m main implicit none real grades write(*,*) "请输入成绩" read(*,*) grades

write(*,"(' 调整后成绩为 'f8.3)") SQRT(grades)*10.0 end program

r a,b real ra,rb a=2 b=3 ra=2.0 rb=3.0 write(*,*) b/a ! 输出1, 因为使用整数计

算, 小数部分会无条件舍去 write(*,*) rb/ra ! 输出1.5

m 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

第五章

m 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

m 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

m 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 program

m main implicit none integer year, days logical mod_4, mod_100, mod_400

write(*,*) "请输入年份" read(*,*) year mod_4 = ( MOD(year,4) == 0 ) mod_100 =

( MOD(year,100) == 0 ) mod_400 = ( MOD(year,400) == 0 ) if ( (mod_4 .NEQV.

mod_100) .or. mod_400 ) then days = 366 else days = 365 end if write(*,"('这

一年有'I3'天')") days stop end program

第六章

m main implicit none integer i do i=1,5 write(*,*) "Fortran" end do

stop end program

m main implicit none integer i,sum sum = 0 do i=1,99,2 sum = sum+i

end do write(*,*) sum stop end program

m 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 program

m 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 program

m 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

第七章

m 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 program

r 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=49

m 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 program

m 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 end 5.a(2,2) ! 1+(2-1)+(2-1)*(5) = 7 a(3,3) !

1+(3-1)+(3-1)*(5) = 13

第八章

m 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

m 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 function

m 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

m 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 function

m 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 function

m 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

2024年3月14日发(作者:查曼岚)

第四章

m main implicit none write(*,*) "Have a good time." write(*,*) "That's not

bad." write(*,*) '"Mary" isn''t my name.' end program

m main real, parameter :: PI=3 implicit none.14159 real radius write(*,*) "请

输入半径长" read(*,*) radius write(*,"(' 面积='f8. 3)") radius*radius*PI end program

m main implicit none real grades write(*,*) "请输入成绩" read(*,*) grades

write(*,"(' 调整后成绩为 'f8.3)") SQRT(grades)*10.0 end program

r a,b real ra,rb a=2 b=3 ra=2.0 rb=3.0 write(*,*) b/a ! 输出1, 因为使用整数计

算, 小数部分会无条件舍去 write(*,*) rb/ra ! 输出1.5

m 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

第五章

m 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

m 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

m 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 program

m main implicit none integer year, days logical mod_4, mod_100, mod_400

write(*,*) "请输入年份" read(*,*) year mod_4 = ( MOD(year,4) == 0 ) mod_100 =

( MOD(year,100) == 0 ) mod_400 = ( MOD(year,400) == 0 ) if ( (mod_4 .NEQV.

mod_100) .or. mod_400 ) then days = 366 else days = 365 end if write(*,"('这

一年有'I3'天')") days stop end program

第六章

m main implicit none integer i do i=1,5 write(*,*) "Fortran" end do

stop end program

m main implicit none integer i,sum sum = 0 do i=1,99,2 sum = sum+i

end do write(*,*) sum stop end program

m 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 program

m 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 program

m 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

第七章

m 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 program

r 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=49

m 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 program

m 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 end 5.a(2,2) ! 1+(2-1)+(2-1)*(5) = 7 a(3,3) !

1+(3-1)+(3-1)*(5) = 13

第八章

m 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

m 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 function

m 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

m 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 function

m 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 function

m 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

发布评论

评论列表 (0)

  1. 暂无评论