我试图阅读一些Fortran代码,但是无法确定百分号符号
它在类似于以下行中出现:
%
的作用。它在类似于以下行中出现:
x = a%rho * g * (-g*a%sigma + m%gb * m%ca * (1.6 * a%rho+g))
它是做什么用的?
module pipedef
type pipe ! Define new type 'pipe', which
real diameter ! is made up of two reals, an
real flowrate ! integer, and a character.
integer length
character(len=10) :: flowtype
end type pipe
end module pipedef
program main
use pipedef ! Associate module pipedef with main.
type(pipe) water1, gas1 ! Declare two variables of type 'pipe'.
water1 = pipe(4.5,44.8,1200,"turbulent") ! Assign value to water1.
gas1%diameter = 14.9 ! Assign value to parts
gas1%flowrate = 91.284 ! of gas1.
gas1%length = 2550
gas1%flowtype = 'laminar'
.
.
.
end program
%
有许多密切相关的用途。随着Fortran的发展,这些用途的数量也不断增加。%
用于访问派生类型的组件。考虑具有该类型对象a
的派生类型a_t
:type a_t
real rho, sigma
end type
type(a_t) a
rho
和sigma
可以通过a%rho
和a%sigma
访问。如问题所示,这些组件可以用于表达式(例如a%rho * g
)或者作为赋值的左侧(a%rho=1.
)。type b_t
type(a_t) a
end type
type(b_t) b
因此,在单个引用中可能会出现多个%
:
b%a%rho = ...
type a_t(n)
integer, len :: n=1
real x(n)
contains
procedure f
end type
type(a_t(2)) a
a
有一个长度类型参数和一个类型限定过程。在像以下这样的表达式中,该类型限定过程将应用于对象a:
a%foo(x)
x = a%f()
引用派生类型对象的绑定 f
。
参数 a
的参数 n
可以被引用为
print *, a%n, SIZE(a%x)
组件x
可能被引用。
最后,从Fortran 2008开始,%
可以用于访问复杂对象的实部和虚部:
complex x, y(3)
x%im = 1.
x%re = 0.
y = (2., 1.)
print *, y(2)%im+y(3)%re
gas1
中的一个变量,而无需使用整个构造函数,即不使用所有所需的变量(water1 = pipe(4.5,44.8,1200,"turbulent")
)。 - Garini