主题:第一次发帖,希望大家不吝赐教 O(∩_∩)O
大家好,我是最近刚刚接触Fortran不久 ,最近在看彭国伦的一本书中有一些问题。
书的p209 在讲module中的函数时的一个程序:
==================================================================================
module constant
implicit none
real, parameter :: PI = 3.14159
real, parameter :: G = 9.81
end module
module typedef
implicit none
type player
real :: angle
real :: speed
real :: distance
end type
end module
module shoot
use constant
use typedef
implicit none
contains
! 由角度、切线速度来计算投射距离
subroutine Get_Distance( person )
implicit none
type(player) :: person
real rad, Vx, time
rad = Angle_TO_Rad( person%angle ) ! 单位转换
Vx = person%speed * cos(rad) ! 水平方向速度
time = 2.0 * person%speed * sin(rad) / G ! 在空中飞行时间
person%distance = Vx * time ! 距离 = 水平方向速度 * 飞行时间
return
end subroutine
! 把0~360的角度转换成0~2PI的弧度
real function Angle_TO_Rad( angle )
implicit none
real angle
Angle_TO_Rad = angle*pi/180.0
return
end function
end module
program ex0837
use shoot
implicit none
integer, parameter :: players = 5
type(player) :: people(players) = (/ player(30.0, 25.0, 0.0),&
player(45.0, 20.0, 0.0),&
player(35.0, 21.0, 0.0),&
player(50.0, 27.0, 0.0),&
player(40.0, 22.0, 0.0) &
/)
integer :: I
do I=1, players
call Get_Distance( people(I) )
write(*,"('Player ',I1,' =',F8.2)") I, people(I)%distance
end do
stop
end
==============================================================================
在这个程序的module typedef中,创建了type(player),这样在主程序和子程序中就无需在创建,而只需声明了,
在主程序和子程序中分别声明了 people和person这两个player类型的数据。我的想法是能不能把
type(player)::people和type(player)::person声明部分也放到module中,并设置为全局变量
这样在程序中仅仅是用就可以了。
我大概的想法是:
==============================================================================
module constant
implicit none
real,parameter::pi=3.14159
real,parameter::g=9.81
end module
module typedef
implicit none
type player
real angle
real speed
real distance
end type
end module
module shoot
use constant
use typedef
type(player)::person
type(player)::people
common person,people
implicit none
end module shoot
========================================
real function angle_to_rad(angle)
use shoot
implicit none
real angle
angle_to_rad=angle*pi/180.0
return
end function angle_to_rad
========================================
subroutine get_distance(person)
use shoot
implicit none
!type(player)::person
real rad,vx,time
!封装在一个module中的子程序要调用函数时无需声明
rad=angle_to_rad(person%angle)
vx=person%speed*cos(rad)
time=2.0*person%speed*sin(rad)/g
person%distance=vx*time
return
end subroutine get_distance
end module shoot
==========================================
program main
use shoot
implicit none
integer,parameter::players=5
!type(player)::
people(players)=(/ player(30.0,25.0,0.0),player(45.0,20.0,0.0),player(35.0,21.0,0.0),player(50.0,27.0,0.0),player(40.0,22.0&
,0.0)/)
integer i
do i=1,players
call get_distance(people(i))
write(*,"('player',i1,'=',f8.2)")i,people(i)%distance
end do
stop
end program main
===========================================================================
在module shoot中直接把type(player)::people和type(player)::person直接申明成全局变量。
并且没有用contains,而是直接把每个函数都封装好。
但是这样编译时总是有问题,我用的是cvf6.6
我觉得这条错误信息很关键:If a common-block-object is of a derived type, it must be of a sequence type
但不知道是什么意思,另外还有一些别的错误,比如,会说函数的参数没有声明。
希望大家能帮我看看,为什么我这样做不行,应该如何解决。自定义类型的数据能否设置为全局变量,应该如何设置。
谢谢大家了!
书的p209 在讲module中的函数时的一个程序:
==================================================================================
module constant
implicit none
real, parameter :: PI = 3.14159
real, parameter :: G = 9.81
end module
module typedef
implicit none
type player
real :: angle
real :: speed
real :: distance
end type
end module
module shoot
use constant
use typedef
implicit none
contains
! 由角度、切线速度来计算投射距离
subroutine Get_Distance( person )
implicit none
type(player) :: person
real rad, Vx, time
rad = Angle_TO_Rad( person%angle ) ! 单位转换
Vx = person%speed * cos(rad) ! 水平方向速度
time = 2.0 * person%speed * sin(rad) / G ! 在空中飞行时间
person%distance = Vx * time ! 距离 = 水平方向速度 * 飞行时间
return
end subroutine
! 把0~360的角度转换成0~2PI的弧度
real function Angle_TO_Rad( angle )
implicit none
real angle
Angle_TO_Rad = angle*pi/180.0
return
end function
end module
program ex0837
use shoot
implicit none
integer, parameter :: players = 5
type(player) :: people(players) = (/ player(30.0, 25.0, 0.0),&
player(45.0, 20.0, 0.0),&
player(35.0, 21.0, 0.0),&
player(50.0, 27.0, 0.0),&
player(40.0, 22.0, 0.0) &
/)
integer :: I
do I=1, players
call Get_Distance( people(I) )
write(*,"('Player ',I1,' =',F8.2)") I, people(I)%distance
end do
stop
end
==============================================================================
在这个程序的module typedef中,创建了type(player),这样在主程序和子程序中就无需在创建,而只需声明了,
在主程序和子程序中分别声明了 people和person这两个player类型的数据。我的想法是能不能把
type(player)::people和type(player)::person声明部分也放到module中,并设置为全局变量
这样在程序中仅仅是用就可以了。
我大概的想法是:
==============================================================================
module constant
implicit none
real,parameter::pi=3.14159
real,parameter::g=9.81
end module
module typedef
implicit none
type player
real angle
real speed
real distance
end type
end module
module shoot
use constant
use typedef
type(player)::person
type(player)::people
common person,people
implicit none
end module shoot
========================================
real function angle_to_rad(angle)
use shoot
implicit none
real angle
angle_to_rad=angle*pi/180.0
return
end function angle_to_rad
========================================
subroutine get_distance(person)
use shoot
implicit none
!type(player)::person
real rad,vx,time
!封装在一个module中的子程序要调用函数时无需声明
rad=angle_to_rad(person%angle)
vx=person%speed*cos(rad)
time=2.0*person%speed*sin(rad)/g
person%distance=vx*time
return
end subroutine get_distance
end module shoot
==========================================
program main
use shoot
implicit none
integer,parameter::players=5
!type(player)::
people(players)=(/ player(30.0,25.0,0.0),player(45.0,20.0,0.0),player(35.0,21.0,0.0),player(50.0,27.0,0.0),player(40.0,22.0&
,0.0)/)
integer i
do i=1,players
call get_distance(people(i))
write(*,"('player',i1,'=',f8.2)")i,people(i)%distance
end do
stop
end program main
===========================================================================
在module shoot中直接把type(player)::people和type(player)::person直接申明成全局变量。
并且没有用contains,而是直接把每个函数都封装好。
但是这样编译时总是有问题,我用的是cvf6.6
我觉得这条错误信息很关键:If a common-block-object is of a derived type, it must be of a sequence type
但不知道是什么意思,另外还有一些别的错误,比如,会说函数的参数没有声明。
希望大家能帮我看看,为什么我这样做不行,应该如何解决。自定义类型的数据能否设置为全局变量,应该如何设置。
谢谢大家了!