37
Chapter 3 輸輸輸輸輸輸

Chapter 3 輸出入及宣告

  • Upload
    wilmer

  • View
    47

  • Download
    0

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Chapter 3  輸出入及宣告

Chapter 3 輸出入及宣告

Page 2: Chapter 3  輸出入及宣告

1 write, print 輸出敘述< Ex. 完整程式 >

program main

write(*,*) "hello, world!"

stop

end program main

< Ex. 執行結果 >

hello, world!

印出的字串要用 " " 框起來

Page 3: Chapter 3  輸出入及宣告

1 write, print 輸出敘述程式說明write (*,*) "hello, world!"

填入數字或 *:輸出的位置6或 *表示輸出的位置使用內定值 (也就是螢幕 )

填入數字或 *:輸出的格式*表示不特別設定輸出的格式

Page 4: Chapter 3  輸出入及宣告

1 write, print 輸出敘述完整寫法write (unit = *, FMT = *) "hello, world"

print 寫法 ( 只印在螢幕上 )print *, "hello, world"

填入數字或 *:輸出的格式*表示不特別設定輸出的格式

Page 5: Chapter 3  輸出入及宣告

2 宣告 (Declaration)< 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

在程式當中,程式設計師要問電腦的作業系統,要求電腦的記憶體中,預留一個存放程式進行所需要資料的空間

Page 6: Chapter 3  輸出入及宣告

2 宣告 (Declaration)

不同資料型態的宣告a) integer

integer :: A 宣告一個叫做 A 的整數變數< Ex. 完整程式 >

program ex0302

implicit none

integer :: A

A = 2+2*4-3

write(*,*) "2+2*4-3=",A

stop

end program ex0302

< Ex. 執行結果 >2+2*4-3= 7

以 2+2*4-3 計算結果來設定變數 A 的數值

印出變數 A 的值

Page 7: Chapter 3  輸出入及宣告

2 宣告 (Declaration)integer :: A,B,C 宣告 A,B,C 皆為整數變數integer (kind=2) :: A 使用兩個位元組來記錄一個整數integer (kind=4) :: A 使用四個位元組來記錄一個整數<Note>1) 省略 (kind) 敘述時,會以 (kind = 4) 為 integer

型態的內定值

2) 整數變數小數點的部份無條件忽略

< Ex. 程式片段 >integer :: A

A=3/2

write(*,*)A

A=1/2

write(*,*)A

< Ex. 執行結果 >

1

0

Page 8: Chapter 3  輸出入及宣告

2 宣告 (Declaration)

b) realreal :: a 宣告一個叫做 a 的單精確度浮點數變數real (kind=4) :: a 宣告一個叫做 a 的單精確度浮點數變數real (kind=8) :: a 宣告一個叫做 a 的雙精確度浮點數變數<Note>1) 省略 (kind) 敘述時,會以 (kind = 4) 為 real 型態

的內定值

2) 電腦在儲存浮點數時,都會先把它轉成以指數來表示的科學符號型式,其中可儲存 6 位小數。<Ex.>

12345 →

1234567 →

50.123450 1080.123457 10

Page 9: Chapter 3  輸出入及宣告

2 宣告 (Declaration)

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

< Ex. 完整程式 >

Page 10: Chapter 3  輸出入及宣告

2 宣告 (Declaration)

c) complexcomplex :: a 宣告一個叫做 a 的單精確度複數變數complex (kind=4) :: a 宣告一個叫做 a 的單精確度複數變數complex (kind=8) :: a 宣告一個叫做 a 的雙精確度複數變數<Note>1) 設定複數變數的數值

a = (x,y) 其中 x 為實部, y 為虛部。

2) 省略 (kind) 敘述時,會以(kind = 4) 為 complex 型態的內定值。

3) 複數變數在儲存時,其實是以兩個浮點數的資料型態儲存,因此以 (kind = 4)的複數變數必須需要 4*2=8 byte 來儲存。

< Ex. 程式片段 >complex :: a

a=(3.2,2.5)

write(*,*) a

< Ex. 執行結果 >(3.2,2.5)

Page 11: Chapter 3  輸出入及宣告

2 宣告 (Declaration)

d) charactercharacter (len = 1) :: a

character (1) :: a

character*1 :: a

character (len = 80) :: a

character (80) :: a

character*80 :: a<Note>1) 設定字元變數的數值

a = " xxx " 或 a = ' xxx '< Ex. 程式片段 >character (20) :: a

a= "hello, world"

write(*,*) a

< Ex. 執行結果 >hello, world

宣告一個叫做 a的字元變數

宣告一個叫做 a的字元變數字串長度為 80個字元

Page 12: Chapter 3  輸出入及宣告

2 宣告 (Declaration)

