Fortran 简明教程

Fortran - Basic Input Output

到目前为止,我们已经看到,我们可以分别使用 read * statement, and display output to the screen using the *print 语句从键盘读取数据。这种输入-输出形式是 free format I/O,称为 list-directed 输入-输出。

We have so far seen that we can read data from keyboard using the read * statement, and display output to the screen using the *print statement, respectively. This form of input-output is free format I/O, and it is called list-directed input-output.

自由格式简单 I/O 具有以下形式 −

The free format simple I/O has the form −

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

然而,格式化 I/O 能让你更灵活地控制数据传输。

However the formatted I/O gives you more flexibility over data transfer.

Formatted Input Output

格式化输入输出的语法如下 −

Formatted input output has the syntax as follows −

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

其中,

Where,

  1. fmt is the format specification

  2. variable-list is a list of the variables to be read from keyboard or written on screen

格式规范定义了格式化数据显示的方式。它包含一个字符串,其中包含括号中的一列 edit descriptors

Format specification defines the way in which formatted data is displayed. It consists of a string, containing a list of edit descriptors in parentheses.

edit descriptor 指示准确的格式,如字符和数字的显示宽度、小数点后的位数等。

An edit descriptor specifies the exact format, for example, width, digits after decimal point etc., in which characters and numbers are displayed.

For example

Print "(f6.3)", pi

下表描述了描述符:

The following table describes the descriptors −

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

以下符号与格式描述符一起使用:

Following symbols are used with the format descriptors −

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

Example 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

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

3.142
3.1415927
3.141592741012573
0.3142E-01

Example 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)

When the above code is compiled and executed, it produces the following result: (assume the user enters the name Zara)

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

Example 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

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

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

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

The Format Statement

格式化语句允许您在一个语句中混合和匹配字符、整数和实数输出。以下示例对此进行了演示:

The format statement allows you to mix and match character, integer and real output in one statement. The following example demonstrates this −

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

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

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