Fortran-derived-data-types

提供:Dev Guides
移動先:案内検索

Fortran-派生データ型

Fortranでは、派生データ型を定義できます。 派生データ型は構造体とも呼ばれ、さまざまな型のデータオブジェクトで構成できます。

派生データ型は、レコードを表すために使用されます。 E.g. あなたは図書館であなたの本を追跡したい場合は、各本に関する次の属性を追跡することができます-

  • タイトル
  • 著者
  • 件名
  • ブックID

派生データ型の定義

派生データ type を定義するには、typeおよび end type ステートメントが使用されます。 。 typeステートメントは、プログラムに複数のメンバーを持つ新しいデータ型を定義します。 タイプステートメントの形式はこれです-

type type_name
   declarations
end type

ここにあなたが本の構造を宣言する方法があります-

type Books
   character(len = 50) :: title
   character(len = 50) :: author
   character(len = 150) :: subject
   integer :: book_id
end type 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