e) logicallogical :: a 宣告一個叫做 a 的邏輯變數<Note>1) 設定邏輯變數的數值只有

a = .TRUE. (真值)或 a = .FALSE. (假值)< Ex. 程式片段 >logical :: a,b

a = .TRUE.

b = .FALSE.

write(*,*) a,b

< Ex. 執行結果 >T F

Page 13: Chapter 3  輸出入及宣告

2 宣告 (Declaration)

使用變數名稱之原則1) 變數的名稱以英文字母為原則,可以內含下底線 (_) 及數字,

但不能以數字來起頭。2) 變數名字的長度,只有前 31 個字元有效。3) 變數的名稱不能和 Fortran 的執行指令同名。4) 程式中辨認變數時,不會區分它的大小寫。5) 變數名稱最好具有意義,可提高程式可讀性。

Page 14: Chapter 3  輸出入及宣告

3 read 輸入敘述< 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

Page 15: Chapter 3  輸出入及宣告

3 read 輸入敘述程式說明read (*,*) A

填入數字或 *:輸入的位置5或 *表示輸入的位置使用內定值 (也就是鍵盤 )

填入數字或 *:輸入的格式*表示不特別設定輸入的格式

Page 16: Chapter 3  輸出入及宣告

3 read 輸入敘述完整寫法read (unit = *, FMT = *) A

多變數輸入在變數間以空格 ( ) 或逗號 (,) 隔開

program ex0305

integer :: A,B,C

read (*,*) A,B,C

write (*,*) A,B,C

stop

end program ex0305

< 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

< Ex. 完整程式 >

Page 17: Chapter 3  輸出入及宣告

4 格式化輸出入 (Format)

< Ex. 完整程式 >

program main

write(*,100) "hello"

100 format(A10)

stop

end program main

< Ex. 執行結果 >□□□□□hello < □ 表空格 >

可合併寫成 write(*,’(A10)’)”hello”

Page 18: Chapter 3  輸出入及宣告

4 格式化輸出入 (Format)

重要格式控制敘述a) Ap

以 p 個字元寬來輸出字串。

b) Dp.q以 p 個字元的寬度來輸出指數型態的浮點數,小數部分佔 q個字元寬 。 (p≧q+7)

< Ex. 程式片段 >write(*,'(A10)')"hello"

< Ex. 執行結果 >□□□□□hello

10

< Ex. 程式片段 >write(*,'(D15.7)')123.45

< Ex. 執行結果 >□□0.1234500D+03

157

Page 19: Chapter 3  輸出入及宣告

4 格式化輸出入 (Format)

重要格式控制敘述c) Ep.q

以 p 個字元的寬度來輸出指數型態的浮點數,小數部分佔 q個字元寬。 (p≧q+7)

d) Fp.q以 p 個字元的寬度來輸出浮點數,小數部分佔 q 個字元寬。

< Ex. 程式片段 >write(*,'(E15.7)')123.45

< Ex. 執行結果 >□□0.1234500E+03

157

< Ex. 程式片段 >write(*,'(F8.4)')123.45

< Ex. 執行結果 >123.4500

84

Page 20: Chapter 3  輸出入及宣告

4 格式化輸出入 (Format)

重要格式控制敘述e) Ip

以 p 個字元的寬度來輸出整數。

f) Lp以 p 個字元的寬度來輸出 T 或 F 的真假值。

g) pX把輸出的位置向下跳過 p 個位置,配合其他格式使用。

< Ex. 程式片段 >write(*,'(I5)')100

< Ex. 執行結果 >□□100

< Ex. 程式片段 >write(*,'(L5)') .TRUE.

< Ex. 執行結果 >□□□□T

5

5

< Ex. 程式片段 >write(*,'(5X,I3)') 100

< Ex. 執行結果 >□□□□□100

5

Page 21: Chapter 3  輸出入及宣告

4 格式化輸出入 (Format)

格式化輸出入其他注意事項1) 欄位不足,會輸出 * 。

2) 格式化輸出入可混合使用,也可以插入字串。

3) 簡略寫法 write(*,'( 3(2X, F5.2) )') A, B, C≡ write(*,'( 2X, F5.2, 2X, F5.2, 2X, F5.2 )') A, B, C

4) 合併寫法

< Ex. 程式片段 >write(*,'(I3)')10000

< Ex. 執行結果 >***

< Ex. 程式片段 >write(*,'(1X,"Ans=",I3)')10

< Ex. 執行結果 >□Ans= □10

≡ write(*, '(1X, "You are ", I4, "years old. " )') AGE

write(*,100) AGE

100 format( 1X,"You are ", I4,"years old. " )

Page 22: Chapter 3  輸出入及宣告

5 宣告的其它補述implicit 內定型態a) implict

