最新Fortran95習(xí)題答案資料_第1頁
最新Fortran95習(xí)題答案資料_第2頁
最新Fortran95習(xí)題答案資料_第3頁
最新Fortran95習(xí)題答案資料_第4頁
最新Fortran95習(xí)題答案資料_第5頁
已閱讀5頁,還剩23頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、精品文檔第四章.program mainimplicit nonewrite(*,*) "Have a good time."write(*,*) "That's n ot bad."write(*,*) '"Mary" isn "t my name.'end program2. program mainreal, parameter : Pl=3implicit non e.14159real radiuswrite(*,*)"請輸入半徑長”read(*,*) radiuswrite(*,

2、"(' 面積='f8. 3)") radius*radius*PIend program3. program mainimplicit nonereal gradeswrite(*,*)"請輸入成績”read(*,*) gradeswrite(*,"('調(diào)整后成績?yōu)?#39;f8.3)") SQRT(grades)*10.0end program4.i nteger a,breal ra,rba=2b=3ra=2.0rb=3.0write(*,*) b/a !輸出1,因為使用整數(shù)計算,小數(shù)部分會無條件舍去 write(*

3、,*) rb/ra !輸出 1.55.program mainimplicit nonetype dista neereal meter, i nch, cmend typetype(dista nee) : dwrite(*,*)"請輸入長度:"read(*,*) d%meterd%cm = d%meter*100d%i nch = d%cm/2.54write(*,"(f8.3'米='f8.3'厘米 ='f8.3'英寸')") d%meter, d%cm, d%inchend program第五章l.

4、program mainimplicit nonein teger moneyreal taxwrite(*,*)"請輸入月收入”read(*,*) moneyif ( mo ney<1OOO ) thentax = 0.03else if ( mon ey<5000) thentax = 0.1elsetax = 0.15end ifwrite(*,"('稅金為'18)") nint(money*tax)end program2. program mainimplicit nonein teger daycharacter(le n=

5、20) : tvwrite(*,*)"請輸入星期幾”read(*,*) dayselect case(day)case(1,4)tv ="新聞“case(2,5)tv ="電視劇”case(3,6)tv ="卡通”case(7)tv ="電影"case defaultwrite(*,*)"錯誤的輸入”stopend selectwrite(*,*) tvend program3. program mainimplicit nonein teger age, moneyreal taxwrite(*,*)"請輸入年齡

6、” read(*,*) agewrite(*,*)"請輸入月收入” read(*,*) moneyif ( age<50 ) thenif ( mo ney<1OOO ) thentax = 0.03else if ( mon ey<5000 )thentax = 0.10elsetax = 0.15end ifelseif ( mo ney<1000 ) thentax = 0.5else if ( mon ey<5000 )thentax = 0.7elsetax = 0.10end ifend ifwrite(*,"('稅金為&#

7、39;18)") nint(money*tax) end program4. program mainimplicit nonein teger year, dayslogical mod_4, mod_100, mod_400write(*,*)"請輸入年份”read(*,*) yearmod_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

8、= 366elsedays = 365end ifwrite(*,"('這一年有13'天')") daysstopend gram mainimplicit nonein teger ido i=1,5write(*,*) "Fortra n"end dostopend program2. program mainimplicit nonein teger i,sumsum = 0do i=1,99,2sum = sum+iend dowrite(*,*) sumstopend program3. progr

9、am mainimplicit nonein teger, parameter : an swer = 45in teger, parameter : max = 5in teger weight, ido i=1,maxwrite(*,*)"請輸入體重”read(*,*) weightif ( weight=a nswer ) exit end doif ( i<=max ) the nwrite(*,*)"猜對了 ” elsewrite(*,*)"猜錯了 ”end ifstopend program4. program mainimplicit none

10、in teger, parameter : max=10in teger ireal itemreal ansans = 1.0item = 1.0do i=2,maxitem = item/real(i)ans = an s+itemend dowrite(*,*) ansstopend program5. program mainimplicit nonein teger, parameter : len gth = 79 character" n=len gth) : in put, output in teger i,jwrite(*,*)"請輸入一個字串”read

11、(*,"(A79)") in putj=1do i=1, le n_trim(i nput)if ( in put(i:i) /= ' ' ) the n output(j:j)=i nput(i:i) j=j+1end ifend dowrite(*,"(A79)") outputstopend program第七章1. program mainimplicit nonein teger, parameter : max = 10in teger iin teger : a(max) = (/ (2*i, i=1,10) /)in te

