ADA Data Structures - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

ADA Data Structures

Description:

A subset of the values associated. with the original type. Example : SUBTYPE Natural IS Integer RANGE 0. ... (blanc, noir, rouge); Package Eng_Color_IO is ... – PowerPoint PPT presentation

Number of Views:74
Avg rating:3.0/5.0
Slides: 23
Provided by: mht3
Category:
Tags: ada | data | noir | structures

less

Transcript and Presenter's Notes

Title: ADA Data Structures


1
ADAData Structures
  • Subtype
  • Enumeration type

2
SUBTYPE
  • Definition
  • A subset of the values associated
  • with the original type.
  • Example
  • SUBTYPE Natural IS Integer RANGE 0..IntegerLast
  • SUBTYPE Positive IS Integer RANGE 0..IntegerLast

3
More Examples
  • SUBTYPE SmallInt IS Integer RANGE -50. .50
  • SUBTYPE NonNegFloat IS Float RANGE 0.0 . .
    FloatLast
  • SUBTYPE CapLetter IS Character RANGE A.
    .Z
  • X, Y, Z SmallInt
  • NextChar CapLetter
  • Hours_Worked NonNegFloat

4
Constraint Error
  • SUBTYPE SmallInt IS Integer RANGE -50. .50
  • X, Y, Z SmallInt
  • X 26
  • Y 28
  • Z X Y
  • Compilation Error ?
  • At Run Time, Error will be raised. Why ?

5
Compatibility Rules for Types and Subtypes
  • Do not allow to mix the types of operands
  • Examples
  • v1 Integer
  • V2 Float
  • V1V2 leads to compilation error

6
More Compatibility
  • Example
  • SUBTYPE SmallInt IS Integer RANGE -50. .50
  • V1 Integer
  • V2 SmallInt
  • V1 V2 is Compatible.

7
More Compatibility
  • Two values are compatible if they have
  • the same type name
  • or
  • one values type is a subtype of the other
    values type.

8
Interesting Things
  • X Integer
  • Y Natural
  • Think about
  • X Y
  • and
  • Y X

9
No Compilation Error
  • X Y is always valid.
  • But
  • Y X is not always valid at execution time.
  • When ?

10
Ada Standard Library
  • With Ada.Numerics
  • Example
  • get the value of Pi as
  • Ada.Numerics.Pi
  • do not need to declare Pi as constant float ...

11
Enumeration Type
  • Defined by a list of values taking the form of
    identifiers.
  • Useful in representing a fixed set of values that
    are not numerical such as days of the week, class
    year (freshman, sophomore, junior, senior) ,
    etc...

12
Defining Enumeration Type
  • Form
  • TYPE enumeration-type ( identifier-list)

13
Examples
  • TYPE Days IS
  • (Monday, Tuesday, Wednesday, Thursday, Friday,
    Saturday, Sunday)
  • Declaration
  • Some_Day Days
  • Today Days
  • Valentine_Day Days

14
More Examples
  • Assignment Statements
  • Today Friday
  • Some_Day Monday

15
Type Attributes and Operations
  • Six important Attributes
  • First
  • Last
  • Pos - - position for a given value
  • Val - - value for a given position
  • Pred - - predecessor of a given value
  • Succ - - successor of a given value

16
Attribute Query
  • Typeattribute-name
  • Typeattribute-name(value)
  • Example
  • DaysFirst
  • DaysSucc(Friday)
  • DaysPos(Sunday)

17
Input/Output for Enumeration Types
  • Within Ada.Text_IO,
  • a generic package called Enumeration_IO
    must be used.
  • But it can not be used immediately.
  • Instances must be created

18
Format
  • PACKAGE instance IS
  • NEW Ada.Text_IO.Enumeration_IO (Enum gt type)

19
Get Examples
  • PACKAGE Day_IO IS NEW Ada.Text_IO.Enumeration (
    Enum gt Dats )
  • Day_IO.Get ( Item gt Some_day)
  • Day_IO.Get ( Item gt Valentine_Day)

20
Put Examples
  • Day_IO.Put
  • ( Item gt Some_Day, width gt 5, Set gt
    Lower_case)

21
Example Program with Enumeration Type
  • with Ada.Text_IO
  • Procedure Colors Is
  • Type English_Colors Is
  • (white, black, red)
  • Type French_Colors is
  • (blanc, noir, rouge)
  • Package Eng_Color_IO is
  • New Ada.Text_IO.Enumeration_IO (Enum gt
    English_Colors)
  • Package Fr_Color_IO is
  • New Ada.Text_IO.Enumeration_IO (Enum gt
    French_Colors)

22
  • Eng_Color English_Colors
  • Fr_Color French_Colors
  • position Natural
  • Begin --
  • Ada.Text_IO.Put (Item gt" The first English
    color is ")
  • Eng_Color_IO.Put(Item gt English_Colors'First)
  • Ada.Text_IO.New_Line
  • Ada.Text_IO.Put (Item gt " Enter an English
    color ")
  • Eng_Color_IO.Get(Item gt Eng_Color)
  • position English_Colors'Pos(Eng_Color)
  • Fr_Color French_Colors'Val(position)
  • Ada.Text_IO.Put (Item gt " The equavilent
    French Color is ")
  • Fr_Color_IO.Put (Item gt Fr_Color, Set
    gtAda.Text_IO.Lower_Case)
  • Ada.Text_IO.New_Line
  • end Colors
Write a Comment
User Comments (0)
About PowerShow.com