在 Fortran 中 USE 语句应该放在哪里?
Where should USE statements be placed in Fortran?
我正在为我的第一个 Fort运行 项目制作一个物理计算器。我已经有了 speed/distance/time 部分,实际上效果很好。然而,当我尝试为电流、电荷和时间添加模块时,我 运行 遇到了一个问题——我应该在哪里放置我的模块的 USE 语句?代码如下:
module kinematics
implicit none
real :: t, d, s
contains
subroutine time_from_distance_and_speed()
print *, 'Input distance in metres'
read *, d
print *, 'Input speed in metres per second'
read *, s
t = d / s
print*, 'Time is ', s
end subroutine
subroutine distance_from_speed_and_time()
print *, 'Input speed in metres per second'
read *, s
print *, 'Input time in seconds'
read *, t
d = s * t
print*, 'Distance is ', d
end subroutine
subroutine speed_from_time_and_distance()
print *, 'Input distance in metres'
read *, d
print *, 'Input time in seconds'
read *, t
s = d / t
print *, 'Speed is ', s
end subroutine
end module
module electronics
implicit none
real :: Q, I, T
contains
subroutine charge_from_current_and_time()
print *, 'Input current in amps'
read *, I
print *, 'Input time in seconds'
read *, T
Q = I * T
print*, 'Charge is ', Q
end subroutine
subroutine current_from_charge_and_time()
print *, 'Input charge in coulombs'
read *, Q
print *, 'Input time in seconds'
read *, T
C = Q/T
print*, 'Distance is ', d
end subroutine
subroutine time_from_current_and_charge()
print *, 'Input current in coulombs'
read *, Q
print *, 'Input charge in amps'
read *, I
T = Q/I
print *, 'Speed is ', s
end subroutine
end module
program bike
integer :: gg
integer :: pp
print *, 'Press 0 for speed, distance, and time. Press 2 for current, charge and time.'
read *, pp
if ( pp == 0 ) then
do while(.true.)
print *, 'Press 1 for speed, 2 for distance, and 3 for time'
read *, gg
if(gg == 1) then
call speed_from_time_and_distance
else if(gg == 2) then
call distance_from_speed_and_time
else if(gg == 3) then
call time_from_distance_and_speed
end if
print *, 'Press 5 to exit the console, or press 4 to do another calculation'
read *, gg
if(gg== 5) then
exit
end if
end do
end program
use
语句放在每个编译单元(模块、程序或过程)的开头,紧跟在 program
、module
、function
或subroutine
行和任何 implicit
语句或声明之前。 (你应该在每个模块和每个程序中有 implicit none
。)
对于非常短的程序,如果您省略 program bike
,您可以直接从 use 语句开始。
我正在为我的第一个 Fort运行 项目制作一个物理计算器。我已经有了 speed/distance/time 部分,实际上效果很好。然而,当我尝试为电流、电荷和时间添加模块时,我 运行 遇到了一个问题——我应该在哪里放置我的模块的 USE 语句?代码如下:
module kinematics
implicit none
real :: t, d, s
contains
subroutine time_from_distance_and_speed()
print *, 'Input distance in metres'
read *, d
print *, 'Input speed in metres per second'
read *, s
t = d / s
print*, 'Time is ', s
end subroutine
subroutine distance_from_speed_and_time()
print *, 'Input speed in metres per second'
read *, s
print *, 'Input time in seconds'
read *, t
d = s * t
print*, 'Distance is ', d
end subroutine
subroutine speed_from_time_and_distance()
print *, 'Input distance in metres'
read *, d
print *, 'Input time in seconds'
read *, t
s = d / t
print *, 'Speed is ', s
end subroutine
end module
module electronics
implicit none
real :: Q, I, T
contains
subroutine charge_from_current_and_time()
print *, 'Input current in amps'
read *, I
print *, 'Input time in seconds'
read *, T
Q = I * T
print*, 'Charge is ', Q
end subroutine
subroutine current_from_charge_and_time()
print *, 'Input charge in coulombs'
read *, Q
print *, 'Input time in seconds'
read *, T
C = Q/T
print*, 'Distance is ', d
end subroutine
subroutine time_from_current_and_charge()
print *, 'Input current in coulombs'
read *, Q
print *, 'Input charge in amps'
read *, I
T = Q/I
print *, 'Speed is ', s
end subroutine
end module
program bike
integer :: gg
integer :: pp
print *, 'Press 0 for speed, distance, and time. Press 2 for current, charge and time.'
read *, pp
if ( pp == 0 ) then
do while(.true.)
print *, 'Press 1 for speed, 2 for distance, and 3 for time'
read *, gg
if(gg == 1) then
call speed_from_time_and_distance
else if(gg == 2) then
call distance_from_speed_and_time
else if(gg == 3) then
call time_from_distance_and_speed
end if
print *, 'Press 5 to exit the console, or press 4 to do another calculation'
read *, gg
if(gg== 5) then
exit
end if
end do
end program
use
语句放在每个编译单元(模块、程序或过程)的开头,紧跟在 program
、module
、function
或subroutine
行和任何 implicit
语句或声明之前。 (你应该在每个模块和每个程序中有 implicit none
。)
对于非常短的程序,如果您省略 program bike
,您可以直接从 use 语句开始。