12、ger : t! sum()是 fortran 庫函數(shù)write(*,*) real(sum(a)/real(max)stopend program2.i nteger a(5,5)! 5*5=25in teger b(2,3,4)! 2*3*4=24in teger c(3,4,5,6) ! 3*4*5*6=360in teger d(-5:5)! 11in teger e(-3:3, -3:3)! 7*7=493. program mainimplicit nonein teger, parameter : max=10in teger f(max)in teger if(1)=0f(2)

13、=1do i=3,maxf(i)=f(i-1)+f(i-2)end dowrite(*,"(10I4)") fstopend program4. programmainimplicit nonein teger, parameter : size=10in teger : a(size) = (/ 5,3,6,4,8,7,1,9,2,10 /) in teger : i,jin teger : tdo i=1, size-1do j=i+1, sizeif ( a(i) < a(j) ) then ! a(i)跟 a(j)交換 t=a(i)a(i)=a(j)a(j)=

14、tend ifend doend dowrite(*,"(1OI4)") astopend5. a(2,2) ! 1+(2-1)+(2-1)*(5) = 7a(3,3) ! 1+(3-1)+(3-1)*(5) = 13第八章.program mainimplicit nonereal radius, areawrite(*,*)"請輸入半徑長”read(*,*) radiuscall CircleArea(radius, area)write(*,"(' 面積 ='F8.3)") areastopend programsubro

15、uti ne CircleArea(radius, area)implicit nonereal, parameter : PI=3.14159real radius, areaarea = radius*radius*PIreturnend subrout ine2. program mainimplicit nonereal radiusreal, exter nal : CircleAreawrite(*,*)"請輸入半徑長”read(*,*) radiuswrite(*,"(' 面積 ='F8.3)") CircleArea(radius)

16、stopend programreal fun cti on CircleArea(radius)implicit nonereal, parameter : PI=3.14159real radiusCircleArea = radius*radius*PIreturnend fun cti on3. program mainimplicit nonecall bar(3)call bar(10)stopend programsubrout ine bar(le ngth)implicit nonein teger, i nten t(i n) : len gthin teger ichar

17、acter(le n=79) : stri ngstri ng=""do i=1,le ngthstri ng(i:i)='*'end dowrite(*,"(A79)") stri ngreturnend subrout ine4. program mainimplicit nonein teger, exter nal : addwrite(*,*) add(100)end programrecursive in teger function add( n) result(sum) implicit nonein teger, i n

18、ten t(i n) : nif ( n<0 ) then sum=0 returnelse if ( n<=1 ) the n sum=n returnend ifsum = n + add( n-1)returnend fun cti on5. program mainimplicit nonein teger, exter nal : gcdwrite(*,*) gcd(18,12)end programin teger fun cti on gcd(A,B)implicit nonein teger A,B,BIG,SMALL,TEMPBIG=max(A,B)SMALL=m

19、i n(A,B)do while( SMALL /= 1 )TEMP=mod(BIG ,SMALL)if ( TEMP=0 ) exitBIG=SMALLSMALL=TEMPend dogcd=SMALLreturnend fun cti on6. program mainuse TextGraphLibimplicit nonein teger, parameter : maxx=60, maxy=20real, parameter : StartX=0.0, En dX=3.14159*2.0real, parameter : xi nc = (En dX-StartX)/(maxx-1)

20、 real xin teger i,px,pycall SetScree n(60,20)call SetCurre ntChar('*')x=StartXdo px=1,maxxpy = (maxy/2)*si n(x)+maxy/2+1call PutChar(px,py)x=x+x incend docall UpdateScree n()stopend program第九章.program mainimplicit nonecharacter(le n=79): file namecharacter(le n=79): bufferin teger, parameter

21、 : fileid = 10in teger countin teger : status = 0logical alivewrite(*,*) "File name:"read (*,"(A79)") file nameinquire( file=file name, exist=alive)if ( alive ) the nope n(un it=fileid, file=file name, & access="seque ntial", status="old") count = 0do whil

22、e(.true.)read(u ni t=fileid, fmt="(A79)", iostat=status ) buffer if ( status/=0 ) exit !沒有資料就跳出循環(huán) write(*,"(A79)") buffercount = coun t+1if ( coun t=24 ) the npausecount = 0end ifend doelsewrite(*,*) TRIM(file name)," does n't exist."end ifstopend2. program mainimpl

23、icit nonecharacter(le n=79): file namecharacter(le n=79): bufferin teger, parameter : fileid = 10 in teger iin teger : status = 0logical alivewrite(*,*) "File name:"read (*,"(A79)") file nameinquire( file=file name, exist=alive)if ( alive ) the nope n(un it=fileid, file=file name

24、, &access="seque ntial", status="old")do while(.true.)read(u nit=fileid, fmt="(A79)", iostat=status ) bufferif ( status/=0 ) exit !沒有資料就跳出循環(huán)do i=1, le n_trim(buffer)buffer(i:i) = char( ichar(buffer(i:i)-3 )end dowrite(*,"(A70)") bufferend doelsewrite(*,*)

25、TRIM(file name)," does n't exist."end ifstopend3. program mainimplicit nonetype stude ntin teger chin ese, en glish, math, scie nee, social, totalend typetype(stude nt) : s, totalin teger, parameter : stude nts=20, subjects=5in teger iope n( 10,file="grades.b in ”,access="dir

26、ect",recl=1)write(*,"(7A10)")"座號","中文","英文","數(shù)學(xué)","自然","社會","總分”total = stude nt(O,O,O,O,O,O)do i=1, stude ntsread(10,rec=(i-1)*subjects+1) s%chi neseread(10,rec=(i-1)*subjects+2) s%e nglishread(10,rec=(i-1)*subjects+3) s

27、%mathread(10,rec=(i-1)*subjects+4) s%scie neeread(10,rec=(i-1)*subjects+5) s%socials%total = s%ch in ese+s%e nglish+s%math+s%scie nce+s%social total%ch in ese = total%ch in ese+s%ch in esetotal%e nglish = total%e nglish+s%e nglish total%math = total%math+s%math total%scie nee = total%scie nce+s%scie

28、 nee total%social = total%social+s%social total%total = total%total+s%total write(*,"(7l10)") i, send dowrite(*,"(A10,6F10.3)")" 平均",& real(total%ch in ese)/real(stude nts),& real(total%e nglish)/real(stude nts), & real(total%math)/real(stude nts), & rea

29、l(total%scie nee)/real(stude nts), & real(total%social)/real(stude nts), & real(total%total)/real(stude nts)stopend4. program mainimplicit nonecharacter(le n=79): file namecharacter(le n=79): bufferin teger, parameter : fileid = 10 in teger iin teger : status = 0logical alivewrite(*,*) "

30、;File name:"read (*,"(A79)") file nameinquire( file=file name, exist=alive)if ( alive ) the nope n(un it=fileid, file=file name, & access="seque ntial", status="old") do while(.true.)read(u nit=fileid, fmt="(A79)", iostat=status ) bufferif ( status/=0

31、 ) exit !沒有數(shù)據(jù)就跳出循環(huán) do i=1, le n_trim(buffer)buffer(i:i) = char( ichar(buffer(i:i)-(mod(i-1,3)+1) end do write(*,"(A70)") bufferend doelsewrite(*,*) TRIM(file name)," does n't exist."end ifstopend5. module typedeftype stude ntin teger : numin teger : Chin ese, En glish, Math,

32、Natural, Socialin teger : totalin teger : rankend typeend moduleprogram mai nuse typedefimplicit nonein teger, parameter : fileid=10in teger, parameter : stude nts=20 character" n=80): tempstrtype(student) : s(students) !儲存學(xué)生成績 type(student) : total!計算平均分?jǐn)?shù)用in teger i, num, errorope n( fileid, f

33、ile="grades.txt",status="old", iostat=error) if ( error/=0 ) the nwrite(*,*) "Open grades.txt fail."stopend ifread(fileid, "(A80)") tempstr !讀入第一行文字total=stude nt(O,O,O,O,O,O,O,O)!用循環(huán)讀入每位學(xué)生的成績do i=1,stude ntsread(fileid,*) s(i)% num, s(i)%Chi nese, s(i)%E ngli

34、sh, & s(i)%Math, s(i)%Natural, s(i)%Social!計算總分s(i)%Total = s(i)%Ch in ese + s(i)%E nglish + & s(i)%Math + s(i)%Natural + s(i)%Social!累加上各科的分?jǐn)?shù),計算各科平均時使用 total%Chi nese = total%Ch in ese + s(i)%Chi nese total%E nglish = total%E nglish + s(i)%E nglish total%Math = total%Math + s(i)%Math total%

35、Natural = total%Natural + s(i)%Natural total%Social = total%Social + s(i)%Socialtotal%Total = total%Total + s(i)%Totalend docall sort(s,stude nts)!重新輸出每位學(xué)生成績write(*,"(8A7)")" 座號","中文","英文","數(shù)學(xué)","自然","社會","總分","名次” d

36、o i=1,stude ntswrite(*,"(8I7)") s(i)end do!計算并輸出平均分?jǐn)?shù)write(*,"(A7,6F7.1)")" 平均",&real(total%Chi nese)/real(stude nts),& real(total%E nglish)/real(stude nts),& real(total%Math)/real(stude nts),&real(total%Natural)/real(stude nts),& real(total%Social) /

