Dem*_*emi 3 fortran command-line-arguments fortran90 fortran2003
我不知道这个自由形式的Fortran程序有什么问题.它无法正确处理其命令行参数.
如果我使用静态数组作为命令行参数而不是数组,它可以工作allocatable.
此外,这是一个很好的第一个Fortran程序吗?这是Fortran有用的问题类型吗?我已经知道C,C++和一点点D.
module fibonacci
use ISO_FORTRAN_ENV
implicit none
contains
subroutine output_fibonacci(ordinal)
! Declare variables
integer, parameter :: LongInt = selected_int_kind (38)
integer, intent(in) :: ordinal
integer :: count
! integer (kind=LongInt) :: count, compare=2
integer (kind=LongInt), dimension(2,2) :: matrix, initial
matrix=reshape((/ 1, 1, 1, 0 /), shape(matrix))
initial=reshape((/ 1, 0, 0, 1 /), shape(initial))
count = ordinal
! Do actual computations
do while (count > 0)
! If the exponent is odd, then the output matrix
! should be multiplied by the current base
if (mod(count,2) == 1) then
initial = matmul(matrix, initial)
end if
! This is the squaring step
matrix = matmul(matrix, matrix)
count = count/2
end do
write (*,*) initial(1,2)
end subroutine output_fibonacci
end module fibonacci
program main
use, intrinsic :: ISO_FORTRAN_ENV
use fibonacci
implicit none
! The maximum allowed input to the program
integer :: max=200, i, size=20
character, allocatable :: argumen(:)
integer :: error, length, input
allocate(argumen(size))
! write(*,*) argcount
do i=1, command_argument_count()
call get_command_argument(i, argumen, length, error)
read(argumen,*,iostat=error) input
! write(*,*) argument
! write (*,*) input
if (error .ne. 0) then
write(ERROR_UNIT,'(I36.1,A)') input, "is not an integer"
stop (1)
else if (input > max) then
write(ERROR_UNIT,'(A,I36.1,A)') "Input ", input, " is too large"
stop (1)
end if
call output_fibonacci(input)
end do
end program
Run Code Online (Sandbox Code Playgroud)
这条线
character, allocatable :: argumen(:)
Run Code Online (Sandbox Code Playgroud)
声明一个可分配的字符数组.所以声明
allocate(argumen(size))
Run Code Online (Sandbox Code Playgroud)
制作argumen一个包含20个单字符元素的数组.这不是在Fortran中处理字符串的常用方法,并且argumen与调用中的第二个参数的要求不匹配(在类型或等级中)get_command_argument.
相反,你应该写
character(len=:), allocatable :: argumen
Run Code Online (Sandbox Code Playgroud)
声明argumen为可分配长度的字符变量.在某些情况下,您可以简单地分配给这样的变量,例如
argumen = 'this is the argument'
Run Code Online (Sandbox Code Playgroud)
无需事先明确分配.
对于英特尔Fortran v14,调用get_command_argument编译时没有警告但在执行时,参数argumen不会自动分配,并且仍然未分配.老实说,我不确定这种行为是否符合标准.一种方法是进行两次调用get_command_argument,首先获得参数的大小,然后获得参数; 像这样
do i=1, command_argument_count()
call get_command_argument(i, length=length, status=error)
allocate(character(length)::argumen)
call get_command_argument(i, argumen, status=error)
! do stuff with argument
deallocate(argumen)
end do
Run Code Online (Sandbox Code Playgroud)
使用length要分配的变量的名称,调用的可选参数返回的值length是合法的,但是混淆了一个mite.该deallocate语句确保argumen可以为下一个参数再次分配.
我将告诉你一个可分配的可分配长度字符数组的声明和使用.
免责声明:接下来的两段包含一些可能会发现主观的材料.我不会就这个答案的这些部分进行任何讨论.
这是一个很好的Fortran计划吗? 这比我在SO上看到的很多东西要好.就个人而言,我更喜欢现代的/=使用.ne.,<对于.lt(等),stop如果我可以避免它(我通常可以),我不会使用,我相信我可以找到其他的尼特来挑选.
这是Fortran有用的问题类型吗? Fortran对于所有类型的问题都很有用,但我承认使用它来编写Web服务器非常具有挑战性.