在Fortran中通过添加更新变量 [英] Updating the variable by addition in fortran

查看:186
本文介绍了在Fortran中通过添加更新变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FORTRAN 95.在代码中,我需要通过添加一些内容来更新许多变量。例如,如果x是我的变量,我可能需要这样做:

  x = x + 1 

问题是我的变量是数组元素并且有大名字等等,所以在上面的等式中重复x是繁琐的任务。例如在Python中,我们有+ =操作符来实现这一点。我们在FORTRAN有类似的东西吗?

提前致谢

解决方案

不,Fortran没有这个运营商。但是,您可以实现一个子例程来执行此操作:

 元素子例程my_incr(var,incr)
隐式无
整数,意图(inout):: var
整数,意图(in):: incr

var = var + incr
结束子程序

然后你可以在你的代码中调用它:

 ! ... 
呼叫my_incr(x,1)
! ...

由于元素性质你可以在数组上执行这个操作:

 ! ... 
调用my_incr(array(:),1)
! ...


I am using FORTRAN 95. In the code I need to update many variables by adding something to them. For example, if x is my variable, I may need to do something like this:

x = x + 1

The problem is that my variables are array elements and have big names etc so repeating x in the above equation is cumbersome task. In python for example we have += operator to achieve this. Do we have something similar in FORTRAN?

Thanks in advance

解决方案

No, Fortran does not have this operator. However, you could implement a subroutine to do so:

elemental subroutine my_incr( var, incr )
  implicit none
  integer,intent(inout) :: var
  integer,intent(in)    :: incr

  var = var + incr
end subroutine

Then you could call that in your code:

! ...
call my_incr( x, 1 )
! ...

Due to the elemental nature of the subroutine, you can also perform this operation on arrays:

! ...
call my_incr( array(:), 1 )
! ...

这篇关于在Fortran中通过添加更新变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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