home *** CD-ROM | disk | FTP | other *** search
- -- Chapter 21 - Programming exercise 2
-
- -- This package uses a data structure composed of three INTEGER
- -- variables. It allow the user to add two structures, component
- -- by component, or subtract component by component. Provision is
- -- also made to build a structure from three numbers, or decompose
- -- a structure into its components.
-
- package Three is
- type DATA_STRUCTURE is private;
- function "+"(Data1, Data2 : DATA_STRUCTURE) return DATA_STRUCTURE;
- function "-"(Data1, Data2 : DATA_STRUCTURE) return DATA_STRUCTURE;
- function Build_Structure(Val1, Val2, Val3 : INTEGER) return
- DATA_STRUCTURE;
- procedure Decompose(Data1 : DATA_STRUCTURE;
- Val1, Val2, Val3 : out INTEGER);
- function Compare_Sum(Data1, Data2 : DATA_STRUCTURE) return BOOLEAN;
-
- private
- type DATA_STRUCTURE is array(1..3) of INTEGER;
- end Three;
-
-
-
- package body Three is
-
- function "+"(Data1, Data2 : DATA_STRUCTURE) return DATA_STRUCTURE is
- Temp : DATA_STRUCTURE;
- begin
- Temp(1) := Data1(1) + Data2(1);
- Temp(2) := Data1(2) + Data2(2);
- Temp(3) := Data1(3) + Data2(3);
- return Temp;
- end "+";
-
-
- function "-"(Data1, Data2 : DATA_STRUCTURE) return DATA_STRUCTURE is
- Temp : DATA_STRUCTURE;
- begin
- Temp(1) := Data1(1) - Data2(1);
- Temp(2) := Data1(2) - Data2(2);
- Temp(3) := Data1(3) - Data2(3);
- return Temp;
- end "-";
-
-
- function Build_Structure(Val1, Val2, Val3 : INTEGER) return
- DATA_STRUCTURE is
- Temp : DATA_STRUCTURE;
- begin
- Temp(1) := Val1;
- Temp(2) := Val2;
- Temp(3) := Val3;
- return Temp;
- end Build_Structure;
-
-
- procedure Decompose(Data1 : DATA_STRUCTURE;
- Val1, Val2, Val3 : out INTEGER) is
- begin
- Val1 := Data1(1);
- Val2 := Data1(2);
- Val3 := Data1(3);
- end Decompose;
-
-
- function Compare_Sum(Data1, Data2 : DATA_STRUCTURE)
- return BOOLEAN is
- begin
- return (Data1(1) + Data1(2) + Data1(3)) =
- (Data2(1) + Data2(2) + Data2(3));
- end Compare_Sum;
-
- end Three;
-
-
-
-
- -- This program exercises the package Three as an illustration.
-
- with Text_IO; use Text_IO;
- with Three; use Three;
-
- procedure CH21_2 is
-
- My_Data, Extra_Data : DATA_STRUCTURE;
- Temp : DATA_STRUCTURE;
-
- begin
-
- My_Data := Build_Structure(3,7,13);
- Extra_Data := Build_Structure(-4,77,0);
- My_Data := My_Data + Extra_Data;
-
- if My_Data /= Extra_Data then
- Put_Line("The two structures are not equal.");
- end if;
-
- My_Data := Extra_Data;
-
- if My_Data = Extra_Data then
- Put_Line("The two structures are equal now.");
- end if;
-
- -- The following line is illegal with the private type.
- -- My_Data.Value1 := My_Data.Value1 + 13;
-
- Temp := Build_Structure(13,0,0);
- My_Data := My_Data + Temp;
-
- if not Compare_Sum(My_Data, Temp) then
- Put_Line("My_Data and Temp are not equal.");
- end if;
-
- My_Data := Build_Structure(1,9,3);
- if Compare_Sum(My_Data, Temp) then
- Put_Line("My_Data and Temp are equal.");
- end if;
-
- end CH21_2;
-
-
-
-
- -- Result of execution
-
- -- The two structures are not equal.
- -- The two structures are equal now.
- -- My_Data and Temp are not equal.
- -- My_Data and Temp are equal.
-
-