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

  1.                         -- Chapter 29 - Programming exercise 1
  2. generic
  3.    type MY_INT_TYPE is (<>);
  4.    type MY_REAL_TYPE is digits <>;
  5. package EasyPkg is
  6.    procedure Trade_Values (X, Y : in out MY_INT_TYPE);
  7.    function Average_Values (X, Y : MY_REAL_TYPE)
  8.                                        return MY_REAL_TYPE;
  9. end EasyPkg;
  10.  
  11.  
  12. package body EasyPkg is
  13.    procedure Trade_Values (X, Y : in out MY_INT_TYPE) is
  14.    Temp : MY_INT_TYPE;
  15.    begin
  16.       Temp := X;
  17.       X := Y;
  18.       Y := Temp;
  19.    end Trade_Values;
  20.  
  21.    function Average_Values (X, Y : MY_REAL_TYPE)
  22.                                        return MY_REAL_TYPE is
  23.    begin
  24.       return (X + Y) / 2.0;
  25.    end Average_Values;
  26.  
  27. end EasyPkg;
  28.  
  29.  
  30.  
  31. with Text_IO, EasyPkg;
  32. use Text_IO;
  33.  
  34. procedure CH29_1 is
  35.  
  36. type MY_NEW_TYPE is new INTEGER range -12..123;
  37. type MY_NEW_FLOAT is new FLOAT digits 6;
  38. type RESULT is (WIN, LOSE, DRAW, FORFEIT, CANCELLED);
  39.  
  40. package Funny_Stuff is new EasyPkg(MY_NEW_TYPE,MY_NEW_FLOAT);
  41. use Funny_Stuff;
  42. package Usual_Stuff is new EasyPkg(INTEGER,FLOAT);
  43. use Usual_Stuff;
  44. package Ball_Game is new EasyPkg(RESULT, FLOAT);
  45. use Ball_Game;
  46.  
  47. Int1 : INTEGER := 12;
  48. Int2 : INTEGER := 35;
  49. My_Int1 : MY_NEW_TYPE := 1;
  50. My_Int2 : MY_NEW_TYPE := 14;
  51.  
  52. Game1   : RESULT := WIN;
  53. Game2   : RESULT := FORFEIT;
  54.  
  55. Real1 : FLOAT;
  56. My_Real1 : MY_NEW_FLOAT;
  57.  
  58. begin
  59.    Ball_Game.Trade_Values(Game1, Game2);
  60.    Trade_Values(Game1, Game2);
  61.  
  62.    Trade_Values(Int1, Int2);       -- Uses Usual_Stuff.Trade_Values
  63.    Trade_Values(My_Int1, My_Int2); -- Uses Funny_Stuff.Trade_Values
  64.    Usual_Stuff.Trade_Values(Int1, Int2);
  65.    Funny_Stuff.Trade_Values(My_Int1, My_Int2);
  66. -- Usual_Stuff.Trade_Values(My_Int1, My_Int2);   -- Illegal
  67. -- Trade_Values(My_Int1, Int2);                  -- Illegal
  68.  
  69.    Real1 := Usual_Stuff.Average_Values(2.71828, 3.141592);
  70.    Real1 := Usual_Stuff.Average_Values(Real1, 2.0 * 3.141592);
  71.    My_Real1 := Average_Values(12.3, 27.345);
  72.    My_Real1 := Average_Values(My_Real1, 2.0 * 3.141592);
  73.    My_Real1 := Funny_Stuff.Average_Values(12.3, 27.345);
  74. end CH29_1;
  75.  
  76.  
  77.  
  78.  
  79. -- Result of execution
  80.  
  81. -- (There is no output from this program)
  82.  
  83.