Fortran 2003
外观
一位维基教科书用户建议将本书或章节合并到Fortran中。 请在讨论页面上讨论是否应该进行此合并。 |
Fortran 2003是Fortran的一个标准。本书将重点介绍现代Fortran,不会涵盖Fortran 77和更早标准中过时的功能。
以下是用Fortran编写的Hello World程序。
program hello
implicit none
write (*,*) "Hello, world."
end program hello
"implicit none"语句强制程序员声明所有变量,这被认为是良好的编程风格。Fortran具有整数、字符、实数、复数和逻辑数据类型。以下程序演示了它们的使用。
program data_types
implicit none
integer :: i
real :: x
logical :: tf
complex :: z
i = 3
x = 3.0
z = (3.0,3.0)
tf = .true.
write (*,*) "i =",i," x =",x," z =",z," tf = ",tf
end program data_types
输出:i = 3 x = 3. z = (3.,3.) tf = T
Fortran具有算术运算符+、-、/、*和**(用于求幂)。以下程序的输出为
program xx
implicit none
write (*,*) 2+3,2-3,2/3,4*3,2**3
end program xx
是
5 -1 0 12 8
Fortran 90及更高版本在数组方面具有强大的功能。以下程序演示了数组的一些功能。默认情况下,数组元素从1开始编号,而不是像C或C++那样从0开始编号。
program xarray
! demonstrate array constructor and intrinsic functions
implicit none
integer, parameter :: n = 3
integer :: vec(n)
vec = (/9,4,1/) ! set vec(1) to 9, vec(2) to 4, vec(3) to 1
write (*,*) "vec = ",vec ! print each element of vec
write (*,*) "vec(1) = ",vec(1),", vec(3) =",vec(3) ! print the 1st and 3rd elements
write (*,*) "size(vec), sum(vec), product(vec) = ", &
size(vec), sum(vec), product(vec)
write (*,*) "minval(vec), maxval(vec) = ",minval(vec),maxval(vec)
vec = vec + 2 ! add 2 to each element of vec
write (*,*) "vec = ",vec ! print each element of vec
vec = vec**2 ! square each element of vec
write (*,*) "vec = ",vec ! print each element of vec
end program xarray
输出
vec = 9 4 1 vec(1) = 9 , vec(3) = 1 size(vec), sum(vec), product(vec) = 3 14 36 minval(vec), maxval(vec) = 1 9 vec = 11 6 3 vec = 121 36 9
Fortran使用do循环进行迭代。例如,程序
program xloop
implicit none
integer :: i
do i=1,3
write (*,*) i,i**2
end do
write (*,*) "i=",i
do i=1,4,2
write (*,*) i
end do
write (*,*) "i=",i
end program xloop
输出
1 1 2 4 3 9 i= 4 1 3 i= 5
因为在第一个循环中,变量i以1为步长在1到3之间取值,在第二个循环中,步长为2。循环结束后,i的值为离开循环之前i的最后一个值加上步长。
Fortran具有比较运算符< <= /= == >= >,其中/=表示“不等于”,其他运算符具有通常的含义。程序
program xcompare
implicit none
write (*,*) 1<0,1<=0,1==0,1/=0,1>=0,1>0
end program xcompare
输出
F F F T T T
可以有一个没有计数变量的DO循环,在这种情况下,将需要一个EXIT语句来退出循环,如以下程序所示
program xfibonacci
! print Fibonacci numbers up to max_fib
implicit none
integer, parameter :: max_fib = 10
integer :: i,fib,fib1,fib2
i = 0
fib = 0
fib1 = 0
fib2 = 0
write (*,*) "Fibonacci numbers <= ",max_fib
do
if (fib > max_fib) exit
write (*,*) fib
i = i + 1
if (i > 1) then
fib = fib1 + fib2
else
fib = 1
end if
fib2 = fib1
fib1 = fib
end do
end program xfibonacci
声明max_fib为参数意味着它的值在程序的其余部分中不能更改。
循环可以嵌套,如以下程序所示
program xnest
implicit none
integer :: i,j
do i=1,3
do j=1,2
write (*,*) "i,j=",i,j
end do
end do
end program xnest
输出
i,j= 1 1 i,j= 1 2 i,j= 2 1 i,j= 2 2 i,j= 3 1 i,j= 3 2
函数可以用来根据零个或多个参数返回一个值。下面的代码显示了一个将华氏度转换为摄氏度的函数。
module convert_mod
implicit none
contains
function cels_from_fahr(degrees_fahr) result(degrees_cels)
real, intent(in) :: degrees_fahr
real :: degrees_cels
degrees_cels = (degrees_fahr-32)/1.8
end function cels_from_fahr
end module convert_mod
program xtemperature
use convert_mod, only: cels_from_fahr
real :: deg
integer :: i
write (*,"(2a10)") "degrees_F","degrees_C"
do i=12,100,20
deg = real(i)
write (*,"(2f10.1)") deg,cels_from_fahr(deg)
end do
end program xtemperature
输出
degrees_F degrees_C 12.0 -11.1 32.0 0.0 52.0 11.1 72.0 22.2 92.0 33.3
子例程不能在表达式中使用,并且通过调用语句调用,如以下程序所示,该程序与上面的程序产生相同的输出。
module convert_mod
implicit none
contains
subroutine cels_from_fahr(degrees_fahr,degrees_cels)
real, intent(in) :: degrees_fahr
real, intent(out) :: degrees_cels
degrees_cels = (degrees_fahr-32)/1.8
end subroutine cels_from_fahr
end module convert_mod
program xtemperature
use convert_mod, only: cels_from_fahr
real :: deg_f,deg_c
integer :: i
write (*,"(2a10)") "degrees_F","degrees_C"
do i=12,100,20
deg_f = real(i)
call cels_from_fahr(deg_f,deg_c)
write (*,"(2f10.1)") deg_f,deg_c
end do
end program xtemperature