如何向子例程指示可分配模块的数组使用哪个



我正在工作的项目要求通过一堆子例程和函数传递相当多的数组,所以我选择了一个模块。

这些数组都是可分配的,它们不会引起太多麻烦,除非我必须明确子程序应该使用这些数组中的哪一个。我管理代码运行的方式,完全是小题大做。

它的工作方式:

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

除了正在排序的数组之外,上面的例子还有两个相等的子例程sortcast和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来做这件事了,但我需要显示哪个子程序。

我真的很想知道谁能写出子程序的这两个声明。排序(一维数组)和速率(二维)。或者至少教我。

dadose6h.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

这是一个很小的变化,所以我可能不理解你的描述。我看不出为什么排序过程需要带有数据的模块。

最新更新