位置:首页 > > Fortran导出数据类型

Fortran导出数据类型

Fortran语言可以定义导出的数据类型。导出的数据类型也被称为一个结构,它可以包含不同类型的数据对象。

导出的数据类型被用来代表一个记录。例如要跟踪在图书馆的书,可能希望跟踪的每本书有如下属性:

  • 标题- Title
  • 作者 - Author
  • 科目 - Subject
  • 编号 - Book ID

定义一个导出的数据类型

定义一个派生数据类型,类型和端类型的语句被使用。类型语句定义了一个新的数据类型,项目不止一个成员。类型声明的格式是这样的:

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

访问结构成员

一个派生数据类型的对象被称为结构

类型书籍(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