Fortran - Types de données dérivés

Fortran vous permet de définir des types de données dérivés. Un type de données dérivé est également appelé une structure, et il peut être constitué d'objets de données de différents types.

Les types de données dérivés sont utilisés pour représenter un enregistrement. Par exemple, vous souhaitez garder une trace de vos livres dans une bibliothèque, vous voudrez peut-être suivre les attributs suivants pour chaque livre -

  • Title
  • Author
  • Subject
  • ID du livre

Définition d'un type de données dérivé

Pour définir une donnée dérivée type, le type et end typedes instructions sont utilisées. . L'instruction de type définit un nouveau type de données, avec plus d'un membre pour votre programme. Le format de l'instruction de type est le suivant -

type type_name      
   declarations
end type

Voici la façon dont vous déclareriez la structure Book -

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

Accès aux membres de la structure

Un objet d'un type de données dérivé est appelé une structure.

Une structure de type Books peut être créée dans une déclaration de type comme -

type(Books) :: book1

Les composants de la structure sont accessibles à l'aide du caractère de sélection de composant (%) -

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

Note that there are no spaces before and after the % symbol.

Exemple

Le programme suivant illustre les concepts ci-dessus -

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

Lorsque le code ci-dessus est compilé et exécuté, il produit le résultat suivant -

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

Tableau de structures

Vous pouvez également créer des tableaux d'un type dérivé -

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

Les éléments individuels du tableau peuvent être accédés comme -

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

Le programme suivant illustre le concept -

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

Lorsque le code ci-dessus est compilé et exécuté, il produit le résultat suivant -

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