- Fortran 教程
- Fortran - 首页
- Fortran - 概述
- Fortran - 环境设置
- Fortran - 基本语法
- Fortran - 数据类型
- Fortran - 变量
- Fortran - 常量
- Fortran - 运算符
- Fortran - 决策
- Fortran - 循环
- Fortran - 数字
- Fortran - 字符
- Fortran - 字符串
- Fortran - 数组
- Fortran - 动态数组
- Fortran - 派生数据类型
- Fortran - 指针
- Fortran - 基本输入输出
- Fortran - 文件输入输出
- Fortran - 过程
- Fortran - 模块
- Fortran - 本质函数
- Fortran - 数值精度
- Fortran - 程序库
- Fortran - 编程风格
- Fortran - 调试程序
- Fortran 资源
- Fortran - 快速指南
- Fortran - 有用资源
- Fortran - 讨论
Fortran - 派生数据类型
Fortran 允许您定义派生数据类型。派生数据类型也称为结构,它可以包含不同类型的数据对象。
派生数据类型用于表示记录。例如,您想跟踪图书馆中的书籍,您可能希望跟踪以下关于每本书的属性:
- 标题
- 作者
- 主题
- 图书 ID
定义派生数据类型
要定义派生数据类型,使用 type 和end type语句。type 语句定义了一种新的数据类型,为您的程序提供了多个成员。type 语句的格式如下:
type type_name declarations end type
以下是您声明 Book 结构的方式:
type Books character(len = 50) :: title character(len = 50) :: author character(len = 150) :: subject integer :: book_id end type Books
访问结构成员
派生数据类型的对象称为结构。
可以在类型声明语句中创建 Books 类型的结构,例如:
type(Books) :: book1
可以使用组件选择符字符(%)访问结构的组件:
book1%title = "C Programming" book1%author = "Nuha Ali" book1%subject = "C Programming Tutorial" book1%book_id = 6495407
请注意,% 符号前后没有空格。
示例
以下程序说明了上述概念:
program deriveDataType !type declaration type Books character(len = 50) :: title character(len = 50) :: author character(len = 150) :: subject integer :: book_id end type Books !declaring type variables type(Books) :: book1 type(Books) :: book2 !accessing the components of the structure book1%title = "C Programming" book1%author = "Nuha Ali" book1%subject = "C Programming Tutorial" book1%book_id = 6495407 book2%title = "Telecom Billing" book2%author = "Zara Ali" book2%subject = "Telecom Billing Tutorial" book2%book_id = 6495700 !display book info Print *, book1%title Print *, book1%author Print *, book1%subject Print *, book1%book_id Print *, book2%title Print *, book2%author Print *, book2%subject Print *, book2%book_id end program deriveDataType
当编译并执行上述代码时,会产生以下结果:
C Programming Nuha Ali C Programming Tutorial 6495407 Telecom Billing Zara Ali Telecom Billing Tutorial 6495700
结构数组
您还可以创建派生类型的数组:
type(Books), dimension(2) :: list
可以按如下方式访问数组的各个元素:
list(1)%title = "C Programming" list(1)%author = "Nuha Ali" list(1)%subject = "C Programming Tutorial" list(1)%book_id = 6495407
以下程序说明了该概念:
program deriveDataType !type declaration type Books character(len = 50) :: title character(len = 50) :: author character(len = 150) :: subject integer :: book_id end type Books !declaring array of books type(Books), dimension(2) :: list !accessing the components of the structure list(1)%title = "C Programming" list(1)%author = "Nuha Ali" list(1)%subject = "C Programming Tutorial" list(1)%book_id = 6495407 list(2)%title = "Telecom Billing" list(2)%author = "Zara Ali" list(2)%subject = "Telecom Billing Tutorial" list(2)%book_id = 6495700 !display book info Print *, list(1)%title Print *, list(1)%author Print *, list(1)%subject Print *, list(1)%book_id Print *, list(1)%title Print *, list(2)%author Print *, list(2)%subject Print *, list(2)%book_id end program deriveDataType
当编译并执行上述代码时,会产生以下结果:
C Programming Nuha Ali C Programming Tutorial 6495407 C Programming Zara Ali Telecom Billing Tutorial 6495700
广告