1 write print Ex program main write hello

  • Slides: 37
Download presentation

1 write, print輸出敘述 < Ex. 完整程式> program main write(*, *) "hello, world!" stop end

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

2 宣告(Declaration) 不同資料型態的宣告 a) integer : : A 宣告一個叫做A的整數變數 < Ex. 完整程式> program ex

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

2 宣告(Declaration) < Ex. 完整程式> program ex 0303 implicit none real : : a,

2 宣告(Declaration) < Ex. 完整程式> program ex 0303 implicit none real : : a, b a = 10000. 0 b = 0. 0001 write (*, *) a, "+", b, "=", a + b stop end program ex 0303 < Ex. 執行結果> 10000. 0 + 1. 0 E-4 = 10000. 0

2 宣告(Declaration) d) character (len = 1) : : a 宣告一個叫做a的字元變數 character (1) :

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

3 read 輸入敘述 < Ex. 完整程式> program ex 0304 integer : : A real

3 read 輸入敘述 < Ex. 完整程式> program ex 0304 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 ex 0304 < 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 輸入敘述 完整寫法 read (unit = *, FMT = *) A 多變數輸入 在變數間以空格(

3 read 輸入敘述 完整寫法 read (unit = *, FMT = *) A 多變數輸入 在變數間以空格( )或逗號(, )隔開 < Ex. 完整程式> program ex 0305 integer : : A, B, C read (*, *) A, B, C write (*, *) A, B, C stop end program ex 0305 < Ex. 執行結果> 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

4 格式化輸出入 (Format) < Ex. 完整程式> program main write(*, 100) "hello" 100 format(A 10)

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

5 宣告的其它補述 parameter常數宣告 < Ex. 完整程式> program ex 0307 implicit none real, parameter :

5 宣告的其它補述 parameter常數宣告 < Ex. 完整程式> program ex 0307 implicit none real, parameter : : pi = 3. 14159 write(*, '(1 X, A 10, F 5. 2)') 'sin(pi/6)=', sin(pi/6. 0) stop end program ex 0307 < Ex. 執行結果> sin(pi/6)= 0. 50

5 宣告的其它補述 設定變數的初值 < Ex. 完整程式> program ex 0308 implicit none integer : :

5 宣告的其它補述 設定變數的初值 < Ex. 完整程式> program ex 0308 implicit none integer : : a = 1 < Ex. 執行結果> 1 1. 00 A T real : : b = 1. 0 complex : : c = (1. 0, 1. 0) character(len=1) : : d = 'A' logical : : e =. TRUE. write (*, '(I 3, TR 1, F 4. 2, TR 1, (F 4. 2, TR 1, F 4. 2), A 2, L 3)') a, b, c, d, e stop end program ex 0308

5 宣告的其它補述 自訂資料型態 < Ex. 完整程式> write(*, *) 'Input his(her) name: ' program ex

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

5 宣告的其它補述 自訂資料型態 開始宣告一個叫做 "person"的資料型態 type : : person character(len=30) : : name 自訂資料型態結束

5 宣告的其它補述 自訂資料型態 開始宣告一個叫做 "person"的資料型態 type : : person character(len=30) : : name 自訂資料型態結束 … < Ex. 執行結果> end type person type(person) : : a read(*, *) a% name 使用自訂資料 型態宣告變數 使用自訂資料 型態的變數 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 Tom His(her) age is 15

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

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

5 宣告的其它補述 < Ex. 執行結果> module模組 < Ex. 完整程式> program ex 0312 module constants

5 宣告的其它補述 < Ex. 執行結果> module模組 < Ex. 完整程式> program ex 0312 module constants implicit none use constants real, parameter : : pi= 3. 14159 use vars real, parameter : : g = 9. 81 implicit none 產生 constants. mod vars. mod 兩個模組檔 PI = 3. 14159 G = 9. 81 2 PI = 6. 28318 write(*, *) 'PI = ', pi end module constants write(*, *) 'G = ', g pi_2 = 2. 0* pi module vars implicit none write(*, *) '2 PI = ', pi_2 real : : pi_2 stop end module vars <接右> end program ex 0312

5 宣告的其它補述 module模組 module constants implicit none … 自訂一個叫做“constants” 的module區塊 end module constants program

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

5 宣告的其它補述 module模組 < Ex. 完整程式> module typedef 自訂一個module區塊 implicit none type : :

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

5 宣告的其它補述 module模組 < Ex. 完整程式> < Ex. 執行結果> 產生typedef. mod的模組檔 program ex 0311

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

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

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

5 宣告的其它補述 kind 的使用 < Ex. 完整程式> module kind_var implicit none integer, parameter :

5 宣告的其它補述 kind 的使用 < 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 宣告的其它補述 < Ex. 完整程式> kind 的使用 program ex 0313 use kind_var implicit none

5 宣告的其它補述 < Ex. 完整程式> kind 的使用 program ex 0313 use kind_var implicit none kind=4 integer(kind=long_int) : : a=12345678 kind=2 integer(kind=short_int) : : b=12 real(kind=long_real) : : c=1. 23456789 D 45 real(kind=short_real) : : d=1230 write(*, '(I 10)') a write(*, '(I 10)') b write(*, '(E 15. 5)') c write(*, '(E 15. 5)') d stop end program ex 0313 kind=8 < Ex. 執行結果> 產生kind_var. mod的模組檔 kind=4 12345678 12 0. 12346 E+46 0. 12300 E+04