如何摆脱 Fortran 打印输出中不需要的间距? [英] How to get rid of unwanted spacing in Fortran's print output?

查看:26
本文介绍了如何摆脱 Fortran 打印输出中不需要的间距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能看起来像一个微不足道的问题,但我无法通过谷歌搜索找到任何答案.我有这个小程序:

It may look like a trivial issue, but I couldn't find any answer through googling. I have this little program :

Program Test_spacing_print
  Integer:: N
  Real:: A,B

  N=4; A=1.0; B=100.0

  print*,'N =',N

  print*,'A =',A,' B =',B
  print '(2(A3,F8.2,1X))' ,'A =',A,' B =',B
  print 20, A,B
  20 format('A =',F8.2,x,'B =',F8.2)

End Program Test_spacing_print

这给了我输出:

     N =           4
 A =   1.00000000      B =   100.000000
A =    1.00  B   100.00
A =    1.00 B =  100.00

我想去掉 = 符号后不需要的空格,即我想要的输出应该看起来像(= 后的 1 个空格):

I want to get rid of the unwanted space that I get after = sign, i.e. my desired output should look like (1 space after =):

 N = 4
 A = 1.00000000 B = 100.000000
 A = 1.00 B = 100.00
 A = 1.00 B = 100.00

在 fortran 中可以吗?

Is it possible in fortran ?

推荐答案

您说输出中有不需要的"空间,但您使用指定的显式格式恰好有您要求的空间.当您没有提供格式时,列表导向的输出意味着您对间距没有发言权.

You say that you have "unwanted" space in the output, but you have exactly the space that you asked for with your specified explicit formats. When you didn't provide a format, list-directed output means that you have no say on the spacing.

要输出 A,你有编辑描述符 F8.2:字段宽度为 8.小数点后有两位数,小数点本身离开你用五位数字表示小数点前的数字(和符号).因此,对于 A1. 没有打印可选符号,您将有四个空白.

To output A you have the edit descriptor F8.2: the field width will be 8. With two digits after the decimal point and the decimal point itself that leaves you with five digits for the digits (and sign) before the decimal point. So, for A value 1. without the optional sign printed you will have four blanks.

就像 Fortran 95 引入了 I0 编辑描述符,因此它允许 F0.d.[对于其他描述符,虽然 G0.d 是在以后添加的.] F0.2 将提供小数点后两位数字的最小字段宽度,即你想要什么.但请注意,您需要在 = 符号后显式添加一个空格:

Much as Fortran 95 introduced the I0 edit descriptor so it allows F0.d. [And for other descriptors, although G0.d was added even later.] F0.2 will provide the minimal field width with those two digits after the decimal point, which is what you want. Note, though, that you will need to explicitly add a blank after the = sign:

print '("N = ", I0)', N
print '(2(A4,F0.2,:,1X))' ,'A = ',A,'B = ',B

[我还使用了 : 编辑描述符来避免尾随空格.]

[I've also used the : edit descriptor to avoid trailing blanks.]

如果您想要一个真正的 Fortran 90 答案,就像您标记的那样,那么它不会那么好,但它仍然可以完成.

If you want a truly Fortran 90 answer, as you've tagged, then it won't be as nice but it still can be done.

这篇关于如何摆脱 Fortran 打印输出中不需要的间距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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