2022年Fortran語言編程小結(jié)_第1頁
2022年Fortran語言編程小結(jié)_第2頁
2022年Fortran語言編程小結(jié)_第3頁
2022年Fortran語言編程小結(jié)_第4頁
2022年Fortran語言編程小結(jié)_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、1.單雙精度Program ex01implicit nonereal(kind=4) : areal(kind=8) : ba=3._4 ! 擬定這個(gè)數(shù)字是使用單精度b=3._8 ! 擬定這個(gè)數(shù)字是使用雙精度write(*,*) a,bEnd program ex012.判斷kind值program ex02Implicit none ! 判斷可以記錄9個(gè)位數(shù)的整數(shù)kind值integer, parameter : long_int = selected_int_kind( 9 ) ! 判斷可以記錄3個(gè)位數(shù)的整數(shù)kind值integer, parameter : short_int = se

2、lected_int_kind( 3 ) ! 判斷可以有3個(gè)有效位數(shù), 指數(shù)可以記錄到3的浮點(diǎn)數(shù)kind值integer, parameter : long_real = selected_real_kind( 10, 50 )! 判斷可以有10個(gè)有效位數(shù), 指數(shù)可以記錄到50的浮點(diǎn)數(shù)kind值integer, parameter : short_real= selected_real_kind( 3, 3 )integer(kind=long_int) : a = 123456integer(kind=short_int) : b = 123real(kind=long_real) : c

3、= +1.23456789D45real(kind=short_real) : d =+1230write(*, (I3,1X,I10) ) long_int, awrite(*, (I3,1X,I10) ) short_int, bwrite(*, (I3,1X,E10.5) ) long_real, cwrite(*, (I3,1X,E10.5) ) short_real, dEND3.TYPEprogram ex0434implicit noneType : person! 開始建立person這個(gè)類型 character(len=30) : name ! 人名 integer : ag

4、e ! 年齡 integer : height ! 身高 INTEGER : weight ! 體重 character(len=80) : address ! 地址End type persontype(person) : a! 聲明一種person類型的變量write(*,*) NAME:read(*,*) a%name write(*,*) AGE:read(*,*) a%agewrite(*,*) HEIGHT:read(*,*) a%heightwrite(*,*) WEIGHT:read(*,*) a%weightwrite(*,*) ADDRESS:read(*,*) a%add