37、real(stude nts),& real(total%Total) /real(stude nts)stopend programsubrouti ne sort(s ,n)use typedefimplicit nonein teger ntype(stude nt) : s(n), tin teger i,jdo i=1, n-1do j=i+1, nif ( s(i)%total < s(j)%total ) the nt = s(i)s(i)=s(j)s(j) = tend ifend doend doforall(i=1: n) s(i)%ra nk = i end

38、 forallend subrout ine第十章1.i nteger(k in d=4) : a ! 4 bytesreal(k in d=4) : b! 4 bytesreal(ki nd=8) : c! 8 bytescharacter(le n=10) : str! 10 bytesin teger(k in d=4), poin ter : pa ! 4 bytes real(k in d=4), poin ter : pb ! 4 bytes real(k in d=8), poin ter : pc ! 4 bytes character(le n=10), poin ter :

39、 pstr ! 4 bytes type stude ntin teger Chin ese, En glish, Mathend typetype(stude nt) : s ! 12 bytestype(stude nt), poin ter : ps ! 4 bytes2.i nteger, target : a = 1in teger, target : b = 2 in teger, target : c = 3 in teger, poin ter : p p=>a write(*,*) p ! 1 p=>b write(*,*) p ! 2 p=>c p=5 w

40、rite(*,*) c ! 5 3.module lin klisttype stude ntin teger : numin teger : Chin ese, En glish, Math, Scie nee, Social end typetype datali nktype(stude nt) : itemtype(datali nk), poin ter : n extend typecontainsfunction SearchList (num, head)implicit nonein teger : numtype(datali nk), poin ter : head, p

