home *** CD-ROM | disk | FTP | other *** search
- -- Chapter 29 - Programming exercise 1
- generic
- type MY_INT_TYPE is (<>);
- type MY_REAL_TYPE is digits <>;
- package EasyPkg is
- procedure Trade_Values (X, Y : in out MY_INT_TYPE);
- function Average_Values (X, Y : MY_REAL_TYPE)
- return MY_REAL_TYPE;
- end EasyPkg;
-
-
- package body EasyPkg is
- procedure Trade_Values (X, Y : in out MY_INT_TYPE) is
- Temp : MY_INT_TYPE;
- begin
- Temp := X;
- X := Y;
- Y := Temp;
- end Trade_Values;
-
- function Average_Values (X, Y : MY_REAL_TYPE)
- return MY_REAL_TYPE is
- begin
- return (X + Y) / 2.0;
- end Average_Values;
-
- end EasyPkg;
-
-
-
- with Text_IO, EasyPkg;
- use Text_IO;
-
- procedure CH29_1 is
-
- type MY_NEW_TYPE is new INTEGER range -12..123;
- type MY_NEW_FLOAT is new FLOAT digits 6;
- type RESULT is (WIN, LOSE, DRAW, FORFEIT, CANCELLED);
-
- package Funny_Stuff is new EasyPkg(MY_NEW_TYPE,MY_NEW_FLOAT);
- use Funny_Stuff;
- package Usual_Stuff is new EasyPkg(INTEGER,FLOAT);
- use Usual_Stuff;
- package Ball_Game is new EasyPkg(RESULT, FLOAT);
- use Ball_Game;
-
- Int1 : INTEGER := 12;
- Int2 : INTEGER := 35;
- My_Int1 : MY_NEW_TYPE := 1;
- My_Int2 : MY_NEW_TYPE := 14;
-
- Game1 : RESULT := WIN;
- Game2 : RESULT := FORFEIT;
-
- Real1 : FLOAT;
- My_Real1 : MY_NEW_FLOAT;
-
- begin
- Ball_Game.Trade_Values(Game1, Game2);
- Trade_Values(Game1, Game2);
-
- Trade_Values(Int1, Int2); -- Uses Usual_Stuff.Trade_Values
- Trade_Values(My_Int1, My_Int2); -- Uses Funny_Stuff.Trade_Values
- Usual_Stuff.Trade_Values(Int1, Int2);
- Funny_Stuff.Trade_Values(My_Int1, My_Int2);
- -- Usual_Stuff.Trade_Values(My_Int1, My_Int2); -- Illegal
- -- Trade_Values(My_Int1, Int2); -- Illegal
-
- Real1 := Usual_Stuff.Average_Values(2.71828, 3.141592);
- Real1 := Usual_Stuff.Average_Values(Real1, 2.0 * 3.141592);
- My_Real1 := Average_Values(12.3, 27.345);
- My_Real1 := Average_Values(My_Real1, 2.0 * 3.141592);
- My_Real1 := Funny_Stuff.Average_Values(12.3, 27.345);
- end CH29_1;
-
-
-
-
- -- Result of execution
-
- -- (There is no output from this program)
-
-