5、resswrite(*,100) a%name,a%age,a%height,a%weight100 format(Name:,A10/,Age:,I3/,Height:,I3/,Weight:,I3/,& Addres:,A80)End4.REAL 與 INTEGERProgram ex0431implicit none integer : a=1 integer : b=2 real : c c=a/b ! c=1/2=0, 雖然c是浮點(diǎn)數(shù),但由于a,b是整數(shù),計(jì)算a/b時(shí)會(huì)用整數(shù)去計(jì)算. write(*,(F5.2) c End5.DATA 變量表/初值表/,變量表/初值表/,PROGR

6、AM ex0430IMPLICIT NONEINTEGER AREAL BCOMPLEX CCHARACTER(20) STRDATA A,B,C,STR /1, 2.0, (1.0,2.0), FORTRAN 77/ WRITE(*,*) A,B,C,STREND6.復(fù)數(shù)實(shí)虛部Program ex0430complex : c = (1,2)write(*,100)real(c),+,aimag(c),i100 format(f5.1,a1,f5.1,a1)End7.邏輯輸出Program ex0416 logical a,b a=.true. b=.false. write(*,100)

7、a,b 100 format(L5,L4)End8.Program ex0415 character(len=20) string character(len=5) substring string = Have a nice day. substring = nice write(*,*) ichar(A) ! 輸出字符A的ASCII碼 write(*,*) char(65) ! 輸出ASCII碼65所代表的字符,也就是A write(*,*) len(string) ! 輸出字符串string聲明時(shí)的長度 write(*,*) len_trim(string) ! 輸出字符串string內(nèi)

8、容的長度 write(*,*) index(string, substring) ! nice在Have a nice day的第8個(gè)位置End9.Program ex0414 character(len= 6) first character(len=10) second character(len=20) add first=Happy second=Birthday add = first/second ! 經(jīng)由兩個(gè)持續(xù)的除號可以連接兩個(gè)字符串 write(*,100) add 100 FORMAT(生日快樂,/,A)End10.帶精度計(jì)算Program ex0408 real(kind

9、=8) : a,b a=1 b=0.1 write(*,*) a,+,b,=,a+bEnd11.邏輯if語句Program ex0504implicit none integer rain, windspeed logical r,w write(*,*) Rain: read(*,*) rain write(*,*) Wind: read(*,*) windspeed r = (rain=500) ! 如果rain=150, r=.true, 否則r=.false. w = (windspeed=10) ! 如果windspeed=10, w=.true, 否則w=.false. if (

10、r .or. w ) then ! 只要r或w有一種值是true就成立 write(*,*) 停止上班上課 else write(*,*) 照常上班上課End ifEnd12.Select Case用法Program ex0512implicit none integer : score character grade write(*,*) Score: read(*,*) score select case(score) case(90:100) grade=A case(80:89) grade=B case default grade=? End select write(*,(Grad

11、e:,A1) gradeEnd13.IF GOTO語句PROGRAM ex0514IMPLICIT NONEREAL height REAL weight WRITE(*,*) height:READ(*,*) height WRITE(*,*) weight:READ(*,*) weight IF ( weight height-100 ) GOTO 200100WRITE(*,*) Under control. GOTO 300 200 WRITE(*,*) Too fat!300STOPEND14.DO WHILE 循環(huán)Program ex0604implicit none intege

12、r, parameter : limit=10 integer counter integer : ans = 0 counter = 2 do while( counter J ) a(I,J)=1 ! 上半部分 forall ( I=1:size, J=1:size, I=J ) a(I,J)=2 ! 對角線 forall ( I=1:size, J=1:size, IJ ) a(I,J)=3 !下半部分 write(*,(5(5I5,/) a End21.Allocatable Program EXImplicit none integer : size, error=0 integer

13、, parameter : one_mb=1024*1024 ! 1MB character, allocatable : a(:) Do size=size+one_mb ! allocate( a(size), stat=error ) if ( error/=0 ) exit write(*,(Allocate ,I10, bytes) sizewrite(*,(F10.2, MB used) real(size)/real(one_mb) deallocate( a ) End doEndFunction 函數(shù)Program eximplicit none real : a=10 re

14、al : b=20 real : add write(*,(f6.2) add(a,b)EndFunction add(a,b)implicit none real : a,b real : add add = a*bEndSAVE語句Program exImplicit none call sub() call sub() call sub()End programSubroutine sub() Implicit none Integer : count = 1 Save count ! 指定save變量永遠(yuǎn)活著,不會(huì)忘掉它的內(nèi)容 Write(*,*) count count = coun

15、t+1End 運(yùn)營成果:1 2 3 24.生成隨機(jī)數(shù)program eximplicit none interface ! 定義函數(shù)的接口 function random10(lbound, ubound)implicit nonereal : lbound, uboundreal : random10(10) end function end interface real : a(10) CALL RANDOM_SEED() ! 系統(tǒng)根據(jù)日期和時(shí)間隨機(jī)地提供種子 a = random10(1.0, 10.0) write(*,(10F6.2) a endfunction random10(l

16、bound, ubound)implicit none real : lbound, ubound real : len real : random10(10) real t integer i len = ubound - lbound ! 計(jì)算范疇大小 do i=1,10 call random_number(t) ! t會(huì)是01之間的隨機(jī)數(shù) random10(i) = lbound + len * t ! 把t轉(zhuǎn)換成lboundubound間的隨機(jī)數(shù) end doEndMODULE語句Module global implicit none integer a,b common a,bEn

17、d moduleProgram ex0834 use global implicit none a=1 b=2 call sub()End programSubroutine sub() use global implicit none write(*,*) a,b returnEnd subroutine26.寫文獻(xiàn)到textprogram ex0902 implicit none character(len=20) : string open(unit=10, file=test.txt) write(10,(A20) I LOVE YOU. ! 寫到文獻(xiàn)中 rewind(10) read(10,(A20) string ! 從文獻(xiàn)中讀出來 write(*,(A2

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(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)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論