One Loop根本不起作用,为什么?

时间:2012-08-10 09:07:20

标签: gfortran

我想问一下是否有人知道为什么我在Fortran中编写的这个程序的第一个循环根本不起作用。对于do r = 0, 5, 0.1,只需读取r的第一个数字r=0。我能做些什么使它成为可能?

implicit none

real , allocatable ::x(:)
real , allocatable ::y(:)
real , allocatable ::h(:)
integer i , n , a , j , d  
real hp1 , s , c , r      
real , allocatable ::T1(:)

open (10,file='point.txt', status='old')
open (6,file='pointr2.txt',status='new') 

read (10,*) n

allocate (x(n))
allocate (y(n))
allocate (h(n)) 
allocate (T1(n))

s=0
d=0
c=0

do r=0.0000 , 5.0000 , 0.1
  do j= 1, n
    if ( j.gt.1 ) goto 39
    do i=1,n
      read (10,*)  x(i), y(i), h(i)
      write (6,*) x(i), y(i), h(i)
    end do

    close (10)
    call highest ( h,n,hp1,a )
    write (6,*) hp1 
    write (6,*) ' The First highest point number' , a  
    write (6,*)  x(a)

    39 if ( j.eq. a ) goto 100

    s=abs((h(j)-h(a))/(x(j)-x(a)))
    d=((x(j)-x(a))**2+(y(j)-y(a))**2+(h(j)-h(a))**2)**0.5
    c=((x(j)-x(a))**2+(y(j)-y(a))**2)
    T1(j)=atan((y(a)-y(j))/(x(a)-x(j)))

    if ( c .eq.r**2 .and. d .ge. 0.0025 .and.s.ge.0.and.s .le. 0.04) then
      T1(j)=  atan((y(a)-y(j))/(x(a)-x(j)))
      write (6,*) 'Group 1' , j  ,T1(j) 
    end if                               
    write (6,*) s, c ,T1(j)  
    100 end do 
  end    
do
end

subroutine highest ( h,n,hp1,a )
  implicit none

  integer i , n ,a 
  real hp1  
  real h(n)  
  hp1=h(1)
  a= 1

  do i=1,n
    if ( h(i) .gt.hp1  ) then
      hp1=h(i) 
      a = i  
    end if 
  end do 
end subroutine 

我的意见是:

6
0.01     0.02        0.03
0.13     0.14        0.1504
0.04     0.05        0.06
0.07     0.08        0.15  
0.10     0.11        0.12
0.15     0.042020410 0.15

1 个答案:

答案 0 :(得分:2)

你在内循环的第一次迭代时关闭单位10,再也不打开它:

close (10)   <------ HERE
call highest ( h,n,hp1,a )
write (6,*) hp1

因此,在外部do循环的第二次迭代中,程序尝试从封闭单元读取。在大多数编译器中,这会导致在名为OPEN的文件上发出隐式fort.10语句,这个语句肯定不存在。 gfortran创建长度为零的文件,因此在尝试从中读取并且程序失败时会立即触发EOF。

为了防止出现这种情况,您应该移动从point.txt读取的代码,并在循环外填充x(i)y(i)h(i)

另请注意,Fortran 95引入了DO循环控件中的所有内容都应为标量INTEGER类型的要求,即F95及更高版本不允许do r=0.0000 , 5.0000 , 0.1。如果您提供gfortran选项开关,则-std=f95无法编译您的程序:

do r=0.0000 , 5.0000 , 0.1
   1
Error: Deleted feature: Loop variable at (1) must be integer
相关问题