在fortran90中智能打印整数 [英] Smart printing of integers in fortran90

查看:93
本文介绍了在fortran90中智能打印整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几年前简要介绍了Fortran77之后,我正在学习Fortran90.在Fortran中打印整数时,必须指定要保留多少空间来打印整数.考虑这个程序...

I'm learning Fortran90 after a brief introduction to Fortran77 a few years ago. When printing integers in Fortran, you must specify how many spaces you want to reserve for printing the integer. Consider this program...

implicit none

integer :: i
i = 123

write(*, '(A, I3, A)')  "'", i, "'"  !3 spaces for output = no padding
write(*, '(A, I5, A)')  "'", i, "'"  !5 is too many, so output is padded
write(*, '(A, I2, A)')  "'", i, "'"  !2 is too few, so output is jibberish
write(*, '(A, I:, A)')  "'", i, "'"  !Default behavior

end program

...将生成以下输出.

...which generates the following output.

'123'
'  123'
'**'
'         123'

当我不知道整数中有多少位数时,如何为整数打印分配正确的空间?

How do I allocate the correct amount of space for integer printing when I do not know how many digits are in the integer?

更新:如果您的编译器兼容F95,则可以使用I0编辑描述符(即,在上面的示例中,将'(A, I0, A)'用作write函数的第二个参数)谢谢@janneb!

Update: If your compiler is F95-compliant, you can use the I0 edit descriptor (i.e., put '(A, I0, A)' for the second argument of the write function in my example above. Thanks @janneb!

推荐答案

使用I0编辑描述符.好吧,要成为适用于Fortran 95的学究型IIRC,因此,如果您真的严格要求不超过F90,那么我想这是行不通的.

Use the I0 edit descriptor. Well, to be pedantic IIRC that is Fortran 95, so if you're really strict about no more than F90, then I suppose this won't work.

这篇关于在fortran90中智能打印整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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