如何在fortran中将字符转换为整数?
问题描述:
如何操纵命令行参数?例如
How to manipulate the command line argument? for example
te.f90
program print_
integer :: i
character(len = 32) :: arg
i = 1
Do
call get_command_argument(i, arg)
if ( len_trim(arg) == 0) exit
write(*,*) trim(arg)
write(*,*) trim(arg)**2
i = i + 1
end do
end program print_
te.sh
#!/bin/bash
for (( x = 1; x <=3; x++ ))
do
./te $x
done
我通过 $ x
作为 arg
类型为 character
,但是我想在执行 arg
作为数字进行操作> ./ te.sh ,我得到了错误提升(1)处的二进制数值运算符'**'的运算是CHARACTER(1)/ INTEGER(4)
。
I pass $x
as arg
which has type character
, but I want to manipulate arg
as a number when I execute ./te.sh
, I got the error promotion Operands of binary numeric operator '**' at (1) are CHARACTER(1)/INTEGER(4)
.
该怎么办?
答
您需要将字符串(arg)转换为整数。
You'll need to convert the string (arg) into an integer.
program print_
integer :: i, iarg
character(len = 32) :: arg
i = 1
Do
call get_command_argument(i, arg)
if ( len_trim(arg) == 0) exit
write(*,*) trim(arg)
read(arg,"(I)") iarg
write(*,*) iarg**2
i = i + 1
end do
end program print_