如何在fortran中将字符转换为整数? [英] How to convert character to integer in fortran?

查看:799
本文介绍了如何在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_

这篇关于如何在fortran中将字符转换为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