Fortran - 基本輸入輸出



到目前為止,我們已經看到可以使用read *語句從鍵盤讀取資料,並分別使用print*語句將輸出顯示到螢幕上。這種形式的輸入輸出是自由格式I/O,稱為列表定向輸入輸出。

自由格式簡單I/O的形式為:

read(*,*) item1, item2, item3...
print *, item1, item2, item3
write(*,*) item1, item2, item3...

但是,格式化I/O為您提供了更多關於資料傳輸的靈活性。

格式化輸入輸出

格式化輸入輸出的語法如下:

read fmt, variable_list 
print fmt, variable_list 
write fmt, variable_list 

其中,

  • fmt 是格式規範

  • variable-list 是要從鍵盤讀取或寫入螢幕的變數列表

格式規範定義了格式化資料顯示的方式。它由一個字串組成,該字串包含括號中編輯描述符的列表。

編輯描述符指定精確的格式,例如寬度、小數點後的位數等,字元和數字以該格式顯示。

例如

Print "(f6.3)", pi

下表描述了描述符:

描述符 描述 示例
I

用於整數輸出。它採用“rIw.m”的形式,其中r、w和m的含義在下表中給出。整數在其欄位中右對齊。如果欄位寬度不足以容納整數,則欄位將填充星號。

print "(3i5)", i, j, k
F

用於實數輸出。它採用“rFw.d”的形式,其中r、w和d的含義在下表中給出。實數在其欄位中右對齊。如果欄位寬度不足以容納實數,則欄位將填充星號。

print "(f12.3)",pi
E

用於以指數表示法輸出實數。“E”描述符語句採用“rEw.d”的形式,其中r、w和d的含義在下表中給出。實數在其欄位中右對齊。如果欄位寬度不足以容納實數,則欄位將填充星號。

請注意,要列印具有三位小數的實數,至少需要十位欄位寬度。一位用於尾數的符號,兩位用於零,四位用於尾數,兩位用於指數本身。一般來說,w ≥ d + 7。

print "(e10.3)",123456.0 給出“0.123e+06”
ES

用於實數輸出(科學計數法)。它採用“rESw.d”的形式,其中r、w和d的含義在下表中給出。“E”描述符與傳統的眾所周知的“科學計數法”略有不同。科學計數法的尾數範圍為1.0到10.0,而E描述符的尾數範圍為0.1到1.0。實數在其欄位中右對齊。如果欄位寬度不足以容納實數,則欄位將填充星號。這裡同樣,寬度欄位必須滿足表示式w ≥ d + 7

print "(es10.3)",123456.0 給出“1.235e+05”
A

用於字元輸出。它採用“rAw”的形式,其中r和w的含義在下表中給出。字元型別在其欄位中右對齊。如果欄位寬度不足以容納字元字串,則欄位將填充字串的前“w”個字元。

print "(a10)", str
X

用於空格輸出。它採用“nX”的形式,其中“n”是所需空格的數量。

print "(5x, a10)", str
/

斜槓描述符 – 用於插入空行。它採用“/”的形式,並強制將下一個資料輸出到新行。

print "(/,5x, a10)", str

以下符號與格式描述符一起使用:

序號 符號及描述
1

c

列號

2

d

實數輸入或輸出小數點右邊的位數

3

m

要顯示的最小位數

4

n

要跳過的空格數

5

r

重複計數 – 使用描述符或描述符組的次數

6

w

欄位寬度 – 用於輸入或輸出的字元數

示例 1

program printPi

   pi = 3.141592653589793238 
   
   Print "(f6.3)", pi 
   Print "(f10.7)", pi
   Print "(f20.15)", pi 
   Print "(e16.4)", pi/100 
   
end program printPi

編譯並執行上述程式碼後,將產生以下結果:

3.142
3.1415927
3.141592741012573
0.3142E-01

示例 2

program printName
implicit none

   character (len = 15) :: first_name
   print *,' Enter your first name.' 
   print *,' Up to 20 characters, please'
   
   read *,first_name 
   print "(1x,a)",first_name
   
end program printName

編譯並執行上述程式碼後,將產生以下結果:(假設使用者輸入名稱Zara)

Enter your first name.
Up to 20 characters, please
Zara 

示例 3

program formattedPrint
implicit none

   real :: c = 1.2786456e-9, d = 0.1234567e3 
   integer :: n = 300789, k = 45, i = 2
   character (len=15) :: str="Tutorials Point"
   
   print "(i6)", k 
   print "(i6.3)", k 
   print "(3i10)", n, k, i 
   print "(i10,i3,i5)", n, k, i 
   print "(a15)",str 
   print "(f12.3)", d
   print "(e12.4)", c 
   print '(/,3x,"n = ",i6, 3x, "d = ",f7.4)', n, d
   
end program formattedPrint

編譯並執行上述程式碼後,將產生以下結果:

45
045
300789 45  2
300789 45  2
Tutorials Point
123.457
0.1279E-08

n = 300789 d = *******

格式語句

格式語句允許您在一個語句中混合和匹配字元、整數和實數輸出。以下示例演示了這一點:

program productDetails 
implicit none 

   character (len = 15) :: name
   integer :: id 
   real :: weight
   name = 'Ardupilot'
   id = 1
   weight = 0.08
   
   print *,' The product details are' 
   
   print 100
   100 format (7x,'Name:', 7x, 'Id:', 1x, 'Weight:')
   
   print 200, name, id, weight 
   200 format(1x, a, 2x, i3, 2x, f5.2) 
   
end program productDetails

編譯並執行上述程式碼後,將產生以下結果:

The product details are
Name:       Id:    Weight:
Ardupilot   1       0.08
廣告

© . All rights reserved.