帮助需要FORTRAN中的Allocatable Array

时间:2009-05-03 16:08:34

标签: arrays types fortran multidimensional-array

我在使用Allocatable数组时遇到了麻烦。

我必须将文件中的所有信息复制到可分配的数组中。 该文件是这样的:

3 
3 5 
2 1 
4 0

3是分数 其他六个数字以(x,y)形式显示图表上的点。 所以(3,5),(2,1),(4,0)是要点。 但我有问题将这些数字作为一对。

我尝试编码,这是我的编码:

PROGRAM practice

IMPLICIT NONE

INTEGER :: astat, ioStatus
INTEGER :: x, y
INTEGER :: num, code1, code2, code3, code4, code5, code6
! num shows number of location. in this case 3
! code 1 to 6 shows x and y variable. and code1 and 2 have to be paired. 
! as well as this, code 3 and 4, code 5 and 6 have to be paired

! Declare TYPE
! set 1 to 3 show pair of (x, y)
TYPE Location
  INTEGER :: set1, set2, set3
  INTEGER :: num_locations
END TYPE

! Array ()
! for number of locations to visit
TYPE(Location), DIMENSION(:) :: numLocationArray(1000)

! allocatable array
! For locations
TYPE(Location), DIMENSION(:, :) :: LocationArray
ALLOCATABLE :: LocationArray

! allocate LocationArray
ALLOCATE(LocationArray(x, y), STAT = astat)
  IF (astat < 0) STOP "allocate failed"

! open input file to copy info into array
OPEN (UNIT = 10, File ="input.txt", STATUS = "OLD", ACTION = "READ", &
IOSTAT = ioStatus)
IF (ioStatus < 0) STOP "open failed"
! format of the file  
100 FORMAT (I1, /, 2I2, /, 2I2, / 2I2)

! Do loop to set table
DO x = 0, size(LocationArray), 1
   READ (UNIT = 10, FMT = 100, IOSTAT = ioStatus) num, code1, code2, &
   code3, code4, code5, code6
   ! check whether program read file correctly  (option) 
        PRINT *, num, code1, code2, code3, code4, code5, code6

   IF (x == code1) THEN
       DO y = 0, size(LocationArray), 1
          IF (y == code2) THEN
             LocationArray%set1 = LocationArray(x, y)
              ! check whether copied correctly
            PRINT *, LocationArray(x, y)
        PRINT *, LocationArray%set1
      END IF
   END DO
   END IF
 END DO

! ==============
! execution part
! ==============

! instructions:
! use pointer to do excecution

! read allocatable array above
! do excecution (distance) ** do not forget to go back to the original place (0,0)
!                          ** do not forget to try every single possible way
! when get total distance, do distance times 2 and figure out cost
! print all info (cost, distance, and steps)
! (example of output)
!  The minimum cost is    36
!  The distance travelled is    18
!  Step  1: Start at (  0,   0)
!  Step  2: Goes to (  2,   1)
!  Step  3: Goes to (  3,   5)
!  Step  4: Goes to (  4,   0)
!  Step  5: Ends at (  0,   0)

END PROGRAM

这个程序不起作用......我有一个错误:

LocationArray%set1 = LocationArray(x, y)
Error: Can't convert TYPE(location) to INTEGER(4) at (1)

我厌倦了解这个错误,但我做不到 有没有人对我的编码提出任何建议或建议?

原谅我的英语,我是日本人。

如果有人对我的问题有疑问(我的意思是需要更多解释),请告诉我。

谢谢。 UKA

2 个答案:

答案 0 :(得分:1)

在类型Location的定义中,您已经说过set1,set2和set3是整数变量,然后您尝试为其分配一个数组。我认为你想要的是因为这些是对,所以set1,set2和set3是一个大小为2的整数数组。

如果您将Location类型更改为:

,该怎么办?
TYPE Location
  INTEGER, DIMENSION(2) :: set1, set2, set3
  INTEGER :: num_locations
END TYPE

读取数据的循环对我来说也没有意义。我想我会把它写成(注意Fortran中的数组默认是从1开始的,而不是像C中那样从零开始):

DO x = 1, size(numLocationArray), 1
   READ (UNIT = 10, FMT = 100, IOSTAT = ioStatus) num, code1, code2, &
   code3, code4, code5, code6
   ! check whether program read file correctly  (option) 
        PRINT *, num, code1, code2, code3, code4, code5, code6

   numLocationArray(x)%num_locations = num
   numLocationArray(x)%set1(0) = code1
   numLocationArray(x)%set1(1) = code2
   numLocationArray(x)%set2(0) = code3
   numLocationArray(x)%set2(1) = code4
   numLocationArray(x)%set3(0) = code5
   numLocationArray(x)%set3(1) = code6
END DO

您显然需要做一些事情来检测和处理文件结束条件。

如果位置的数量确实是可变的,那么您需要执行以下操作:

TYPE Coordinate
   INTEGER :: x
   INTEGER :: y
END TYPE

TYPE Locations
   TYPE(Coordinate), DIMENSION(:), ALLOCATABLE :: location
   INTEGER :: num_locations
END TYPE

TYPE(Location), DIMENSION(:) :: numLocationArray(1000)

! open input file to copy info into array
OPEN (UNIT = 10, File ="input.txt", STATUS = "OLD", ACTION = "READ", &
IOSTAT = ioStatus)
IF (ioStatus < 0) STOP "open failed"
! format of the file  
100 FORMAT (I1 )
200 FORMAT (2I2)

DO n = 1, size(numLocationArray), 1
   READ (UNIT = 10, FMT = 100, IOSTAT = iostatus) num

   numLocationArray(n)%num_locations = num

   ALLOCATE (numLocationArray(n)%locations(num), STAT = astat)
   if (astat < 0) STOP 'allocate failed'

   DO l = 1, num, 1
      READ (UNIT = 10, FMT = 200, IOSTAT = iostatus) x, y
      numLocationArray(n)%locations(l)%x = x
      numLocationArray(n)%locations(l)%y = y
   END DO
END DO

答案 1 :(得分:0)

看起来你试图给一个整数值两个整数。这就像你试图做的那样=(或var = 5,5)。