在Fortran中是否可以输出一个空值为零的变量? [英] Is it possible to output a variable with zero value as blank in Fortran?

查看:575
本文介绍了在Fortran中是否可以输出一个空值为零的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以格式化文件输出实际变量。如果变量不为零,则使用格式语句。但是如果变量为零,那么只会输出空格,类似于Iw.0所做的。格式声明中可以这样做吗?谢谢。

I would like to output real variables in a formatted file. If the variables are non-zero, format statements are used. But if variables are zero, then only blank spaces are outputted, similar to what Iw.0 does. Is it possible to do this in the format statements? Thank you.

推荐答案

不,不是格式化语句,但是通过将值写入字符串并处理,这相当容易实现。下面是一个演示。

No, not with a format statement, but this is reasonably easy to do by writing the values to a string and processing. Below is a demo. Probably better to put into a subroutine.

program demo

   real, dimension (6) :: values = [ 1.0, 2.0, 0.0, 4.0, 0.0, 6.0 ]
   character (len=100) :: string
   integer :: pos

   write (string,'( 6 (1X, F4.1 ) )' )  values
   write (55, '(A)' )  trim (string)

   MakeBlanks: do

      pos = index (string, "0.0")

      if ( pos < 1 )  exit MakeBlanks

      string (pos:pos+2) = "   "

   end do MakeBlanks

   write (55, '(A)' )  trim (string)

end program demo

这篇关于在Fortran中是否可以输出一个空值为零的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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