2013-02-04 128 views
2

我正在通過Metcalf,Reid和Cohen的「現代Fortran解釋」的方式工作,並在第四章中將斐波那契程序作爲一項作業問題。我的代碼不能編譯。我該如何糾正它?Fortran斐波納契困境

! Fibonacci here we go 
    integer :: lim, i 
    lim=0 
    read *, lim 

    integer, dimension(lim) :: fib 

    if(lim >= 1) 
     fib(1) = 0 
    end if 

    if(lim >= 2) 
     fib(2) = 1 
    end if 

    if(lim >= 3) 
     fib(3) = 1 
    end if 

    do i=4, lim 
     fib(i) = fib(i-2) + fib(i-1) 
    end do 

    do i=1, size(fib) 
     print *, fib(i) 
    end do 

    end 

此外,這裏是我得到的錯誤。我會試圖縮短這個需求,但我不確定在查看Fortran錯誤日誌時需要什麼。

Compiling the source code.... 
$/usr/bin/gfortran /tmp/135997827718658.f95 -o /tmp/135997827718658 2>&1 
In file /tmp/135997827718658.f95:7 

integer, dimension(lim) :: fib 
1 
Error: Unexpected data declaration statement at (1) 
In file /tmp/135997827718658.f95:9 

if(lim >= 1) 
1 
Error: Unclassifiable statement in IF-clause at (1) 
In file /tmp/135997827718658.f95:10 

fib(1) = 0 
1 
Error: Unclassifiable statement at (1) 
In file /tmp/135997827718658.f95:11 

end if 
1 
Error: Expecting END PROGRAM statement at (1) 
In file /tmp/135997827718658.f95:13 

if(lim >= 2) 
1 
Error: Unclassifiable statement in IF-clause at (1) 
In file /tmp/135997827718658.f95:14 

fib(2) = 1 
1 
Error: Unclassifiable statement at (1) 
In file /tmp/135997827718658.f95:15 

end if 
1 
Error: Expecting END PROGRAM statement at (1) 
In file /tmp/135997827718658.f95:17 

if(lim >= 3) 
1 
Error: Unclassifiable statement in IF-clause at (1) 
In file /tmp/135997827718658.f95:18 

fib(3) = 1 
1 
Error: Unclassifiable statement at (1) 
In file /tmp/135997827718658.f95:19 

end if 
1 
Error: Expecting END PROGRAM statement at (1) 
In file /tmp/135997827718658.f95:22 

fib(i) = fib(i-2) + fib(i-1) 
1 
Error: Statement function at (1) is recursive 
Error: Unexpected end of file in '/tmp/135997827718658.f95' 
+0

如果你習慣於Linux,我確定在CygWin中有一個gfortran或fort77。這個假設傷害較少。在網上尋找一個FORTRAN教程(自FORTRAN 77以來,已經有了大量的語言變化,所以我甚至無法分辨上述語法是否正確:-( – vonbrand

回答

4

隨機修復....

如果塊應該是

if (condition) then 
    do something 
endif 

不能ommit 「然後」。

你不能去

read *, lim 
integer, dimension(lim) :: fib 

所有的聲明有來之前可執行代碼。所以相反,使用可分配的陣列

integer, dimension(:), allocatable :: fib 
read *, lim 
allocate(fib(lim)) 
+0

我不知道這個......是嗎?好東西知道嗎?結束的東西,但沒有啓動它,對我來說沒有太大的意義... – yosukesabai

+0

擺脫了關於程序語句的大驚小怪...如果編譯器不在乎,我不該關心。 – yosukesabai