Fortran派生數(shù)據(jù)類(lèi)型

2018-12-12 14:24 更新

Fortran語(yǔ)言允許您定義導(dǎo)出的數(shù)據(jù)類(lèi)型。導(dǎo)出的數(shù)據(jù)類(lèi)型也被稱(chēng)為結(jié)構(gòu),它可以由不同類(lèi)型的數(shù)據(jù)對(duì)象。

導(dǎo)出的數(shù)據(jù)類(lèi)型被用來(lái)表示一個(gè)記錄。例如,你想跟蹤您的圖書(shū)館中的書(shū)籍,你可能要跟蹤有關(guān)每本書(shū)以下屬性:

  • 標(biāo)題
  • 作者
  • 學(xué)科
  • 圖書(shū)ID

定義導(dǎo)出的數(shù)據(jù)類(lèi)型

定義一個(gè)導(dǎo)出的數(shù)據(jù)類(lèi)型 ,使用的種類(lèi)和末端型語(yǔ)句。 。該類(lèi)型語(yǔ)句定義了一個(gè)新的數(shù)據(jù)類(lèi)型,為你的程序多個(gè)成員。類(lèi)型語(yǔ)句的格式是這樣的:

type type_name      
   declarations
end type 

這里是你將聲明書(shū)的結(jié)構(gòu)方式:

type Books
   character(len=50) :: title
   character(len=50) :: author
   character(len=150) :: subject
   integer :: book_id
end type Books

訪問(wèn)結(jié)構(gòu)成員

一個(gè)派生數(shù)據(jù)類(lèi)型的對(duì)象被稱(chēng)為結(jié)構(gòu)

類(lèi)型書(shū)籍的結(jié)構(gòu)可以在類(lèi)型聲明語(yǔ)句像創(chuàng)建:

type(Books) :: book1 

該結(jié)構(gòu)的組成部分可以使用組件選擇字符(%)來(lái)訪問(wèn):

book1%title = "C Programming"
book1%author = "Nuha Ali"
book1%subject = "C Programming Tutorial"
book1%book_id = 6495407

請(qǐng)注意,有沒(méi)有空格前后%符號(hào)之后。

下面的程序說(shuō)明上述概念:

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

當(dāng)上述代碼被編譯和執(zhí)行時(shí),它產(chǎn)生了以下結(jié)果:

 C Programming                                     
 Nuha Ali                                          
 C Programming Tutorial            
     6495407
 Telecom Billing                                   
 Zara Ali                                          
 Telecom Billing Tutorial            
     6495700

結(jié)構(gòu)數(shù)組

你也可以創(chuàng)建一個(gè)派生類(lèi)型的數(shù)組:

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

數(shù)組中的單個(gè)元素可以訪問(wèn)如下:

list(1)%title = "C Programming"
list(1)%author = "Nuha Ali"
list(1)%subject = "C Programming Tutorial"
list(1)%book_id = 6495407

下面的程序說(shuō)明了這一概念:

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

當(dāng)上述代碼被編譯和執(zhí)行時(shí),它產(chǎn)生了以下結(jié)果:

C Programming                                     
Nuha Ali                                          
C Programming Tutorial               
   6495407
C Programming                                     
Zara Ali                                          
Telecom Billing Tutorial                                      
   6495700

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)