M1 Public and Private module test implicit none
M-1 Public and Private 程式說明 module test 將模組內的變數或函式 定義成只能在此模組內 使用的變數或函式 implicit none private var_1 public var_2, sub_1 內定的狀態為 public 將模組內的變數或函式 定義成可以對外界公開 使用的變數或函式 integer : : var_1, var_2 . . . contains subroutine sub_1 … end module test 函式要放在contains中
M-1 Public and Private < Ex 0 M 01. 完整程式> module bank 變數 money 只能在 module bank 中使用,主程式中不能 使用 money 變數 implicit none private money public Load. Money, Save. Money, Report integer : : money = 1000000 contains subroutine Load. Money(num) 將 Load. Money, Save. Money, Report 定義成可以對外界公開 使用的函式 implicit none integer : : num money=money-num return end subroutine <接下頁>
M-1 Public and Private < Ex 0 M 01. 完整程式> subroutine Save. Money(num) implicit none integer : : num money=money+num return end subroutine Report() implicit none write(*, "('銀行目前庫存', I, '元')") money return end subroutine end module bank <接下頁>
M-1 Public and Private < Ex 0 M 01. 完整程式> program ex 0 M 01 use bank implicit none call Load. Money(100) call Save. Money(1000) call Report() < Ex. 執行結果> stop TEST end program ex 0 M 01
M-2 USE 更改模組中變數名稱 module A implicit none integer : : va end module A module B implicit none integer : : va, vb end module program main use A, aa=>va use B implicit none . . . use指令後面,可以臨時 把 module 裡面的變數或 函式名稱改名 把 module A 中的變數 va改名為 aa 來使用,若不使用 “use A, aa=> va”, module A 和 module B 都同時擁有 名稱為 va 的變數,在主程式中同時 使用這兩個 module 時,會出現變數 名稱重複的問題
M-2 USE 只使用模組中的某些變數 module A implicit none integer : : va, vb, vc end module B implicit none integer : : va, vb end module program main use A, only : vc use B implicit none . . . use指令後面,可以臨時 只選擇 module 裏面所需 之變數或函式 只用 module A 中的變數 vc,其他 va 和vb 變數都不使用,因此不會與 module B 中的va 和vb 變數衝突
M-2 USE 合併使用 module A implicit none integer : : va, vb, vc end module B implicit none integer : : va, vb end module program main use A, only : c => vc use B implicit none . . . use指令後面,可以臨時 只選擇 module 裏面所需 之變數或函式並名稱改名 只用module A 中的變數 vc, 不過把 vc 改名成 c 來使用
M-2 USE < Ex 0 M 02. 完整程式> module MA implicit none real : : a, b contains subroutine getx() write(*, "('x=', F 5. 2)") -b/a return end subroutine end module <接下頁>
M-2 USE < Ex 0 M 02. 完整程式> module MB use MA implicit none real : : c contains subroutine getx 2() real : : a 2, d, sqrt_d a 2=2*a d=b*b-4*a*c if ( d>=0 ) then sqrt_d = sqrt(d) <接下頁> write(*, "('x=', F 5. 2, ', ', F 5. 2)")(-b+sqrt_d)/a 2, (-b-sqrt_d)/a 2
M-2 USE < Ex 0 M 02. 完整程式> else write(*, *) "無實數解" end if return end subroutine end module < Ex 0 M 02. 完整程式> subroutine sub 1() use MA <接下頁> implicit none a=2. 0 b=3. 0 call getx() return end subroutine <接下頁>
M-2 USE < Ex. 執行結果> < Ex 0 M 02. 完整程式> TEST subroutine sub 2() use MB implicit none a=1. 0 b=4. 0 c=4. 0 < Ex 0 M 02. 完整程式> call getx 2() program main return implicit none end subroutine call sub 1() call sub 2() end program
M-3 Interface < Ex 0 M 03. 完整程式> module MA implicit none 虛擬的函式名稱show interface show module procedure show_int module procedure show_character end interface contains subroutine show_int( n ) implicit none integer, intent(in) : : n write(*, "('n=', I 3)") n return end subroutine show_int <接下頁>
M-3 Interface < Ex 0 M 03. 完整程式> subroutine show_character( str ) implicit none character(len=*), intent(in) : : str write(*, "('str=', A)") str return end subroutine show_character end module <接下頁>
M-3 Interface < Ex. 執行結果> TEST < Ex 0 M 03. 完整程式> program main use MA implicit none call show_int(1) 參數是整數, 會自動選 擇呼叫show_int call show(1) call show_character("FORTRAN 95") call show("FORTRAN 95") stop end program 參數是字串, 會自動選 擇呼show_character
- Slides: 16