Fortran 中是否可以将零值变量输出为空白?

Leo*_*eon 2 variables fortran output

我想在格式化文件中输出实际变量。如果变量非零,则使用格式语句。但如果变量为零,则仅输出空格,类似于 Iw.0 的做法。是否可以在格式语句中执行此操作?谢谢。

M. *_* B. 5

不,不是使用格式语句,但是通过将值写入字符串并进行处理,这相当容易做到。下面是一个演示。可能最好放入子程序中。

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
Run Code Online (Sandbox Code Playgroud)