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

  1.                                  -- Chapter 28 - Program 2
  2.    generic
  3.       type ITEM is private;
  4.    procedure Exchange_Data(X,Y : in out ITEM);
  5.  
  6.    procedure Exchange_Data(X,Y : in out ITEM) is
  7.    Temp : ITEM;
  8.    begin
  9.       Temp := X;
  10.       X := Y;
  11.       Y := Temp;
  12.    end Exchange_Data;
  13.  
  14.  
  15.  
  16.    generic
  17.       type ANOTHER_ITEM is range <>;   -- Integer class only
  18.    function Average(X,Y : ANOTHER_ITEM) return ANOTHER_ITEM;
  19.  
  20.    function Average(X,Y : ANOTHER_ITEM) return ANOTHER_ITEM is
  21.       Temporary : ANOTHER_ITEM;
  22.    begin
  23.       Temporary := (X + Y) / 2;
  24.       return Temporary;
  25.    end Average;
  26.  
  27.  
  28.  
  29.  
  30.  
  31. -- This is the beginning of the main program which uses the generic
  32. -- procedure and function defined above.
  33.  
  34. with Exchange_Data, Average;
  35. with Text_IO;
  36. use Text_IO;
  37.  
  38. procedure SwapMore is
  39.  
  40.    package Int_IO is new Text_IO.Integer_IO(INTEGER);
  41.    use Int_IO;
  42.    package Real_IO is new Text_IO.Float_IO(FLOAT);
  43.    use Real_IO;
  44.  
  45.    type NEW_TYPE is new INTEGER range 122..12345;
  46.  
  47.    type MY_RECORD is
  48.       record
  49.          My_Grade : INTEGER range 1..128;
  50.          My_Class : CHARACTER;
  51.       end record;
  52.  
  53.    procedure Swap is new Exchange_Data(INTEGER);
  54.    procedure Swap is new Exchange_Data(MY_RECORD);
  55.    procedure Swap is new Exchange_Data(FLOAT);
  56.    procedure Swap is new Exchange_Data(NEW_TYPE);
  57.    procedure Swap is new Exchange_Data(CHARACTER);
  58.    procedure Trade is new Exchange_Data(CHARACTER);
  59.    procedure Exchange is new Exchange_Data(CHARACTER);
  60.    procedure Puppy is new Exchange_Data(CHARACTER);
  61.  
  62.    function Mean is new Average(INTEGER);
  63.    function Mean is new Average(NEW_TYPE);
  64.    function Swap is new Average(INTEGER);    -- This is dumb to do,
  65.                                              --  but it is legal.
  66.  
  67.    Index1 : INTEGER := 117;
  68.    Index2 : INTEGER := 123;
  69.    Data1  : MY_RECORD := (15,'A');
  70.    Data2  : MY_RECORD := (91,'B');
  71.    Real1  : FLOAT := 3.14;
  72.    Real2  : FLOAT := 77.02;
  73.    Value1 : NEW_TYPE := 222;
  74.    Value2 : NEW_TYPE := 345;
  75.  
  76. begin
  77.  
  78.    Put(Real1,4,2,0);
  79.    Put(Real2,4,2,0);
  80.    New_Line;
  81.    Swap(Real1,Real2);
  82.    Put(Real1,4,2,0);
  83.    Put(Real2,4,2,0);
  84.    New_Line(2);
  85.  
  86.    Put(Index1);
  87.    Put(Index2);
  88.    New_Line;
  89.  
  90.    Swap(Index1,Index2);
  91.    Swap(Data1,Data2);
  92.  
  93.    Put(Index1);
  94.    Put(Index2);
  95.    New_Line;
  96.  
  97.                   -- Now to exercise some of the functions
  98.  
  99.    Index1 := Mean(Index2,16);
  100.    Value1 := Mean(Value2, Value2 + 132);
  101.    Index1 := Swap(Index1, Index2 + 12);  -- This actually gets the
  102.                                          --  mean of the inputs.
  103.  
  104. end SwapMore;
  105.  
  106.  
  107.  
  108.  
  109. -- Result of Execution
  110.  
  111. --    3.14  77.02
  112. --   77.02   3.14
  113. --
  114. --    117   123
  115. --    123   117
  116.  
  117.