传递字符串以在Fortran子例程中执行 [英] Passing strings for execution in Fortran subroutines

查看:252
本文介绍了传递字符串以在Fortran子例程中执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的子例程中,我想传递一个名为 str 的字符串变量。如果它是'poly''gaus''slat',那么它有一个预定义的动作( fval = 参见下面的代码)。我希望让用户指定一个函数来使用并将其作为字符串变量传递。



这就是...



如果 str ='3 * cos(i * t)',那么我想要 fval 等于 3 * cos(i * t)

 子程序f(fval,i, t,str)
隐式无
整数,参数:: ikind = selected_int_kind(8)
整数,参数:: dbl = selected_real_kind(15,307)

整数kind = ikind):: i
real(kind = dbl):: fval,t
character str * 100

if(str .eq。'poly')then
fval = t ** i
elseif(str .eq。'slat')then
fval = exp(-i * t)
elseif(str .eq。'gaus' )然后
fval = exp(-i * t * t)
else
fval = ???
endif

结束子程序


解决方案

你不能。不容易。但您可以做两件事:


In the following subroutine I would like to pass a string variables named str. If it is 'poly', 'gaus', 'slat', then it has a predefined action (fval = see code below ). I would like to have the user specify a function to use and pass that as a string variable.

That is ...

If str = '3*cos(i*t)', then i would like to have fval be equal to 3*cos(i*t). How can I get Fortran to interpret the string entered as a command to be executed by Fortran?

subroutine f(fval, i, t, str)
implicit none
integer, parameter :: ikind = selected_int_kind(8)
integer, parameter :: dbl = selected_real_kind(15,307)

integer(kind = ikind) :: i
real(kind = dbl) :: fval, t
character str*100

if(str .eq. 'poly') then
    fval = t**i
elseif(str .eq. 'slat') then
    fval = exp(-i*t)
elseif(str .eq. 'gaus') then
    fval = exp(-i*t*t)
else
    fval = ???
endif

end subroutine

解决方案

you can't. not easily. there are two things you can do, though:

这篇关于传递字符串以在Fortran子例程中执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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