|  | 
 
| 本帖最后由 McP 于 2015-5-17 21:44 编辑 
 使用函数子程序读取文件中的数据并代入递归子程序计算,编译无误,运行显示错误为forrtl:severe(170):program exception - stack overflow。
 因为这只是测试子程序用的,实际数据量可能更大,猜想是不是应该用直接文件来读取呢(但之前发过贴求助,大大们建议使用顺序文件。)?若用直接文件的话,还望指点如何编程。。。之前就是因为用直接文件失败才改为顺序文件的。。。谢谢诸位大大!!!!!!!
 
 [Fortran] syntaxhighlighter_viewsource syntaxhighlighter_copycode function P(n) result(Pa)
implicit none
integer::i
integer,intent(in)::n
real::a(1827),b(1827),c(1827),d(1827),e(1827),Pa
open(1,file='et.csv')
read(1,*)
do i=1,1827
    read(1,*)a(i),b(i),c(i),d(i),e(i)
enddo
close(1)
Pa=a(n)
end function
function ET(n) result(ETa) 
implicit none
integer::z
integer,intent(in)::n
real::a(1827),b(1827),c(1827),d(1827),e(1827),ETa
open(1,file='et.csv')
read(1,*)
do  z=1,1827
    read(1,*)a(z),b(z),c(z),d(z),e(z)
enddo
close(1)
ETa=a(n)
end function
recursive function th(n) result(tha) 
implicit none
real::tha,n,thmax,P,ET
if(n==1)then
        tha=thmax
else
        tha=th(n-1)+P(n-1)-ET(n-1) 
endif
if(tha>thmax) tha=thmax
end function
program main
implicit none
integer::n,th
print*,'input n'
read*,n
print*,th(n)
end program main
 csv数据文件结构如下
 0.432475628753395,0.9,1980,1,1
 0.342833411868196,0,1980,1,2
 0.774539737844607,0,1980,1,3
 0.891810760279446,0,1980,1,4
 
 
 | 
 |