- 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
廣告