我正在为我的第一个Fortran项目制作一个物理计算器。我已经有了速度/距离/时间部分,它实际上运行得很好。然而,当我试图添加电流,充电和时间模块时,我遇到了一个问题-我在哪里为我的模块放置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语句开始。