Fortran95习题答案.doc

上传人:豆**** 文档编号:23952085 上传时间:2022-07-02 格式:DOC 页数:130 大小:1MB
返回 下载 相关 举报
Fortran95习题答案.doc_第1页
第1页 / 共130页
Fortran95习题答案.doc_第2页
第2页 / 共130页
点击查看更多>>
资源描述

《Fortran95习题答案.doc》由会员分享,可在线阅读,更多相关《Fortran95习题答案.doc(130页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-dateFortran95习题答案第四章第四章1.program main implicit none write(*,*) Have a good time. write(*,*) Thats not bad. write(*,*) Mary isnt my name.end program2.program main real, parameter : PI=3 impl

2、icit none.14159 real radius write(*,*) 请输入半径长 read(*,*) radius write(*,( 面积=f8. 3) radius*radius*PIend program3.program main implicit none real grades write(*,*) 请输入成绩 read(*,*) grades write(*,( 调整后成绩为 f8.3) SQRT(grades)*10.0end program4.integer a,breal ra,rba=2b=3ra=2.0rb=3.0write(*,*) b/a ! 输出1, 因

3、为使用整数计算, 小数部分会无条件舍去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.54 write(*,(f8.3米 =f8.3厘米 =f8.3英寸) d%meter, d%cm, d%inchend program第五章1.program main implicit

4、none integer money real tax write(*,*) 请输入月收入 read(*,*) money if ( money1000 ) then tax = 0.03 else if ( money5000) 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

5、 case(day) case(1,4) tv = 新闻 case(2,5) tv = 电视剧 case(3,6) tv = 卡通 case(7) tv = 电影 case default write(*,*) 错误的输入stop end select write(*,*) tvend program3.program main implicit none integer age, money real tax write(*,*) 请输入年龄 read(*,*) age write(*,*) 请输入月收入 read(*,*) money if ( age50 ) then if ( mone

6、y1000 ) then tax = 0.03else if ( money5000 )then tax = 0.10else tax = 0.15end if else if ( money1000 ) then tax = 0.5else if ( money5000 )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

7、_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 stopend program第六章1.program main implicit none integer i do i=1,5

8、write(*,*) Fortran end do stopend program2.program main implicit none integer i,sum sum = 0 do i=1,99,2 sum = sum+i end do write(*,*) sum stopend program3.program main implicit none integer, parameter : answer = 45 integer, parameter : max = 5 integer weight, i do i=1,max write(*,*) 请输入体重read(*,*) w

9、eightif ( weight=answer ) exit end do if ( i=max ) then write(*,*) 猜对了 else write(*,*) 猜错了 end if stopend 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 stopend prog

10、ram5.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+1end if end do write(*,(A79) output stopend program第七章1.program ma

11、in 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) stopend 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=360integer d(-5:5) ! 11integer e(-3:3, -3:3)

12、 ! 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 stopend program4.program mainimplicit none integer, parameter : size=10 integer : a(size) = (/ 5,3,6,4,8,7,1,9,2,10 /) integer : i,j integer

13、: 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 ifend do end do write(*,(10I4) a stopend5.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

14、(radius, area) write(*,( 面积 = F8.3) area stopend programsubroutine CircleArea(radius, area) implicit none real, parameter : PI=3.14159 real radius, area area = radius*radius*PI returnend subroutine2.program main implicit none real radius real, external : CircleArea write(*,*) 请输入半径长 read(*,*) radius

15、 write(*,( 面积 = F8.3) CircleArea(radius) stopend programreal function CircleArea(radius) implicit none real, parameter : PI=3.14159 real radius CircleArea = radius*radius*PI returnend function3.program main implicit none call bar(3) call bar(10) stopend programsubroutine bar(length) implicit none in

16、teger, intent(in) : length integer i character(len=79) : string string= do i=1,length string(i:i)=* end do write(*,(A79) string returnend subroutine4.program main implicit none integer, external : add write(*,*) add(100)end programrecursive integer function add(n) result(sum) implicit none integer,

17、intent(in) : n if ( n0 ) then sum=0return else if ( n=1 ) then sum=nreturn end if sum = n + add(n-1) returnend function5.program main implicit none integer, external : gcd write(*,*) gcd(18,12)end programinteger function gcd(A,B) implicit none integer A,B,BIG,SMALL,TEMP BIG=max(A,B) SMALL=min(A,B) d

18、o while( SMALL /= 1 ) TEMP=mod(BIG,SMALL) if ( TEMP=0 ) exit BIG=SMALL SMALL=TEMP end do gcd=SMALL returnend function6.program main use TextGraphLib implicit none integer, parameter : maxx=60, maxy=20 real, parameter : StartX=0.0, EndX=3.14159*2.0 real, parameter : xinc = (EndX-StartX)/(maxx-1) real

19、 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 do call UpdateScreen() stopend program第九章1.program main implicit none character(len=79) : filename character(len=79) : buffer integer, parameter : filei

20、d = 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 i

21、f ( status/=0 ) exit ! 没有资料就跳出循环 write(*,(A79) buffer count = count+1 if ( count=24 ) then pausecount = 0 end if end do else write(*,*) TRIM(filename), doesnt exist. end if stopend2.program main implicit none character(len=79) : filename character(len=79) : buffer integer, parameter : fileid = 10 in

22、teger 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 !

23、 没有资料就跳出循环 do i=1, len_trim(buffer) buffer(i:i) = char( ichar(buffer(i:i)-3 ) end do write(*,(A70) buffer end do else write(*,*) TRIM(filename), doesnt exist. end if stopend3.program main implicit none type student integer chinese, english, math, science, social, total end type type(student) : s, to

24、tal 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%m

25、ath read(10,rec=(i-1)*subjects+4) s%science read(10,rec=(i-1)*subjects+5) s%socials%total = s%chinese+s%english+s%math+s%science+s%socialtotal%chinese = total%chinese+s%chinesetotal%english = total%english+s%englishtotal%math = total%math+s%mathtotal%science = total%science+s%sciencetotal%social = t

26、otal%social+s%socialtotal%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%

27、total)/real(students) stopend4.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

28、=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(file

29、name), doesnt exist. end if stopend5.module typedef type student integer : numinteger : Chinese, English, Math, Natural, Socialinteger : totalinteger : rank end typeend moduleprogram main use typedef implicit none integer, parameter : fileid=10 integer, parameter : students=20 character(len=80) : te

30、mpstr 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) ! 用循环读入每位学生的成绩

31、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)%Chinesetotal%English = total%English + s(i)%

32、Englishtotal%Math = total%Math + s(i)%Mathtotal%Natural = total%Natural + s(i)%Naturaltotal%Social = total%Social + s(i)%Socialtotal%Total = total%Total + s(i)%Total end do call sort(s,students) ! 重新输出每位学生成绩 write(*,(8A7) 座号,中文,英文,数学,自然,社会,总分,名次 do i=1,students write(*,(8I7) s(i) end do ! 计算并输出平圴分数

33、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) stopend programsubroutine sort(s,n) use typedef implicit none in

34、teger n type(student) : s(n), t integer i,j do i=1,n-1 do j=i+1,n if ( s(i)%total awrite(*,*) p ! 1p=bwrite(*,*) p ! 2p=cp=5write(*,*) c ! 53.module linklist type student integer : numinteger : Chinese, English, Math, Science, Social end type type datalink type(student) : item type(datalink), pointe

35、r : next end typecontains function SearchList(num, head) implicit noneinteger : numtype(datalink), pointer : head, ptype(datalink), pointer : SearchListp=headnullify(SearchList)do while( associated(p) ) if ( p%item%num=num ) then SearchList = preturn end if p=p%nextend doreturn end functionend modul

36、e linklistprogram ex1016 use linklist implicit none character(len=20) : filename character(len=80) : tempstr type(datalink), pointer : head type(datalink), pointer : p type(student), allocatable : s(:) integer i,error,size write(*,*) filename: read(*,*) filename open(10, file=filename, status=old, i

37、ostat=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%itemif ( error/=0 ) exitsize=size+1allocate(p%next, stat=error) ! 新增下一个数据if (

38、 error/=0 ) then write(*,*) Out of memory! stopend ifp=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 (*,*) iif ( isize ) exit ! 输入不合理的座号 write(*,(5(A6,I3) 中文,s(i)%Chinese,& 英文,s(i)%English,& 数学,s(i)%Math,& 自然,s(i)

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 小学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com