我尝试用现代 fortran(2018 年?)编写代码,我的目标之一是能够将像这样的子例程(旧的 fortran 77)转换为最新的 fortran(2018 年?)
subroutine nobin(e,j,f)
common/binin/bini(40),nbn
er=e
do 100 i=j,40
er=er-bini(i)
if(er.le.0.0) go to 200
100 continue
f1=er/bini(40)
f=41.+f1-float(j)
if(j.gt.40) f=f1
go to 300
200 f=float(i-j)+er/bini(i)+1.0
300 continue
return
end
到目前为止,我能够写出这样的东西(见下文),但我只对标量变量成功了。当我必须使用数组时,我有点卡住了。特别是我应该分配然后给数组 Bini 一些值(旧 fortran 子例程中的公共块)。然后在类中定义的函数(find_bin_nobin)中使用这些数组。感谢任何建议以及对代码的任何正式改进。非常感谢!
module precision
implicit none
public
integer, parameter :: pr = selected_real_kind(12,300)
integer, parameter :: ir = 4
end module precision
module constants
use precision
implicit none
public
real ( kind = pr ), parameter :: zero = 0.0_pr
real ( kind = pr ), parameter :: one = 1.0_pr
real ( kind = pr ), parameter :: four = 4.0_pr
real ( kind = pr ), parameter :: pi = four * atan(one)
end module constants
module class_Dntrnpr
use precision
use constants, only : zero, one
implicit none
type Find_bin
real ( kind = pr ) :: e
integer( kind = ir ) :: j
real ( kind = pr ), allocatable, dimension(:) :: bin
contains
procedure :: f => find_bin_nobin
end type Find_bin
contains
function find_bin_nobin(x, n) result(f)
class(Find_bin), intent(inout) :: x
integer( kind = ir ), intent(in) :: n
real ( kind = pr ) :: f, f1, er
integer( kind = ir ) :: i, k
allocate(x%bin(n))
er = x%e
i = x%j
k = i
er = er - x%bin(i)
do while ( er <= zero )
er = er - x%bin(i)
i = i + 1
enddo
f1 = er / x%bin(n)
f = ( n + 1 ) + f1 - dble(k)
if ( k > n ) then
f = f1; return
else
f = dble(i-k) + er / x%bin(i) + one
endif
return
end function find_bin_nobin
end module class_Dntrnpr
program test
use class_Dntrnpr
implicit none
integer( kind = ir ) :: nbmax,i
real ( kind = pr ), allocatable, dimension(:) :: bini
type(Find_bin) :: en
! type(Find_bin), allocatable, dimension(:) :: bin
en%e = 2.0
en%j = 1
nbmax = 40
allocate(bini(nbmax))
bini = 1.0
end program test