Fortran-basic-input-output

提供:Dev Guides
移動先:案内検索

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

次の表は、記述子を説明しています-

Descriptor Description Example
I This is used for integer output. This takes the form ‘rIw.m’ where the meanings of r, w and m are given in the table below. Integer values are right justified in their fields. If the field width is not large enough to accommodate an integer then the field is filled with asterisks. print "(3i5)", i, j, k
F This is used for real number output. This takes the form ‘rFw.d’ where the meanings of r, w and d are given in the table below. Real values are right justified in their fields. If the field width is not large enough to accommodate the real number then the field is filled with asterisks. print "(f12.3)",pi
E

This is used for real output in exponential notation. The ‘E’ descriptor statement takes the form ‘rEw.d’ where the meanings of r, w and d are given in the table below. Real values are right justified in their fields. If the field width is not large enough to accommodate the real number then the field is filled with asterisks.

実数を小数点以下3桁で出力するには、少なくとも10のフィールド幅が必要であることに注意してください。 1つは仮数の符号、2つはゼロ、4つは仮数、2つは指数自体です。 一般的に、w≥d + 7。

print "(e10.3)",123456.0 gives ‘0.123e+06’
ES This is used for real output (scientific notation). This takes the form ‘rESw.d’ where the meanings of r, w and d are given in the table below. The ‘E’ descriptor described above differs slightly from the traditional well known ‘scientific notation’. Scientific notation has the mantissa in the range 1.0 to 10.0 unlike the E descriptor which has the mantissa in the range 0.1 to 1.0. Real values are right justified in their fields. If the field width is not large enough to accommodate the real number then the field is filled with asterisks. Here also, the width field must satisfy the expressionw ≥ d + 7 print "(es10.3)",123456.0 gives ‘1.235e+05’
A This is used for character output. This takes the form ‘rAw’ where the meanings of r and w are given in the table below. Character types are right justified in their fields. If the field width is not large enough to accommodate the character string then the field is filled with the first ‘w’ characters of the string. print "(a10)", str
X This is used for space output. This takes the form ‘nX’ where ‘n’ is the number of desired spaces. print "(5x, a10)", str
/ Slash descriptor – used to insert blank lines. This takes the form ‘/’ and forces the next data output to be on a new line. print "(/,5x, a10)", str

次のシンボルは、フォーマット記述子で使用されます-

Sr.No Symbol & Description
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 = *******

フォーマットステートメント

formatステートメントを使用すると、1つのステートメントで文字、整数、および実際の出力を組み合わせて一致させることができます。 次の例はこれを示しています-

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