帮助需要 FORTRAN 中的可分配数组

发布于 2024-07-19 05:35:10 字数 2806 浏览 3 评论 0原文

我在可分配数组方面确实遇到了麻烦。

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

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)

我厌倦了找出这个错误,但我无法 有人对我的编码有任何建议或建议吗?

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

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

谢谢。 乌卡

I'm really having trouble with Allocatable array.

I have to copy all information from a file into allocatable array.
The file is like this:

3 
3 5 
2 1 
4 0

3 is the number of points
other six numbers shows points on the graph in (x, y) form.
So (3,5), (2, 1), (4,0) are the points.
But I have problem to make these number as a pair.

I tried to code, and here is my coding:

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

This program does not work...I have an error:

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

I tired to figure out this error, but I couldn't
Does anyone any advice or suggestion about my coding?

Forgive my English, I'm Japanese.

If anyone has questions about my question (I mean need more explanation), please let me know.

Thank you.
Uka

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江城子 2024-07-26 05:35:10

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

In the definition of the type Location, you've said that set1, set2, and set3 are integer variables, then you attempt to assign an array to it. I think what you want, since these are pairs, is to have set1, set2, and set3 be an integer array of size 2.

What if you change the Location type to be:

TYPE Location
  INTEGER, DIMENSION(2) :: set1, set2, set3
  INTEGER :: num_locations
END TYPE

Also the loop to read the data makes no sense to me. I think I'd write it as (note that arrays in Fortran are 1-based by default, not zero-based as in 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

You'll obviously need to do something to detect and handle the end of file condition as well.

If the number of locations is truly varaible, then you'd need to do something like:

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
梦初启 2024-07-26 05:35:10

看起来您正在尝试为一个整数提供两个整数的值。 这就像你尝试做 = (或 var = 5,5)。

Looks like you are trying to give an integer the value of two integeres. It's something like you try to do = (or var = 5,5).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文