Post on 14-Dec-2015
Programmer’s Guide to F
Structures and Derived Datatypes
Structures and Derived Datatypes
Arrays allow data to be grouped, but only if all items have the same data type.
It is often useful to use a structure, which is a compound object consisting of values that may be of different data types.
Structures and Derived Datatypes
Derived types are used to define the form of the structures.
6.1 Structures
A structure is a collection of values, not necessarily of the same type.
The objects that make up a structure are called its components.
6.1 Structures
A good example of the use of a structure might be provided by a simple list, consisting of line numbers and statements on each line.
1 statement12 statement23 statement3: :: :
6.1 Structures
This list can be represented as 2 arrays, one to hold line numbers and one to hold the text of each line.
6.1 Structures
Perhaps, a better way to do this is to have a single object called line consisting of two components, an integer line_number and a character string statement.The entire list would then be an array of these structures, one for each line.
6.1 Structures
A slightly more complicated example: Suppose we wish to store in our
computer the contents of our little black book that contains names, addresses, phone numbers, and some remarks about each person.
6.1 Structures
The name of the structure is person.It has 4 components: name, address, phone, and remarks.
person
name
address
phone
remarks
6.1 Structures
Sometimes the components might be broken down into lower-level components.For instance, the address itself could be a structure with components number, street, city, state, and postal zip_code.
6.2 Derived Types
As we discussed before, there are 5 intrinsic F data types: integer real complex logical character
6.2 Derived Types
A programmer may define a new data type, called a derived type.
6.2 Derived Types
A type definition begins with the keyword type, followed by either private or public accessibility attribute, followed by two colons (::) and the name of the type being defined.
6.2 Derived Types
The components of the type are given in the ordinary type declarations.The type definition ends with the keywords end type, followed by the name of the type being defined.
All type definitions must be put in a module.
6.2 Derived Types
Let’s start with our first example, a list containing line numbers and statements.type, public :: line
integer :: line_numbercharacter (len = line_length) :: text
end type line where line_length is an integer parameter
(named constant)
6.2 Derived Types
Let’s return to the example of the little black book.To define the type phone_type in that example, area_code and number are each declared to be integer components.
phone_type
area_code
number
6.2 Derived Types
type, public :: phone_typeinteger :: area_code,
numberend type phone_type
phone
area_code
number
6.2 Derived Types
The definition od the type address_type is a little more complicated because some of the components are character strings and some are integers.
6.2 Derived Types
type, public :: address_typeinteger :: numbercharacter (len=30) :: street, citycharacter (len=2) :: stateinteger :: zip_code
end type address_type
address
number
street
city
state
zip_code
6.2 Derived Types
Now that, the types address_type and phone_type are defined, it is possible to define a type suitable for one entry in the black book.Note that the names address_type and phone_type were used for the names of the types, so that the names address and phone could be used for the components of the type person_type.
6.2 Derived Types
type, public :: person_typecharacter (len=40) :: nametype (address_type) :: addresstype (phone_type) :: phonecharacter (len=100) :: remarks
end type person_type
6.2 Derived Types
type, public :: person character(len=12) :: first_name character(len=1) :: middle_initial character(len=12) :: last_name integer :: age
character(len=1) :: sex ! M or Fcharacter(len=5) :: tax_number
end type person
6.3 Declaring and Using Structures
Given the type definition for line that can be used to hold a line number and one statement, a variable new_line that could be used to represent one line of the list that can be declared bytype (line) :: new_line
6.3 Declaring and Using Structures
The entire list could be represented by a single variable declared to be an array of values of type line:
type (line), dimension (max_lines) :: list
6.3 Declaring and Using Structures
For example, to print a line of the list: If two arrays were usedprint “(i5, tr1, a)”, line_number(n),
text(n)
With this declarationprint “(i5, tr1, a)”, list(n)
6.3 Declaring and Using Structures
To use the type declarations for the address book, joan can be declared to be type person_type with the statementtype (person_type) : joan
and the little black book can be declared to be an array of type person_type:
type (person_type), dimension (1000) :: black_book
6.3 Declaring and Using Structures
Any program or module that is to contain a derived type declaration must use the module containing the derived type definition (or be in the module).
6.3.1 Referencing Structure Components
A component of a structure is referenced by writing the name of the structure by a percent sign (%) and then the name of the component
6.3.1 Referencing Structure Components
Suppose joan is an f variable declared to be type person_type. Then Joan’s address is referenced by the expressionjoan % address
The object joan%address is itself a structure. If it is desired to refer to one of the components of this structure, another percent symbol is used.joan % address % state
andjoan % phone % area_code
6.3.2 Structure Constructors
Each derived-type declaration creates a structure constructor, whose name is the same as that of the derived type. For example, if you define a type
named boa, you have a boa constructor.
6.3.2 Structure Constructors
The arguments are values to be placed in the individual components of the structure. For example, using the type
phone_type, an area code and telephone number may be assigned with the statement
joan % phone = phone_type (505, 2750800)
6.3.2 Structure Constructors
It is not necessary that the function arguments be constants. If joan%address has been given a value, the variable joan of type person_type can be assigned a value with the statementjoan = person_type (“Joan Doe”, john
%address, &phone_type (505, fax_number – 1), &“Same address as husband John”)
type, public :: employee type(person) :: employee character(len=20) department real :: salaryend type employee
type(person) :: saadetsaadet%employee%age = 34
Now you can declare variable of type person:
type ( type _name ) : : list_of_identifiers
type(person) :: ali, mukaddes, veli
Putting data in a derived type variable
ali = person("Ali","M","Aktepe",56,"M","45645")
mukaddes =person("Mukaddes"," ","Has",18,"F","12345")
veli = person("Veli","M","Karatepe",65,"M","34567")
This is a structure constructor
module kompleks type, public :: kom real :: gercek, sanal end type komend module kompleksprogram ornek use kompleks type(kom) :: s1, s2, top, fark, carpim print *, "Birinci kompleks sayiyi yaziniz." read *, s1 print *, "ikinci kompleks sayiyi yaziniz." read *, s2 !iki kompleks sayinin toplami. top%gercek=s1%gercek + s2%gercek top%sanal =s1%sanal + s2%sanal fark%gercek=s1%gercek- s2%gercek fark%sanal =s1%sanal - s2%sanal carpim%gercek= s1%gercek*s2%gercek - s1%sanal*s2%sanal carpim%sanal = s1%gercek*s2%sanal + s1%sanal*s2%gercek print *,"s1+s2=", top print *,"s1-s2=", fark print *,"s1*s2=", carpimend program ornek
program e_8_3 use m1 type(nokta) :: m,p real :: yaricap, a,b,c,d,e print *, "Cemberin merkezinin koordinatlarini ",& "girin (x,y)." read *, m print *, "Cember uzerindeki bir noktanin ", & "koordinatlarini girin (x,y)." read *, p print *, " " print *, "Cemberin yaricapi", rds(p,m),"dir." print *, " " a = 1.0 b = 1.0 c = -2.0*m%x d = -2*m%y e = (m%x)**2 + (m%y)**2 - (rds(p,m))**2 print *, "Cember denkleminin," print *, "ax2+by2+cx+dy+e=0 formundaki katsayilari:" print *, "a=",a print *, "b=",b print *, "c=",c print *, "d=",d print *, "e=",eend program e_8_3
module m1
public :: rds
type, public :: nokta
real :: x, y
end type nokta
contains
function rds(p,m) result(r)
type(nokta), intent(in) :: p, m
real :: r
real :: g1, g2 ! Ara islemlerde kullanilan
! yerel/gecici degiskenler.
g1 = (p%x - m%x)**2
g2 = (p%y - m%y)**2
r = sqrt(g1 + g2)
end function rds
end module m1
program e_8_4
use m1
type(aile), dimension(4) :: diaz
integer :: i
do i=1,4
print *, "Ad, soyad, cinsiyet(E,K), yas, meslek",&
" bilgilerini giriniz."
print *, "Adres bilgilerinizi girin.(genel, semt, il, ulke)"
read *, diaz(i)
print *, "Adiniz ",trim(adjustl(diaz(i)%kssl%ad))//" "// &
trim(adjustl(diaz(i)%kssl%sad))//"."
print *, diaz(i)%kssl%yas,"yasindasiniz cinsiyetniz ",diaz(i)%kssl%cins, &
" ve mesleginiz ",diaz(i)%kssl%meslek,"."
end do
print *, "Adresiniz:"
print *, trim(adjustl(diaz(i)%addr%genel))
print *, diaz(i)%addr%semt
print *, diaz(i)%addr%il
print *, diaz(i)%addr%ulke
end program e_8_4
module m1
type, public ::kibil
character (len=20) :: ad, sad
character (len=1) :: cins ! E,K
integer :: yas
character (len=20) :: meslek
end type kibil
type, public :: adres
character (len=100):: genel
character (len=20) :: semt, il, ulke
end type adres
type, public :: aile
type(kibil) :: kssl
type(adres) :: addr
end type aile
end module m1