第一個字母為 I,J,K,L,M,N 的變數內定成整數型態,其它的變數則

被當成浮點數來使用。

b) implict integer (A-F, I, K)把 A 到 F 開頭及 I, K 開頭的變數都當成整數。

c) implict integer (A-F, I, K)把 F 到 K 開頭的變數都當成浮點數。

d) implict integer (A-F, I, K)把“內定型態“的功能關閉。

Page 23: Chapter 3  輸出入及宣告

5 宣告的其它補述parameter 常數宣告

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

< Ex. 完整程式 >

Page 24: Chapter 3  輸出入及宣告

5 宣告的其它補述parameter 常數宣告real, parameter :: pi = 3.14159

常數必須給予初始值否則沒有功用

加入 parameter 表示 pi 是常數,不佔記憶體,因為不是變數,程式執行中不能改變其值。

Page 25: Chapter 3  輸出入及宣告

5 宣告的其它補述設定變數的初值

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. 完整程式 >

< Ex. 執行結果 > 1 1.00 1.00 1.00 A T

Page 26: Chapter 3  輸出入及宣告

5 宣告的其它補述宣告在程式中的結構

program ex0309

implicit none

integer :: A = 2, B = 1

real :: C

C = B / A

write(*,*) C

stop

end program ex0309

< Ex. 完整程式 >

0.0E+0

< Ex. 執行結果 >

程式開始→

主程式碼 

程式結束→

主程式碼結束→

←implicit 要放在宣告的最前端

開始變數宣告

 

主程式敘述區

必須先將整數型態轉換成浮點數型態,才能算出 0.5 的值

C=real(B)/real(A)

Page 27: Chapter 3  輸出入及宣告

5 宣告的其它補述自訂資料型態

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

< 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< 接右 >

Page 28: Chapter 3  輸出入及宣告

5 宣告的其它補述自訂資料型態type :: person

character(len=30) :: name

end type person

type(person) :: a

read(*,*) a% name

< 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

開始宣告一個叫做"person" 的資料型態

自訂資料型態結束

使用自訂資料型態宣告變數

使用自訂資料型態的變數

Page 29: Chapter 3  輸出入及宣告

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

type a%nam a%lenth a%address

a%age a%weight

Page 30: Chapter 3  輸出入及宣告

5 宣告的其它補述module 模組

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

< 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< 接右 >

產生constants.modvars.mod兩個模組檔

PI = 3.14159 G = 9.812 PI = 6.28318

< Ex. 執行結果 >

Page 31: Chapter 3  輸出入及宣告

5 宣告的其它補述module 模組module constants

implicit none

end module constants

program ex0311

use constants

自訂一個叫做“ constants” 的 module 區塊

在程式一開始宣告要使用那個模組,之後在程式中就可以使用模組的內容。

Page 32: Chapter 3  輸出入及宣告

5 宣告的其它補述module 模組

module typedef

implicit none

type :: person

character(len=30) :: name

integer :: age

real :: length

real :: weight

end type person

end module typedef

< Ex. 完整程式 >

< 接下頁 >

自訂一個 module 區塊

在 module 內宣告一個叫做“ person” 的自訂資料型態

Page 33: Chapter 3  輸出入及宣告

5 宣告的其它補述module 模組

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

< Ex. 完整程式 >< Ex. 執行結果 >產生 typedef.mod 的模組檔

Input his name:Tom < 輸入 T o m [ENTER] > Input hes age:15 < 輸入 1 5 [ENTER] > Name :Tom Age : 15

使用模組後就可以使用模組內的自訂資料型態來宣告

Page 34: Chapter 3  輸出入及宣告

5 宣告的其它補述kind 的使用在 PC 上資料型態宣告變數可儲存值的範圍

integer(kind=1) -127~127

integer(kind=2) -32767~32767

integer(kind=4) -2147483647~2147483647

real(kind=4) ~

real(kind=8) ~

381.18 10 383.40 103082.23 10 3081.79 10

Page 35: Chapter 3  輸出入及宣告

5 宣告的其它補述kind 的使用selected_int_kind(p)

傳回記錄 p 位整數時,所應宣告的 kind 值。

selected_real_kind(p,q)

傳回記錄 p 位有效位數,指數達到 q 的浮點數所需的 kind 值。

增加程式碼 " 跨平台 " 能力

Page 36: Chapter 3  輸出入及宣告

5 宣告的其它補述kind 的使用

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

< Ex. 完整程式 >

< 接下頁 >

Page 37: Chapter 3  輸出入及宣告

5 宣告的其它補述 kind 的使用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

< Ex. 完整程式 >

< Ex. 執行結果 >產生 kind_var.mod 的模組檔

12345678 12 0.12346E+46 0.12300E+04

kind=4

kind=2

kind=8

kind=4