2007年1月28日 星期日

加入前面兩個 副程式以後的 費氏數列程式

重複使用的副程式,鞥該能夠獨立在 主程式的外面,
我們 接下來所要討論的主題,
就是 如何解決 這個問題。


[code]

! file: VF26.F90

subroutine skip1(no)
implicit none
integer no, i

! limits no in 1 .. 20
if ((no >= 1) .AND. (no <= 20)) then
! no is OK!
do i=1, no
print *, ' '
end do
else
! no is invalid
print *, ' '
end if

end ! of sub. skip1()
! -----------------------------------------------

subroutine pause1
implicit none
integer no

print *, 'Input 0 for stop the program, others for continue: '
read(*, '(I2)')no

if (no .EQ. 0) then
stop
end if

end ! of sub. pause1()
! -----------------------------------------------

! begin of main()
implicit none

integer no, f1, f2
real r1, r2
integer fs !for function 費氏數列

r2= (1.0 + sqrt(5.0))/2.0
call skip1(3)
print *, 'r2= ', r2
print *, '費氏數列的前後兩項的比值 會趨近於 黃金分割'
! -----------------------------------------------

! 以費氏數列 示範遞迴程式的寫作
call skip1(3)
do no=1, 50, 1
f1= fs(no+1)
f2= fs(no)
r1= float(f1)/float(f2)

print *, 'no= ', no, 'f1= ', f1, ', f2= ', f2, ', r1= ', r1
if ((mod(no, 10)) .EQ. 0) then
call skip1(1)
call pause1
end if
end do

end
! -----------------------------------------------

recursive integer function fs(no)
implicit none
integer no

if (no <= 1) then
fs= 1
return
else
fs= fs(no-1) + fs(no-2)
return
end if

end
! end of file



[/code]

沒有留言: