问题描述
write(*,FMT =(/,X,17(' - '), /,2X,A,/,X,17(' - ')))My Program Name
在控制台窗口中显示以下几行:
------------- ----
我的程式名称
-----------------
现在,我想以上述格式显示一个预定义的字符,而不是 -
。我试过这段代码没有成功:
$ p $ 字符参数:: Chr = Achar(6)
$ b $ (*,FMT =(/,X,17(< Chr>),/,2X,A,/,X,17(< Chr>)))My Program Name
显然,还有另外一种方法来显示我想通过格式说明符语句中的变量来显示的内容。例如:
pre $ code $ character code $ (*,FMT =(/,X,< iMax> A1,/,2X,A,/,X,< iMax> A1))(Chr,i = 1 ,iMax),&
我的程式名称,&
(Chr,i = 1,iMax)
不过,我想知道有什么方法可以在格式说明符语句中使用一个变量或调用一个函数。
您试图使用的代码(<>
)不是标准的Fortran。它是一些被 编译器接受的扩展。
(/,X,17(// Chr //), /,2X,A,/,X,17(// Chr //))
对于数字的情况下,你必须准备一个字符串的值为
$ $ p $ $ $ c $写入(chMax,*)iMax
(/,X,// chMax //A1,/,2X,A,/,X,// chMax //A1)
或者你可以使用一些函数,如果你有它的话 但事先调用它可能还是更可取的,以避免多次调用。 函数可能看起来像: I can use: to display the following lines on the console window: Now, I want to show a pre-defined character instead of Obviously, there are another ways to display what I am trying to show by means of a variable in the format specifier statement. For instance: However, I would like to know if there is any way to use a variable or invoke a function in the format specifier statement. The code you are trying to use ( For the the numeric case you have to prepare a string with the value or you can use some function, if you have it but it may still be preferable to call it beforehand, to avoid multiple calls. The function can look like: 这篇关于如何在格式说明符语句中使用变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
$ $ $ $ $ $ $ c>(/,X,// itoa(iMax)//A1,/,2X,A,/,X// itoa(iMax)//A1)
函数itoa(i)结果(res)
字符),allocatable :: res
整数,意图(in):: i
字符(范围(i)+2):: tmp
write(tmp,'(i0)')i
res = trim(tmp)
结束函数
write (*, FMT = "(/, X, 17('-'), /, 2X, A, /, X, 17('-'))") "My Program Name"
-----------------
My Program Name
-----------------
-
in the above format. I tried this code with no success:character, parameter :: Chr = Achar(6)
write (*, FMT = "(/, X, 17(<Chr>), /, 2X, A, /, X, 17(<Chr>))") "My Program Name"
character, parameter :: Chr = Achar(6)
integer :: i, iMax = 17
write (*, FMT = "(/, X, <iMax>A1, /, 2X, A, /, X, <iMax>A1)") (Chr, i = 1, iMax), &
"My Program Name", &
(Chr, i = 1, iMax)
<>
) is not standard Fortran. It is an extension accepted by some compilers. Just build the format string as a string."(/, X, 17(" // Chr // "), /, 2X, A, /, X, 17(" // Chr // "))"
write(chMax, *) iMax
"(/, X, " // chMax // "A1, /, 2X, A, /, X, " // chMax // "A1)"
"(/, X, " // itoa(iMax) // "A1, /, 2X, A, /, X, " // itoa(iMax) // "A1)"
function itoa(i) result(res)
character(:),allocatable :: res
integer,intent(in) :: i
character(range(i)+2) :: tmp
write(tmp,'(i0)') i
res = trim(tmp)
end function