1 / 37

Chapter 3 輸出入及宣告

Chapter 3 輸出入及宣告. < Ex. 完整程式 >. program main write(*,*) "hello, world!" stop end program main. 印出的字串要用 " " 框起來. < Ex. 執行結果 >. hello, world!. 1 write, print 輸出敘述. 填入 數字 或 * :輸出的格式 *表示不特別設定輸出的格式. 填入 數字 或 * :輸出的位置 6 或*表示輸出的位置使用內定值 ( 也就是螢幕 ). 1 write, print 輸出敘述. 程式說明

wilmer
Download Presentation

Chapter 3 輸出入及宣告

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 輸出入及宣告

  2. < Ex. 完整程式> program main write(*,*) "hello, world!" stop end program main 印出的字串要用" "框起來 < Ex. 執行結果> hello, world! 1 write, print輸出敘述

  3. 填入數字或*:輸出的格式*表示不特別設定輸出的格式填入數字或*:輸出的格式*表示不特別設定輸出的格式 填入數字或*:輸出的位置6或*表示輸出的位置使用內定值(也就是螢幕) 1 write, print輸出敘述 程式說明 write (*,*) "hello, world!"

  4. 填入數字或*:輸出的格式*表示不特別設定輸出的格式填入數字或*:輸出的格式*表示不特別設定輸出的格式 1 write, print輸出敘述 完整寫法 write (unit = *, FMT = *) "hello, world" print寫法(只印在螢幕上) print *, "hello, world"

  5. < Ex. 完整程式> program ex0301 implicit none integer :: A real :: B character(len=1) :: C logical :: D A = 1 B = 1.0 C = 'c' D = .TRUE. write(*,*) "A=",A,"B=",B,"C=",C,"D=",D stop end program ex0301 < Ex. 執行結果> A= 1 B= 1.0 C=cD= T 2 宣告(Declaration) 在程式當中,程式設計師要問電腦的作業系統,要求電腦的記憶體中,預留一個存放程式進行所需要資料的空間

  6. < Ex. 完整程式> program ex0302 implicit none integer :: A A = 2+2*4-3 write(*,*) "2+2*4-3=",A stop end program ex0302 以2+2*4-3計算結果來設定變數A的數值 印出變數A的值 < Ex. 執行結果> 2+2*4-3= 7 2 宣告(Declaration) 不同資料型態的宣告 • integer integer :: A宣告一個叫做A的整數變數

  7. <Note> • 省略(kind)敘述時,會以(kind = 4)為integer型態的內定值 • 整數變數小數點的部份無條件忽略 < Ex. 程式片段> < Ex. 執行結果> integer :: A A=3/2 write(*,*)A A=1/2 write(*,*)A 1 0 2 宣告(Declaration) integer :: A,B,C宣告A,B,C皆為整數變數 integer (kind=2) :: A使用兩個位元組來記錄一個整數 integer (kind=4) :: A使用四個位元組來記錄一個整數

  8. <Note> • 省略(kind)敘述時,會以(kind = 4)為real型態的內定值 • 電腦在儲存浮點數時,都會先把它轉成以指數來表示的科學符號型式,其中可儲存6位小數。 <Ex.> 12345 → 1234567 → 2 宣告(Declaration) • real real :: a宣告一個叫做a的單精確度浮點數變數 real (kind=4) :: a宣告一個叫做a的單精確度浮點數變數 real (kind=8) :: a宣告一個叫做a的雙精確度浮點數變數

  9. < Ex. 完整程式> program ex0303 implicit none real :: a, b a = 10000.0 b = 0.0001 write (*,*) a,"+",b,"=",a + b stop end program ex0303 < Ex. 執行結果> 10000.0 + 1.0E-4 = 10000.0 2 宣告(Declaration)

  10. <Note> • 設定複數變數的數值a = (x,y)其中x為實部,y為虛部。 • 省略(kind)敘述時,會以(kind = 4)為complex型態的內定值。 • 複數變數在儲存時,其實是以兩個浮點數的資料型態儲存,因此以(kind = 4)的複數變數必須需要4*2=8 byte來儲存。 < Ex. 程式片段> complex :: a a=(3.2,2.5) write(*,*) a < Ex. 執行結果> (3.2,2.5) 2 宣告(Declaration) • complex complex :: a宣告一個叫做a的單精確度複數變數 complex (kind=4) :: a宣告一個叫做a的單精確度複數變數 complex (kind=8) :: a宣告一個叫做a的雙精確度複數變數

  11. 宣告一個叫做a的字元變數 宣告一個叫做a的字元變數 字串長度為80個字元 <Note> • 設定字元變數的數值a = " xxx "或 a = ' xxx ' < Ex. 程式片段> < Ex. 執行結果> character (20) :: a a= "hello, world" write(*,*) a hello, world 2 宣告(Declaration) • character character (len = 1) :: a character (1) :: a character*1 :: a character (len = 80) :: a character (80) :: a character*80 :: a

  12. <Note> • 設定邏輯變數的數值只有a = .TRUE.(真值)或 a = .FALSE.(假值) < Ex. 程式片段> < Ex. 執行結果> logical :: a,b a = .TRUE. b = .FALSE. write(*,*) a,b T F 2 宣告(Declaration) • logical logical :: a宣告一個叫做a的邏輯變數

  13. 2 宣告(Declaration) 使用變數名稱之原則 • 變數的名稱以英文字母為原則,可以內含下底線(_)及數字,但不能以數字來起頭。 • 變數名字的長度,只有前31個字元有效。 • 變數的名稱不能和Fortran的執行指令同名。 • 程式中辨認變數時,不會區分它的大小寫。 • 變數名稱最好具有意義,可提高程式可讀性。

  14. < Ex. 完整程式> program ex0304 integer :: A real :: B write (*,*) "Please input a (integer) number:" read (*,*) A write (*,*) "Please input a (real) number:" read (*,*) B write (*,*) A, B stop end program ex0304 < Ex. 執行結果> Please input a (integer) number:120 <輸入 1 2 0 [ENTER] >Please input a (real) number:0.25 <輸入 0 . 2 5 [ENTER]>120 0.25 3 read 輸入敘述

  15. 填入數字或*:輸入的格式*表示不特別設定輸入的格式填入數字或*:輸入的格式*表示不特別設定輸入的格式 填入數字或*:輸入的位置5或*表示輸入的位置使用內定值(也就是鍵盤) 3 read 輸入敘述 程式說明 read (*,*) A

  16. < Ex. 完整程式> < Ex. 執行結果> program ex0305 integer :: A,B,C read (*,*) A,B,C write (*,*) A,B,C stop end program ex0305 1 23 456<輸入 1 [SPACE] 2 3 [SPACE] 4 5 6 [ENTER] >1 23 456 或1,23,456<輸入 1 , 2 3 , 4 5 6 [ENTER] >1 23 456 3 read 輸入敘述 完整寫法 read (unit = *, FMT = *) A 多變數輸入 在變數間以空格( )或逗號(,)隔開

  17. < Ex. 完整程式> program main write(*,100) "hello" 100 format(A10) stop end program main 可合併寫成write(*,’(A10)’)”hello” < Ex. 執行結果> □□□□□hello < □表空格> 4 格式化輸出入 (Format)

  18. < Ex. 程式片段> < Ex. 程式片段> < Ex. 執行結果> < Ex. 執行結果> write(*,'(A10)')"hello" write(*,'(D15.7)')123.45 □□□□□hello □□0.1234500D+03 10 7 15 4 格式化輸出入 (Format) 重要格式控制敘述 • Ap以p個字元寬來輸出字串。 • Dp.q以p個字元的寬度來輸出指數型態的浮點數,小數部分佔q個字元寬 。(p≧q+7)

  19. < Ex. 程式片段> < Ex. 程式片段> < Ex. 執行結果> < Ex. 執行結果> write(*,'(E15.7)')123.45 write(*,'(F8.4)')123.45 □□0.1234500E+03 123.4500 7 15 4 8 4 格式化輸出入 (Format) 重要格式控制敘述 • Ep.q以p個字元的寬度來輸出指數型態的浮點數,小數部分佔q個字元寬。 (p≧q+7) • Fp.q以p個字元的寬度來輸出浮點數,小數部分佔q個字元寬。

  20. < Ex. 程式片段> < Ex. 程式片段> < Ex. 執行結果> < Ex. 執行結果> write(*,'(I5)')100 write(*,'(L5)') .TRUE. □□100 □□□□T 5 5 < Ex. 程式片段> < Ex. 執行結果> write(*,'(5X,I3)') 100 □□□□□100 5 4 格式化輸出入 (Format) 重要格式控制敘述 • Ip以p個字元的寬度來輸出整數。 • Lp以p個字元的寬度來輸出T或F的真假值。 • pX把輸出的位置向下跳過p個位置,配合其他格式使用。

  21. < Ex. 程式片段> < Ex. 執行結果> write(*,'(I3)')10000 *** < Ex. 程式片段> < Ex. 執行結果> write(*,'(1X,"Ans=",I3)')10 □Ans= □10 write(*,100) AGE 100 format( 1X,"You are ", I4,"years old. " ) ≡ write(*, '(1X, "You are ", I4, "years old. " )') AGE 4 格式化輸出入 (Format) 格式化輸出入其他注意事項 • 欄位不足,會輸出*。 • 格式化輸出入可混合使用,也可以插入字串。 • 簡略寫法write(*,'( 3(2X, F5.2) )') A, B, C≡ write(*,'( 2X, F5.2, 2X, F5.2, 2X, F5.2 )') A, B, C • 合併寫法

  22. 5 宣告的其它補述 implicit內定型態 • implict 第一個字母為I,J,K,L,M,N的變數內定成整數型態,其它的變數則 被當成浮點數來使用。 • implict integer (A-F, I, K) 把A到F開頭及I, K開頭的變數都當成整數。 • implict integer (A-F, I, K) 把F到K開頭的變數都當成浮點數。 • implict integer (A-F, I, K) 把“內定型態“的功能關閉。

  23. < Ex. 完整程式> program ex0307 implicit none real, parameter :: pi = 3.14159 write(*,'(1X,A10,F5.2)') 'sin(pi/6)=', sin(pi/6.0) stop end program ex0307 < Ex. 執行結果> sin(pi/6)= 0.50 5 宣告的其它補述 parameter常數宣告

  24. 常數必須給予初始值否則沒有功用 加入parameter表示pi是常數,不佔記憶體,因為不是變數,程式執行中不能改變其值。 5 宣告的其它補述 parameter常數宣告 real, parameter :: pi = 3.14159

  25. < Ex. 完整程式> program ex0308 implicit none integer :: a = 1 real :: b = 1.0 complex :: c = (1.0, 1.0) character(len=1) :: d = 'A' logical :: e = .TRUE. write (*,'(I3,TR1,F4.2,TR1,(F4.2,TR1,F4.2),A2,L3)') a,b,c,d,e stop end program ex0308 < Ex. 執行結果> 1 1.00 1.00 1.00 A T 5 宣告的其它補述 設定變數的初值

  26. < Ex. 完整程式> 程式開始→ 主程式碼  程式結束→ 主程式碼結束→ program ex0309 implicit none integer :: A = 2, B = 1 real :: C C = B / A write(*,*) C stop end program ex0309 ←implicit要放在宣告的最前端 開始變數宣告 主程式敘述區 必須先將整數型態轉換成浮點數型態,才能算出0.5的值 C=real(B)/real(A) < Ex. 執行結果> 0.0E+0 5 宣告的其它補述 宣告在程式中的結構

  27. < Ex. 完整程式> write(*,*) 'Input his(her) name:' read(*,*) a% name write(*,*) 'Input his(her) age:' read(*,*) a% age write(*,*) 'Input his(her) body length:' read(*,*) a% length write(*,*) 'His(her) name is ', a% name write(*,*) 'His(her) age is ', a% age stop end program ex0310 program ex0310 implicit none type :: person character(len=30) :: name integer :: age integer :: length integer :: weight character(len=80) ::address end type person type(person) :: a <接右> 5 宣告的其它補述 自訂資料型態

  28. 開始宣告一個叫做"person"的資料型態 < Ex. 執行結果> Input his(her) name:Tom <輸入 T o m [ENTER] >Input his(her) age:15 <輸入 1 5 [ENTER] >Input his(her) body length:170 <輸入 1 7 0 [ENTER] >His(her) name is TomHis(her) age is 15 自訂資料型態結束 使用自訂資料型態宣告變數 使用自訂資料型態的變數 5 宣告的其它補述 自訂資料型態 type :: person character(len=30) :: name … end type person type(person) :: a read(*,*) a% name

  29. type a%nam a%lenth a%address a%age a%weight 5 宣告的其它補述 自訂資料型態初始化 a = person("Tom", 15, 170, 60, "Taipei")

  30. < Ex. 執行結果> 產生constants.modvars.mod兩個模組檔 PI = 3.14159 G = 9.812 PI = 6.28318 < Ex. 完整程式> program ex0312 use constants use vars implicit none write(*,*) 'PI = ', pi write(*,*) 'G = ', g pi_2 = 2.0* pi write(*,*) '2 PI = ', pi_2 stop end program ex0312 module constants implicit none real, parameter :: pi= 3.14159 real, parameter :: g = 9.81 end module constants module vars implicit none real :: pi_2 end module vars <接右> 5 宣告的其它補述 module模組

  31. 自訂一個叫做“constants”的module區塊 在程式一開始宣告要使用那個模組,之後在程式中就可以使用模組的內容。 5 宣告的其它補述 module模組 module constants implicit none … end module constants program ex0311 use constants …

  32. < Ex. 完整程式> module typedef implicit none type :: person character(len=30) :: name integer :: age real :: length real :: weight end type person end module typedef 自訂一個module區塊 在module內宣告一個叫做“person”的自訂資料型態 <接下頁> 5 宣告的其它補述 module模組

  33. < Ex. 執行結果> 產生typedef.mod的模組檔 Input his name:Tom <輸入 T o m [ENTER] >Input hes age:15 <輸入 1 5 [ENTER] > Name :Tom Age : 15 < Ex. 完整程式> program ex0311 use typedef type(person) :: a write(*,*) 'Input his name:' read(*,'(A30)') a%name write(*,*) 'Input hes age:' read(*,*) a%age write(*,'(1X, A12, A20)') 'Name :', a%name write(*,'(1X, A12, I6)') 'Age :', a%age stop end program ex0311 使用模組後就可以使用模組內的自訂資料型態來宣告 5 宣告的其它補述 module模組

  34. 5 宣告的其它補述 kind 的使用 在PC上資料型態宣告變數可儲存值的範圍 integer(kind=1) -127~127 integer(kind=2) -32767~32767 integer(kind=4) -2147483647~2147483647 real(kind=4) ~ real(kind=8) ~

  35. 增加程式碼"跨平台"能力 5 宣告的其它補述 kind 的使用 selected_int_kind(p) 傳回記錄p位整數時,所應宣告的kind值。 selected_real_kind(p,q) 傳回記錄p位有效位數,指數達到q的浮點數所需的kind值。

  36. < Ex. 完整程式> module kind_var implicit none integer, parameter :: long_int=selected_int_kind(9) integer, parameter :: short_int=selected_int_kind(3) integer, parameter :: long_real=selected_real_kind(10,50) integer, parameter :: short_real=selected_real_kind(3,3) end module kind_var <接下頁> 5 宣告的其它補述 kind 的使用

  37. < Ex. 完整程式> program ex0313 use kind_var implicit none integer(kind=long_int) :: a=12345678 integer(kind=short_int) :: b=12 real(kind=long_real) :: c=1.23456789D45 real(kind=short_real) :: d=1230 write(*,'(I10)') a write(*,'(I10)') b write(*,'(E15.5)') c write(*,'(E15.5)') d stop end program ex0313 kind=4 kind=2 kind=8 < Ex. 執行結果> 產生kind_var.mod的模組檔 12345678 12 0.12346E+46 0.12300E+04 kind=4 5 宣告的其它補述 kind 的使用

More Related