在 CentOS 上学习 Fortran 编程,可以按下面几个步骤来,从环境准备 → 语法学习 → 动手练习 → 进阶提高,比较系统。
CentOS 默认通常没有 Fortran 编译器,需要安装 gcc-gfortran:
sudo yum install gcc-gfortran
安装完成后检查:
gfortran --version
看到版本号说明安装成功。
可以使用:
vim(终端)nano(轻量)示例(vim 写代码):
vim hello.f90
program hello
print *, "Hello, Fortran!"
end program hello
gfortran hello.f90 -o hello
./hello
输出:
Hello, Fortran!
program main
! 变量声明
implicit none
integer :: a
a = 10
print *, a
end program main
✅ 强烈建议:
implicit none
避免隐式类型错误。
integerrealdouble precision / real(8)characterlogicalif (a > 0) then
print *, "positive"
else
print *, "non-positive"
end if
do i = 1, 10
print *, i
end do
real :: x(10)
x = 0.0
x(1) = 1.0
subroutine add(a, b, c)
real, intent(in) :: a, b
real, intent(out) :: c
c = a + b
end subroutine add
Fortran 常用于:
可以练:
open, read, write)Makefile 管理项目module如果你愿意,我可以:
你现在是零基础还是已经会别的编程语言?