我正在从事的项目要求大量的数组应该通过一堆子例程和函数传递,因此我选择了一个模块。
这些数组都是可分配的,它们不会造成太大的麻烦,除非我必须明确说明子例程应该使用这些数组中的哪个。我管理代码运行的方式实在是太过分了。
工作方式:
Program Principal
Use Info
open(unit=1,file="dadose6h.dat")
call get_data
call sortsupply
call sortcost
call ratecapacity
end Program
Module Info
integer i,j
integer, dimension (:), allocatable :: supply
integer, dimension (:), allocatable :: cost
integer, dimension (:,:), allocatable :: capacity
integer, dimension (:,:), allocatable :: demand
End module info
subroutine get_data
use info
read(1,*) j,i
allocate (supply (j))
allocate (cost (j))
allocate (capacity (j,i))
allocate (demand (j,i))
read(1,*) supply
read(1,*) cost
read(1,*) capacity
read(1,*) demand
end subroutine
Subroutine SortCost
use info
integer u
!u=cost(1)
!...
print*, cost
End subroutine
Subroutine Sortsupply
use info
integer u
!u=supply(1)
!...
print*, supply
End subroutine
Subroutine ratecapacity
use info
integer u
!u=capacity(1,1)
!...
print *, j,i
print*, capacity
End subroutine
上面的示例除了要排序的数组之外,还有两个子例程sortcost和sortsupply相等。我确实想要一个独特的子程序来完成这两项任务。我只是不能声明正确的方法。
方式应该是:
Program Principal
Use Info
open(unit=1,file="dadose6h.dat")
call get_data
call sort(supply)
call sort(cost)
call rate(capacity)
end Program
Module Info
integer i,j
integer, dimension (:), allocatable :: supply
integer, dimension (:), allocatable :: cost
integer, dimension (:,:), allocatable :: capacity
integer, dimension (:,:), allocatable :: demand
End module info
subroutine get_data
use info
read(1,*) j,i
allocate (supply (j))
allocate (cost (j))
allocate (capacity (j,i))
allocate (demand (j,i))
read(1,*) supply
read(1,*) cost
read(1,*) capacity
read(1,*) demand
end subroutine
Subroutine Sort(X)
!use info
!i dunno how the declaration should be
integer u
!u=X(1)
!...
print*, "Sort:",X
End subroutine
Subroutine rate(X)
!use info
!i dunno how the declaration should be neither
integer u
!u=X(1,1)
!...
print*, "rate:", X
End subroutine
我确实尝试了一些声明但是没有用,这些家伙有一个类似的问题FORTRAN-子例程中的可分配数组,如何将可分配数组传递给Fortran中的子例程,首先答案是故意的,但我已经尝试了(inout ,out,in)仍然不起作用。第二个有一个关于显式接口的答案,但是我不会对分配状态做任何事情。
注意:我问这个问题的老师经常会漏掉这一点,问题不是关于将可分配数组传递给例程,我已经可以使用Info模块来做到这一点,但是我需要显示子例程。
我真的很感激谁能写这两个子程序声明。排序(一维数组)和Rate(二维)。或者至少教我。
dadose.dat文件的内容:
4
7
1000 2000 2000 2000
100 100 100 100
10 10 74 19
60 1 25 20
90 50 7 2
11 31 51 96
15 10 94 36
52 89 47 13
30 35 4 12
100 150 50 200
100 100 200 75
100 100 200 250
100 100 150 250
150 100 200 250
200 100 200 250
200 150 200 250
您的设计对我还是很困惑。我会这样做:
Module Info
integer i,j
integer, dimension (:), allocatable :: supply
integer, dimension (:), allocatable :: cost
integer, dimension (:,:), allocatable :: capacity
integer, dimension (:,:), allocatable :: demand
End module info
Module Procedures
contains
Subroutine Sort(X)
integer :: X(:)
integer u
!u=X(1)
!...
print*, "Sort:",X
End subroutine
End module Procedures
Program Principal
Use Info
Use Procedures
open(unit=1,file="dadose6h.dat")
call get_data
call sort(supply)
call sort(cost)
call rate(capacity)
end Program
这是一个很小的变化,所以我可能不理解您的描述。我看不出为什么排序过程需要包含数据的模块的任何原因。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句