41、 type(datali nk), poin ter : SearchListp=>headn ullify(SearchList)do while( associated(p)if ( p%item% num=num ) the nSearchList => preturnend ifp=>p% nextend doreturnend functionend module lin klistprogram ex1016use lin klistimplicit nonecharacter(le n=20) : file namecharacter(le n=80) : te

42、mpstrtype(datali nk), poin ter : headtype(datali nk), poin ter : ptype(stude nt), allocatable : s(:)in teger i,error,sizewrite(*,*) "file name:"read(*,*) file nameope n( 10, file=file name, status="old", iostat=error)if ( error/=0 ) the nwrite(*,*) "Open file fail!"stop

43、end ifallocate(head)n ullify(head% next)p=>headsize=0read(10, "(A80)") tempstr !讀入第一行字符串,不需要處理它!讀入每一位學(xué)生的成績do while(.true.)read(10,fmt=*, iostat=error) p%itemif ( error/=0 ) exitsize=size+1allocate(p% next, stat=error) ! 新增下一個數(shù)據(jù)if ( error/=0 ) the nwrite(*,*) "Out of memory!"st

44、opend ifp=>p%next !移動到鏈表的下一個數(shù)據(jù)n ullify(p% next)end dowrite(*,"('總共有,13,'位學(xué)生')") sizeallocate( s(size)p=>headdo i=1,sizes(i)=p%item p=>p% nextend dodo while(.true.)write(*,*)"要查詢幾號同學(xué)的成績 ?" read (*,*) iif ( i<1 .or. i>size ) exit ! 輸入不合理的座號 write(*,"

45、(5(A6,l3)")" 中文 ”,s(i)%Chinese,&"英文",s(i)%E nglish, & "數(shù)學(xué)",s(i)%Math, & "自然",s(i)%Scienee,& "社會",s(i)%Social end dowrite(*,"('座號',13,'不存在,程序結(jié)束.')")istopend program4.module typedefimplicit nonetype : datali nk

46、in teger : itype(datali nk), poin ter : n extend type datali nkend module typedefprogram ex1012use typedefimplicit nonetype(datali nk) , poin ter : p, head, nextin teger : i,n,errwrite(*,*) 'Input N:'read(*,*) nallocate( head ) head%i=1n ullify(head% next)p=>headdo i=2, nallocate( p%n ext

47、, stat=err ) if ( err /= 0 ) the nwrite(*,*) 'Out of memory!' stopend ifp=>p% next p%i=iend don ullify(p% next)p=>headdo while(associated(p) write(*, "(i5)" ) p%i p=>p% nextend do!釋放鏈表的存儲空間p=>headdo while(associated(p) next => p%n ext deallocate(p) p=>nextend dost

48、opend program第十一章1. module utilityimplicit nonein terface areamodule procedure CircleArea module procedure RectArea end in terfacecontainsreal function CircleArea(r)real, parameter : Pl=3.14159real rCircleArea = r*r*PIreturnend functionreal function RectArea(a,b)real a,bRectArea = a*breturnend funct

49、ionend moduleprogram mai nuse UTILITYimplicit nonewrite(*,*) area(1.0)write(*,*) area(2.0,3.0)stopend program2. module time_utilityimplicit nonetype : timein teger : hour,m inu te,sec ondend type timein terface operator(+)module procedure add_time_time end in terfacecontainsfunction add_time_time( a

50、, b )implicit nonetype(time) : add_time_time type(time), i nten t( in) : a,b in teger : sec on ds, minu tes,carry sec on ds=a%sec on d+b%sec ond carry=sec on ds/60minu tes=a%m inu te+b%m inu te+carrycarry=mi nu tes/60add_time_time%sec on d=mod(sec on ds,60)add_time_time%mi nu te=mod(mi nu tes,60)add

51、_time_time%hour=a%hour+b%hour+carryreturnend function add_time_timesubrouti ne in put( a )implicit nonetype(time), i nten t(out) : awrite(*,*) " I nput hours:"read (*,*) a%hourwrite(*,*) "In put mi nu tes:"read (*,*) a%mi nutewrite(*,*) "In put sec on ds:"read (*,*) a%s

52、ec ondreturnend subrout ine in putsubrouti ne output( a )implicit nonetype(time), i nten t(i n) : awrite(*, "(13,' hours',13,' mi nutes',13,' seco nds')" ) a%hour,a%mi nute,a%seco ndreturnend subrout ine outputend module time_utilityprogram mai nuse time_utilityimpl

53、icit nonetype(time) : a,b,ccall in put(a)call in put(b)c=a+bcall output(c)stopend program mai n3. module rati on al_utilityimplicit noneprivatepublic : rati on al, &operator(+), operator(-), operator(*),& operator(/), assig nmen t(=),operator(>), & operator(<), operator(=), operato

54、r(/=), & output, i nputtype : rati onalin teger : num, denomend type rati onalin terface operator(+)module procedurerat_rat_plus_ratend in terfacein terface operator(-)module procedure rat_rat_minu s_ratend in terfacein terface operator(*)module procedure rat_rat_times_ratend in terfacein terfac

55、e operator(/)module procedure rat_rat_div_ratend in terfacein terface assig nmen t(=)module procedure rat_eq_ratmodule procedure in t_eq_ratmodule procedure real_eq_ratend in terfacein terface operator(>)module procedure rat_gt_ratend in terfacein terface operator(<)module procedure rat_lt_rat

56、end in terfacein terface operator(=)module procedure rat_compare_rat end in terfacein terface operator(/=)module procedure rat_ ne_rat end in terfacecontainsfun ctio n rat_gt_rat(a,b) implicit nonelogical : rat_gt_rattype(rati on al), inten t(i n) : a,b real : fa,fbfa=real(a% num )/real(a%de nom) fb

57、=real(b% num )/real(b%de nom) if ( fa > fb ) thenrat_gt_rat=.true.elserat_gt_rat=.false.end ifreturnend function rat_gt_ratfun ctio n rat_lt_rat(a,b)implicit nonelogical : rat_lt_rattype(rati on al), inten t(i n) : a,b real : fa,fbfa=real(a% num )/real(a%de nom) fb=real(b% num )/real(b%de nom) if

58、 ( fb > fa ) then rat_lt_rat=.true.elserat_lt_rat=.false.end ifreturnend function rat_lt_ratfunction rat_compare_rat(a,b) implicit nonelogical : rat_compare_rattype(rati on al), inten t(i n) : a,btype(rati on al) : cc=a-bif ( c%num = 0 ) the n rat_compare_rat=.true.elserat_compare_rat=.false.end ifreturnend function rat_compare_ratfunction rat_ ne_rat(a,b)implicit nonelogical : rat_ ne_rattype(rati on al), inten t(i n) : a,btype(rati on al) : cc=a-bif ( c%num=0 ) thenrat_n e_rat

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論