Fortran语言基础教程笔记

Fortran语言基础教程笔记

下面是一个全面的 Fortran 语言入门教程,适合初学者和有一定编程经验的人士。Fortran(Formula Translation)是一种主要用于科学计算、工程分析和数值模拟的高级编程语言。它最初由 IBM 在 1950 年代开发,至今仍在许多高性能计算领域中广泛使用。

Fortran 简介

Fortran 的主要特点包括:

  • 高效性:特别适合处理大量数据和进行数值计算。
  • 标准化:有多个版本的标准,最新的标准是 Fortran 2018。
  • 现代特性:支持面向对象编程、模块化编程等现代编程特性。
  • 可移植性:编写的代码可以在多种平台上运行。

安装 Fortran 编译器

在 Windows 上安装
  1. 下载并安装 gfortran

    • 访问 GFortran for Windows 下载适用于 Windows 的 gfortran 编译器。
    • 按照说明进行安装。
  2. 验证安装

    • 打开命令提示符,输入以下命令:
      gfortran --version
      
    • 你应该能看到类似 GNU Fortran (GCC) 10.2.0 的版本号。
在 macOS 上安装
  1. 使用 Homebrew 安装 gfortran

    • 如果你还没有安装 Homebrew,可以通过以下命令安装:
      /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
      
    • 安装 gfortran:
      brew install gcc
      
  2. 验证安装

    • 打开终端,输入以下命令:
      gfortran --version
      
    • 你应该能看到类似 GNU Fortran (GCC) 10.2.0 的版本号。
在 Linux 上安装
  1. 使用包管理器安装 gfortran

    • 对于 Ubuntu/Debian:
      sudo apt-get update
      sudo apt-get install gfortran
      
    • 对于 CentOS/RHEL:
      sudo yum install gcc-gfortran
      
  2. 验证安装

    • 打开终端,输入以下命令:
      gfortran --version
      
    • 你应该能看到类似 GNU Fortran (GCC) 10.2.0 的版本号。

第一个 Fortran 程序

让我们从一个简单的“Hello, World!”程序开始。

  1. 创建文件

    • 使用你喜欢的文本编辑器创建一个名为 hello.f90 的文件,并在其中输入以下内容:
      program hello
          print *, "Hello, World!"
      end program hello
      
  2. 编译和运行程序

    • 打开终端或命令提示符,导航到包含 hello.f90 文件的目录,然后运行以下命令:
      gfortran -o hello hello.f90
      ./hello
      
    • 你应该会看到输出 Hello, World!

基本语法

注释
  • 单行注释使用 ! 符号。
  • 多行注释可以使用 C* 开头的行。
! 这是单行注释
C 这也是单行注释
* 这还是单行注释
变量
  • 变量名区分大小写。
  • 需要声明变量类型。
integer :: i
real :: x
character(len=10) :: name
logical :: is_valid
数据类型
  • 基本类型integer, real, double precision, complex, character, logical 等。
  • 派生类型:用户自定义类型。
type person
    character(len=20) :: name
    integer :: age
end type person

type(person) :: alice
alice%name = 'Alice'
alice%age = 30
字符串
  • 使用 character 类型定义字符串。
  • 支持字符串操作函数。
character(len=20) :: greeting
greeting = 'Hello, World!'
print *, greeting

控制结构

条件语句
  • if 语句
program if_example
    implicit none
    integer :: x
    x = 10

    if (x > 0) then
        print *, 'x is positive'
    elseif (x < 0) then
        print *, 'x is negative'
    else
        print *, 'x is zero'
    endif
end program if_example
  • select case 语句
program select_case_example
    implicit none
    integer :: day
    day = 1

    select case (day)
    case (1)
        print *, 'It is Monday'
    case (2)
        print *, 'It is Tuesday'
    case default
        print *, 'It is another day'
    end select
end program select_case_example
循环
  • do 循环
program do_loop_example
    implicit none
    integer :: i

    do i = 1, 5
        print *, i
    end do
end program do_loop_example
  • while 循环(通过 do while 实现)
program while_loop_example
    implicit none
    integer :: i
    i = 1

    do while (i <= 5)
        print *, i
        i = i + 1
    end do
end program while_loop_example

函数和子程序

定义函数
  • 使用 function 关键字定义函数。
module math_module
    contains
    function add(a, b) result(sum)
        implicit none
        integer, intent(in) :: a, b
        integer :: sum
        sum = a + b
    end function add
end module math_module
调用函数
  • 在主程序中调用函数。
program main
    use math_module
    implicit none
    integer :: result
    result = add(3, 4)
    print *, result
end program main
定义子程序
  • 使用 subroutine 关键字定义子程序。
module math_module
    contains
    subroutine print_sum(a, b)
        implicit none
        integer, intent(in) :: a, b
        print *, 'The sum is', a + b
    end subroutine print_sum
end module math_module
调用子程序
  • 在主程序中调用子程序。
program main
    use math_module
    implicit none
    call print_sum(3, 4)
end program main

数组

创建数组
  • 使用 dimension 属性定义数组。
integer, dimension(5) :: numbers
numbers = [1, 2, 3, 4, 5]
数组操作
  • 初始化:使用 =(/ ... /)
  • 访问元素:使用索引。
integer, dimension(5) :: numbers
numbers = (/ 1, 2, 3, 4, 5 /)

! 访问元素
print *, numbers(1)  ! 输出: 1

! 修改元素
numbers(1) = 10
print *, numbers(1)  ! 输出: 10
数组运算
  • 加法+
  • 减法-
  • 乘法*
  • 除法/
integer, dimension(5) :: a, b, c
a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]

c = a + b  ! c = [7, 9, 11, 13, 15]

输入输出

标准输入输出
  • print 语句用于输出。
  • read 语句用于输入。
program io_example
    implicit none
    integer :: number
    real :: value

    print *, 'Enter an integer and a real number:'
    read *, number, value

    print *, 'You entered:', number, value
end program io_example
文件输入输出
  • 使用 open, close, read, write 语句进行文件操作。
program file_io_example
    implicit none
    integer :: unit, i
    real, dimension(5) :: data

    open(newunit=unit, file='data.txt', status='replace')

    data = [1.0, 2.0, 3.0, 4.0, 5.0]
    write(unit, *) data

    close(unit)

    open(newunit=unit, file='data.txt', status='old')
    read(unit, *) data
    close(unit)

    print *, 'Data from file:', data
end program file_io_example

示例项目

为了更好地理解 Fortran 的应用,你可以尝试构建一些简单的项目,例如:

  • 矩阵运算:实现矩阵加法、乘法等操作。
  • 数值积分:使用梯形法则或辛普森法则进行数值积分。
  • 线性方程求解:使用高斯消去法或雅克比迭代法求解线性方程组。
  • 数据拟合:使用最小二乘法进行数据拟合。

教学资源

  • 官方文档Fortran Wiki 提供了详细的文档和示例。
  • 在线课程:Coursera、Udemy 等平台上有许多关于 Fortran 的在线课程。
  • 社区分享:访问 Stack OverflowGitHub 查看其他用户的作品和分享经验。

总结

以上是 Fortran 语言的一个全面的基础教程,涵盖了从基本语法到高级功能的各个方面。通过这些基础知识,你可以开始编写简单的 Fortran 程序,并进一步探索更复杂的功能和创意。如果你希望深入学习,可以参考上述的教学资源,并通过实际项目来练习 Fortran 技能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值