Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Format statement in fortran
Format statement in fortran
Format statement in Fortran

Fortran is a very old programming language, but it has always played a very important role in scientific calculation and engineering calculation. Format statement is an important part of fortran, which allows programmers to control the output format and is very flexible and convenient. This paper will introduce the format statements in fortran and their usage and application.

Grammar of format statement

In fortran, the use of format statements needs to follow certain grammatical rules. The following is a common format:

Format ([format control statement] [,format control statement] ...)

Most extentsion syntax, such as using header field, is optional in format statement, but using guide field is mandatory. In the previous syntax, square brackets indicate options, and ellipsis indicates items that are allowed to be repeated many times.

Comment field of format statement

In fortranformat statements, the comment field is an option to record additional information. A comment field usually contains the following information:

Type of format item

Filling of spaces and zeros

Width and accuracy

The comment field can be used to process input and output, and can also be used for data formatting during file access. The following is an example of the print format of the comment field:

Sample _ Procedure.f90

Program Format _ Example

Implied nothing

integer,parameter::WP = selected _ real _ kind(8,99)

Real number (wp):: pi

Integer, parameter:: n=50

pi=4.0_wp*atan( 1.0_wp)

Write (*, "(a, i4, f10.8)") "The Valueofpiwith", n, "significantdigitsis differentiation:", pi.

endprogramformat_example

The output of the above program is as follows:

The value of 50 significant digits is: 3. 14 159265.

In the above code, the comment field "a" is followed by a string "Thevalueofpiwith", which we call a character conversion descriptor, which explains the type of fortran output format. Followed by "i4" indicates the width information. The remaining "f 10.8" represents the real output format of fortran.

Other fields of the format declaration

In fortran, there are many other format control fields that can be used, including exponential descriptors (e, es, d, etc.). ), hexadecimal format descriptors and other more specialized types.

The following are examples of using different descriptors:

Sample _ Procedure.f90

Program Format _ Example

Recessive ketone

integer,parameter::WP = selected _ real _ kind(8,99)

Real number (wp):: pi

Integer, parameter:: n=50

pi=4.0_wp*atan( 1.0_wp)

Write (*, "(a, i4, 2x, f 10.8, 2x, f 10.3, 2x, e 12.7)")"Piwith", n

, "significant figures:", pi, pi, pi.

endprogramformat_example

The output of the above program is as follows:

Piwith 50 Valid digits: 3.141592653.142 e+00.

In the above code, we used three different output format selectors: f 10.8, f 10.3 and e 12.7. Where f stands for real number type, 10.8 stands for output width and precision, and e stands for exponential type (scientific counting method), and the output width (12) and the number of digits reserved for precision are also specified.

Application of format statement

Format statement in Fortran can be used in various occasions, including printing screen output, writing text files, and even data formatting operations of binary files. Suppose we want to write the following program into a text file:

Sample _ Procedure.f90

Program Format _ Example 2

Recessive ketone

Integers:: I, J, K.

doi= 1, 10,2

doj= 1,5, 1

k=i+j

Write (*, "(3i5)")i, J, K.

Endor

Endor

endprogramformat_example2

The output of the above program is as follows:

% format example

1 12

123

134

145

156

3 14

325

336

347

358

5 16

527

538

549

55 10

In the above code, we used the 3i5 option, which means "3 integers with a bit width of 5", that is, for each cycle, we printed I, J and K as integers with a bit width of 5.

abstract

Format statement plays an important role in fortran, which can help us control the format of program output and make our output more reasonable and readable. In addition, the format statement can also be used for data access (input/output) and file formatting operations. I hope this article can help you better master the format statements in fortran.