在 Ubuntu 上学习 Fortran 编程,可以按下面的步骤来,从环境搭建 → 基础语法 → 练习项目 → 进阶资源一步步来。
最常用的 Fortran 编译器是 gfortran(GNU Fortran)。
sudo apt update
sudo apt upgrade
sudo apt install gfortran
gfortran --version
看到版本信息就说明安装成功。
nano hello.f90
program hello
print *, "Hello, Fortran!"
end program hello
gfortran hello.f90 -o hello
./hello
输出:
Hello, Fortran!
program main
! 变量声明
integer :: a
real :: b
a = 5
b = 3.14
print *, a, b
end program main
integer:整数real:单精度浮点double precision / real(8):双精度character:字符串logical:逻辑值do i = 1, 10
print *, i
end do
if (a > 0) then
print *, "positive"
else
print *, "non-positive"
end if
real :: x(5)
x = [1.0, 2.0, 3.0, 4.0, 5.0]
print *, x(3)
subroutine add(a, b, c)
real, intent(in) :: a, b
real, intent(out) :: c
c = a + b
end subroutine add
open, read, write)module)fortran examplemakegdb如果你愿意,我可以:
你学 Fortran 主要是为了什么?(科研 / 工程 / 课程 / 兴趣)