Fortran 派生数据类型

Fortran 让您定义派生数据类型。派生数据类型也称为结构,它可以由不同类型的数据对象组成。

派生数据类型用于表示记录。例如,如果你想跟踪图书馆里的书,你可能想跟踪每本书的以下属性:

  • Title
  • Author
  • Subject
  • Book ID

定义派生数据类型

要定义派生数据类型,请使用类型和结束类型语句。type 语句定义了一个新的数据类型,程序有多个成员。类型语句的格式如下:

  1. type type_name
  2. declarations
  3. end type

以下是您声明 Book 结构的方式:

  1. type Books
  2. character(len = 50) :: title
  3. character(len = 50) :: author
  4. character(len = 150) :: subject
  5. integer :: book_id
  6. end type Books

访问结构成员

派生数据类型的对象称为结构。

可以在类型声明语句中创建 Books 类型的结构,如下所示:

  1. type(Books) :: book1

可以使用组件选择器字符(%)访问结构的组件:

  1. book1%title = "C Programming"
  2. book1%author = "Nuha Ali"
  3. book1%subject = "C Programming Tutorial"
  4. book1%book_id = 6495407
请注意,% 符号前后没有空格。
实例
  1. program deriveDataType
  2. !type declaration
  3. type Books
  4. character(len = 50) :: title
  5. character(len = 50) :: author
  6. character(len = 150) :: subject
  7. integer :: book_id
  8. end type Books
  9. !declaring type variables
  10. type(Books) :: book1
  11. type(Books) :: book2
  12. !accessing the components of the structure
  13. book1%title = "C Programming"
  14. book1%author = "Nuha Ali"
  15. book1%subject = "C Programming Tutorial"
  16. book1%book_id = 6495407
  17. book2%title = "Telecom Billing"
  18. book2%author = "Zara Ali"
  19. book2%subject = "Telecom Billing Tutorial"
  20. book2%book_id = 6495700
  21. !display book info
  22. Print *, book1%title
  23. Print *, book1%author
  24. Print *, book1%subject
  25. Print *, book1%book_id
  26. Print *, book2%title
  27. Print *, book2%author
  28. Print *, book2%subject
  29. Print *, book2%book_id
  30. end program deriveDataType

结果如下:

  1. C Programming
  2. Nuha Ali
  3. C Programming Tutorial
  4. 6495407
  5. Telecom Billing
  6. Zara Ali
  7. Telecom Billing Tutorial
  8. 6495700

结构数组

您还可以创建派生类型的数组:

  1. type(Books), dimension(2) :: list

数组的各个元素可以按以下方式访问:

  1. list(1)%title = "C Programming"
  2. list(1)%author = "Nuha Ali"
  3. list(1)%subject = "C Programming Tutorial"
  4. list(1)%book_id = 6495407
实例
  1. program deriveDataType
  2. !type declaration
  3. type Books
  4. character(len = 50) :: title
  5. character(len = 50) :: author
  6. character(len = 150) :: subject
  7. integer :: book_id
  8. end type Books
  9. !declaring array of books
  10. type(Books), dimension(2) :: list
  11. !accessing the components of the structure
  12. list(1)%title = "C Programming"
  13. list(1)%author = "Nuha Ali"
  14. list(1)%subject = "C Programming Tutorial"
  15. list(1)%book_id = 6495407
  16. list(2)%title = "Telecom Billing"
  17. list(2)%author = "Zara Ali"
  18. list(2)%subject = "Telecom Billing Tutorial"
  19. list(2)%book_id = 6495700
  20. !display book info
  21. Print *, list(1)%title
  22. Print *, list(1)%author
  23. Print *, list(1)%subject
  24. Print *, list(1)%book_id
  25. Print *, list(1)%title
  26. Print *, list(2)%author
  27. Print *, list(2)%subject
  28. Print *, list(2)%book_id
  29. end program deriveDataType

结果如下:

  1. C Programming
  2. Nuha Ali
  3. C Programming Tutorial
  4. 6495407
  5. C Programming
  6. Zara Ali
  7. Telecom Billing Tutorial
  8. 6495700

分类导航