Programmer - PowerPoint PPT Presentation

About This Presentation
Title:

Programmer

Description:

Programmers Guide to F – PowerPoint PPT presentation

Number of Views:57
Avg rating:3.0/5.0
Slides: 39
Provided by: esen5
Category:
Tags: fark | programmer

less

Transcript and Presenter's Notes

Title: Programmer


1
Programmers Guide to F
  • Structures and Derived Datatypes

2
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.

3
Structures and Derived Datatypes
  • Derived types are used to define the form of the
    structures.

4
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.

5
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.
  • statement1
  • statement2
  • statement3

6
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.

7
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.

8
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.

9
6.1 Structures
name
  • The name of the structure is person.
  • It has 4 components name, address, phone, and
    remarks.

address
person
phone
remarks
10
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.

11
6.2 Derived Types
  • As we discussed before, there are 5 intrinsic F
    data types
  • integer
  • real
  • complex
  • logical
  • character

12
6.2 Derived Types
  • A programmer may define a new data type, called a
    derived type.

13
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.

14
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.

15
6.2 Derived Types
  • Lets start with our first example, a list
    containing line numbers and statements.
  • type, public line
  • integer line_number
  • character (len line_length) text
  • end type line
  • where line_length is an integer parameter (named
    constant)

16
6.2 Derived Types
  • Lets 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.

area_ code
phone_ type
number
17
6.2 Derived Types
  • type, public phone_type
  • integer area_code, number
  • end type phone_type

area_ code
phone
number
18
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.

19
6.2 Derived Types
number
street
  • type, public address_type
  • integer number
  • character (len30) street, city
  • character (len2) state
  • integer zip_code
  • end type address_type

city
address
state
zip_code
20
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.

21
6.2 Derived Types
  • type, public person_type
  • character (len40) name
  • type (address_type) address
  • type (phone_type) phone
  • character (len100) remarks
  • end type person_type

22
6.2 Derived Types
  • type, public person
  • character(len12) first_name
  • character(len1) middle_initial
  • character(len12) last_name
  • integer age
  • character(len1) sex ! M or F
  • character(len5) tax_number
  • end type person

23
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 by
  • type (line) new_line

24
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

25
6.3 Declaring and Using Structures
  • For example, to print a line of the list
  • If two arrays were used
  • print (i5, tr1, a), line_number(n), text(n)
  • With this declaration
  • print (i5, tr1, a), list(n)

26
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 statement
  • type (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

27
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).

28
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

29
6.3.1 Referencing Structure Components
  • Suppose joan is an f variable declared to be type
    person_type. Then Joans address is referenced by
    the expression
  • joan address
  • The object joanaddress 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
  • and
  • joan phone area_code

30
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.

31
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)

32
6.3.2 Structure Constructors
  • It is not necessary that the function arguments
    be constants. If joanaddress has been given a
    value, the variable joan of type person_type can
    be assigned a value with the statement
  • joan person_type (Joan Doe, johnaddress,
  • phone_type (505, fax_number 1),
  • Same address as husband John)

33
  • type, public employee type(person)
    employee character(len20) department real
    salaryend type employee
  • type(person) saadet
  • saadetemployeeage 34

34
  • Now you can declare variable of type person
  • type ( type_name ) list_of_identifiers
  • type(person) ali, mukaddes, veli

35
Putting data in a derived type variable
  • ali person("Ali","M","Aktepe",56,"M","45645")
  • mukaddes person("Mukaddes"," ","Has",18,"F","1234
    5")
  • veli person("Veli","M","Karatepe",65,"M","34567"
    )
  • This is a structure constructor

36
module kompleks type, public kom real
gercek, sanal end type kom end module
kompleks program 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.
topgerceks1gercek s2gercek topsanal
s1sanal s2sanal farkgerceks1gercek-
s2gercek farksanal s1sanal - s2sanal
carpimgercek s1gerceks2gercek -
s1sanals2sanal carpimsanal
s1gerceks2sanal s1sanals2gercek
print ,"s1s2", top print ,"s1-s2", fark
print ,"s1s2", carpim end program ornek
37
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.0mx d -2my e (mx)2
(my)2 - (rds(p,m))2 print , "Cember
denkleminin," print , "ax2by2cxdye0
formundaki katsayilari" print , "a",a
print , "b",b print , "c",c print ,
"d",d print , "e",e end 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 (px - mx)2
g2 (py - my)2 r sqrt(g1 g2) end
function rds end module m1
38
program e_8_4 use m1 type(aile), dimension(4)
diaz integer i do i1,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)ksslad))//" "//
trim(adjustl(diaz(i)ksslsad))//"."
print , diaz(i)ksslyas,"yasindasiniz
cinsiyetniz ",diaz(i)ksslcins, "
ve mesleginiz ",diaz(i)ksslmeslek,"." end do
print , "Adresiniz" print ,
trim(adjustl(diaz(i)addrgenel)) print ,
diaz(i)addrsemt print , diaz(i)addril
print , diaz(i)addrulke end program e_8_4
module m1 type, public kibil character
(len20) ad, sad character (len1) cins
! E,K integer yas character
(len20) meslek end type kibil type,
public adres character (len100) genel
character (len20) semt, il, ulke end type
adres type, public aile type(kibil)
kssl type(adres) addr end type aile end
module m1
Write a Comment
User Comments (0)
About PowerShow.com