📜  Fortran-基本输入输出

📅  最后修改于: 2020-11-04 06:19:01             🧑  作者: Mango


到目前为止,我们已经看到我们可以使用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.

Please note that, to print out a real number with three decimal places a field width of at least ten is needed. One for the sign of the mantissa, two for the zero, four for the mantissa and two for the exponent itself. In general, 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

Column number

2

d

Number of digits to right of the decimal place for real input or output

3

m

Minimum number of digits to be displayed

4

n

Number of spaces to skip

5

r

Repeat count – the number of times to use a descriptor or group of descriptors

6

w

Field width – the number of characters to use for the input or output

例子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语句允许您在一个语句中混合和匹配字符,integer和real输出。以下示例演示了这一点-

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