2

내가 작업하고있는 프로젝트에서는 상당량의 배열을 일련의 서브 루틴과 함수를 통해 전달해야하므로 모듈을 선택해야합니다.할당 할 수있는 배열 중 모듈을 사용하는 방법은 무엇입니까?

이러한 배열은 모두 할당 가능하며, 서브 루틴에서 사용해야하는 배열을 명시해야하는 경우를 제외하고는 별다른 문제가 발생하지 않습니다. 코드를 실행하는 방식은 완전히 과잉입니다.

방법이 일하고 :

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 - allocatable array in subroutineHow to pass allocatable arrays to subroutines in 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 
+0

더 많은주의를 기울일 수 있도록 모든 Fortran 질문에 tag fortran을 사용하십시오. Fortran 90은 이전 버전에 불과합니다. 질문이 이전 버전에만 적용되는 경우 다른 태그 대신 fortran90 태그를 추가 할 수 있습니다 .. –

+0

ise capital I 질문을 작성할 때는주의하고 쉼표 및 전체 정지 다음에 공백을 넣으십시오. 편집에 많은 시간이 걸립니다. –

+0

나는 선생님들이 옳을지도 모르고 코드의 잘못된 디자인을 주장하고 있습니다. –

답변

1

귀하의 디자인은 여전히 ​​나에게 매우 혼란 스럽다. 나는 단지 이것을 할 것이다 :

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 

그것은 매우 작은 변화이므로, 나는 당신의 설명을 이해하지 못할 수도있다. 정렬 프로 시저에서 모듈에 데이터가 필요한 이유가 없습니다.

+0

예제에는 모듈 데이터를 사용할 이유가 없지만 원래 문제는있었습니다. 정렬 루틴 예제에 그 이유가 있습니다 (print *, i). 대답 주셔서 감사합니다, 그것은 많이 도움이되었습니다. –