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

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

问题描述

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

 程序Test_spacing_print 
整数:: 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
打印20,A,B
20格式('A =',F8 .2,x,'B =',F8.2)

结束程序Test_spacing_print

给我输出:

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

我想摆脱在$ = 之后得到的不需要的空间,也就是说我的输出看起来应该像(code> = 之后的空格) :

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

Fortran有可能吗?

解决方案

你说你在输出中有不需要的空间,但是你有你的空间指定明确的格式。如果您没有提供格式,则列表导向输出意味着您没有对间距进行说明。



输出 A 你有编辑描述符 F8.2 :字段宽度将是8.在小数点后面有两位数字,小数点前的数字(和符号)的数字。因此,对于 A 1. ,如果没有可选标志,您将有四个空格。
$ b

就像Fortran 95引入了 I0 编辑描述符,所以它允许 F0.d 。 [对于其他描述符,尽管后来添加了 G0.d ]。 F0.2 将提供最小字段宽度与小数点后的这两个数字,这是你想要的。请注意,您需要在 = 符号之后显式添加一个空格:

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



<我也使用了编辑描述符以避免尾随空白。]

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


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

which gives me the output:

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

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

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.

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.

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.]

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天全站免登陆