在Linux系统中,使用Fortran进行格式化输出时,通常需要使用特定的库和函数
首先,确保已经安装了gfortran
编译器。如果没有安装,可以使用以下命令安装:
sudo apt-get install gfortran
接下来,创建一个名为format_output.f90
的Fortran源文件,并添加以下内容:
program format_output
implicit none
character(len=10) :: name
integer :: age
write(*, '(A10, I3)') name, age
end program format_output
在这个示例中,我们定义了一个包含字符数组name
和整数变量age
的程序。然后,我们使用write
函数将name
格式化为最多10个字符的字符串,紧接着输出一个空格,然后将age
格式化为一个3位数的整数。
保存文件后,使用以下命令编译源文件:
gfortran format_output.f90 -o format_output
这将生成一个名为format_output
的可执行文件。现在,运行这个程序:
./format_output
程序将提示你输入名字和年龄,然后按照指定的格式输出结果。例如:
John Doe 25
Alice Johnson 30
这就是如何在Linux系统中使用Fortran进行格式化输出的方法。