home *** CD-ROM | disk | FTP | other *** search
- -- Chapter 28 - Program 2
- generic
- type ITEM is private;
- procedure Exchange_Data(X,Y : in out ITEM);
-
- procedure Exchange_Data(X,Y : in out ITEM) is
- Temp : ITEM;
- begin
- Temp := X;
- X := Y;
- Y := Temp;
- end Exchange_Data;
-
-
-
- generic
- type ANOTHER_ITEM is range <>; -- Integer class only
- function Average(X,Y : ANOTHER_ITEM) return ANOTHER_ITEM;
-
- function Average(X,Y : ANOTHER_ITEM) return ANOTHER_ITEM is
- Temporary : ANOTHER_ITEM;
- begin
- Temporary := (X + Y) / 2;
- return Temporary;
- end Average;
-
-
-
-
-
- -- This is the beginning of the main program which uses the generic
- -- procedure and function defined above.
-
- with Exchange_Data, Average;
- with Text_IO;
- use Text_IO;
-
- procedure SwapMore is
-
- package Int_IO is new Text_IO.Integer_IO(INTEGER);
- use Int_IO;
- package Real_IO is new Text_IO.Float_IO(FLOAT);
- use Real_IO;
-
- type NEW_TYPE is new INTEGER range 122..12345;
-
- type MY_RECORD is
- record
- My_Grade : INTEGER range 1..128;
- My_Class : CHARACTER;
- end record;
-
- procedure Swap is new Exchange_Data(INTEGER);
- procedure Swap is new Exchange_Data(MY_RECORD);
- procedure Swap is new Exchange_Data(FLOAT);
- procedure Swap is new Exchange_Data(NEW_TYPE);
- procedure Swap is new Exchange_Data(CHARACTER);
- procedure Trade is new Exchange_Data(CHARACTER);
- procedure Exchange is new Exchange_Data(CHARACTER);
- procedure Puppy is new Exchange_Data(CHARACTER);
-
- function Mean is new Average(INTEGER);
- function Mean is new Average(NEW_TYPE);
- function Swap is new Average(INTEGER); -- This is dumb to do,
- -- but it is legal.
-
- Index1 : INTEGER := 117;
- Index2 : INTEGER := 123;
- Data1 : MY_RECORD := (15,'A');
- Data2 : MY_RECORD := (91,'B');
- Real1 : FLOAT := 3.14;
- Real2 : FLOAT := 77.02;
- Value1 : NEW_TYPE := 222;
- Value2 : NEW_TYPE := 345;
-
- begin
-
- Put(Real1,4,2,0);
- Put(Real2,4,2,0);
- New_Line;
- Swap(Real1,Real2);
- Put(Real1,4,2,0);
- Put(Real2,4,2,0);
- New_Line(2);
-
- Put(Index1);
- Put(Index2);
- New_Line;
-
- Swap(Index1,Index2);
- Swap(Data1,Data2);
-
- Put(Index1);
- Put(Index2);
- New_Line;
-
- -- Now to exercise some of the functions
-
- Index1 := Mean(Index2,16);
- Value1 := Mean(Value2, Value2 + 132);
- Index1 := Swap(Index1, Index2 + 12); -- This actually gets the
- -- mean of the inputs.
-
- end SwapMore;
-
-
-
-
- -- Result of Execution
-
- -- 3.14 77.02
- -- 77.02 3.14
- --
- -- 117 123
- -- 123 117
-
-