huiselilun 发表于 2016-10-14 16:23:29

变数组问题

我需要向一个数组data(n)中添加数据,n是迭代步数,一开始不确定。当迭代结束后以(自然数-data(n))画曲线,我该怎么定义数组?

vvt 发表于 2016-10-14 16:27:12

用链表。

Program www_fcode_cn
Implicit None
Integer :: iCount , i
Real :: rRead
Type :: stPLink
    Real :: rValue
    Type(stPLink) , Pointer :: pNext
End Type stPLink
Type( stPLink ) , Target :: stLocal
Type( stPLink ) , Pointer :: pLocal
pLocal => stLocal
iCount = 0
rRead = 1.0
write(*,*) '请输入一些数,输入0结束:'
Do
    Read(*,*) rRead
    If ( rRead < 0.0001 ) then
      Exit
    End If
    pLocal%rValue = rRead
    Allocate( pLocal%pNext )
    pLocal => pLocal%pNext
    iCount = iCount + 1
End Do
pLocal => stLocal
Do i = 1 , iCount
    Write(*,*) pLocal%rValue
    pLocal => pLocal%pNext
End Do
End Program www_fcode_cn

huiselilun 发表于 2016-10-17 22:11:13

vvt 发表于 2016-10-14 16:27
用链表。

Program www_fcode_cn


我正想问群里有没有指针教程,然后就看见要出的下一个视频就是关于指针的:-)

huiselilun 发表于 2016-10-20 15:24:27

本帖最后由 huiselilun 于 2016-10-20 15:28 编辑

Type :: stPLink
      Double Precision :: Value
      Type(stPLink) , Pointer :: pNext
End Type stPLink
.
.
.
Type( stPLink ) , Pointer :: pwor(:)
.
.
.
Allocate(pwor%pNext, stat=err)


如果我的链表里的数据值是个数组怎么办?上面是我的定义,分配内存的时候提示pNext错误:

error #6563: A component with POINTER attribute may NOT be to the right of an array component (6.1.2).   

pasuka 发表于 2016-10-20 19:29:57

考虑混合的编程的话
C可以参考
http://troydhanson.github.io/uthash/utlist.html
http://troydhanson.github.io/uthash/utarray.html
C++直接上vector
考虑写入外存的话
直接按照一定的规则写成文本或二进制文件,用得顺手就行

huiselilun 发表于 2016-11-4 09:46:41

pasuka 发表于 2016-10-20 19:29
考虑混合的编程的话
C可以参考
http://troydhanson.github.io/uthash/utlist.html


已经用fortran链表完美解决了,谢谢
页: [1]
查看完整版本: 变数组问题