home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog2 / answers / ch03_3.ada < prev    next >
Encoding:
Text File  |  1991-07-01  |  2.7 KB  |  114 lines

  1.                           -- Chapter 3 - Programming Exercise 3
  2.                                        -- Chapter 3 - Program 5
  3. with Text_IO;
  4. use Text_IO;
  5.  
  6. procedure Ch03_3 is
  7.  
  8.    package Int_IO is new Text_IO.Integer_IO(INTEGER);
  9.    use Int_IO;
  10.  
  11.    type BUG_RANGE is range -13..34;
  12.  
  13.    package Bug_IO is new Text_IO.Integer_IO(BUG_RANGE);
  14.    use Bug_IO;
  15.  
  16.    Rat : INTEGER;
  17.    Dog : NATURAL;
  18.    Cat : POSITIVE;
  19.    Bug : BUG_RANGE;
  20.  
  21. begin
  22.  
  23.    Rat := 12;
  24.    Dog := 23;
  25.    Cat := 31;
  26.    Bug := -11;
  27.  
  28.    Put("The type INTEGER uses ");
  29.    Int_IO.Put(INTEGER'SIZE);
  30.    Put(" bits of memory,");
  31.    New_Line;
  32.    Put(" and has a range from ");
  33.    Put(INTEGER'FIRST);
  34.    Put(" to ");
  35.    Put(INTEGER'LAST);
  36.    New_Line;
  37.    Put(" Rat has a present value of ");
  38.    Put(Rat);
  39.    New_Line(2);
  40.  
  41.    Put("The type NATURAL uses ");
  42.    Int_IO.Put(NATURAL'SIZE);
  43.    Put(" bits of memory,");
  44.    New_Line;
  45.    Put(" and has a range from ");
  46.    Rat := NATURAL'FIRST;
  47.    Put(Rat);
  48.    Put(" to ");
  49.    Rat := NATURAL'LAST;
  50.    Put(Rat);
  51.    New_Line;
  52.    Put(" Dog has a present value of ");
  53.    Put(Dog);
  54.    New_Line(2);
  55.  
  56.    Put("The type POSITIVE uses ");
  57.    Int_IO.Put(POSITIVE'SIZE);
  58.    Put(" bits of memory,");
  59.    New_Line;
  60.    Put(" and has a range from ");
  61.    Put(POSITIVE'FIRST);
  62.    Put(" to ");
  63.    Put(POSITIVE'LAST);
  64.    New_Line;
  65.    Put(" Cat has a present value of ");
  66.    Put(Cat);
  67.    New_Line(2);
  68.  
  69.    Put("The type BUG_RANGE uses ");
  70.    Bug_IO.Put(BUG_RANGE'SIZE);
  71.    Put(" bits of memory,");
  72.    New_Line;
  73.    Put(" and has a range from ");
  74.    Put(BUG_RANGE'FIRST);
  75.    Put(" to ");
  76.    Put(BUG_RANGE'LAST);
  77.    New_Line;
  78.    Put(" Bug has a present value of ");
  79.    Put(Bug);
  80.    New_Line(2);
  81.  
  82. end Ch03_3;
  83.  
  84.  
  85.  
  86.  
  87. -- Result of execution
  88.  
  89. -- The type INTEGER uses     16 bits of memory
  90. --  and has a range from -32768 to  32767
  91. --  Rat has a present value of     12
  92.  
  93. -- The type NATURAL uses     16 bits of memory
  94. --  and has a range from      0 to  32767
  95. --  Dog has a present value of     23
  96.  
  97. -- The type POSITIVE uses     16 bits of memory
  98. --  and has a range from      1 to  32767
  99. --  Cat has a present value of     31
  100.  
  101. -- The type BUG_RANGE uses     16 bits of memory
  102. --  and has a range from    -13 to     34
  103. --  Bug has a present value of    -11
  104.  
  105.  
  106. --  Note;  The qualitfier must be put in front of lines 29, 42,
  107. --    57, and 70, because there are now two different ways to
  108. --    output a universal_integer.  The system does not know
  109. --    which one to use so it forces you to make the decision.
  110. --    This will be completly clear to you later in this tutorial
  111. --    and even if it caused you a problem now, the frustration
  112. --    was probably worth the knowledge you gained.
  113.  
  114.