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

  1.                        -- Chapter 21 - Programming exercise 1
  2.  
  3. -- This package uses a data structure composed of three INTEGER
  4. -- variables.  It allow the user to add two structures, component
  5. -- by component, or subtract component by component.  Provision is
  6. -- also made to build a structure from three numbers, or decompose
  7. -- a structure into its components.
  8.  
  9. package Three is
  10. type DATA_STRUCTURE is private;
  11. function "+"(Data1, Data2 : DATA_STRUCTURE) return DATA_STRUCTURE;
  12. function "-"(Data1, Data2 : DATA_STRUCTURE) return DATA_STRUCTURE;
  13. function Build_Structure(Val1, Val2, Val3 : INTEGER) return
  14.                                                    DATA_STRUCTURE;
  15. procedure Decompose(Data1 : DATA_STRUCTURE;
  16.                     Val1, Val2, Val3 : out INTEGER);
  17. function Compare_Sum(Data1, Data2 : DATA_STRUCTURE) return BOOLEAN;
  18.  
  19. private
  20.    type DATA_STRUCTURE is
  21.       record
  22.          Value1 : INTEGER;
  23.          Value2 : INTEGER;
  24.          Value3 : INTEGER;
  25.       end record;
  26. end Three;
  27.  
  28.  
  29.  
  30. package body Three is
  31.  
  32. function "+"(Data1, Data2 : DATA_STRUCTURE) return DATA_STRUCTURE is
  33. Temp : DATA_STRUCTURE;
  34. begin
  35.    Temp.Value1 := Data1.Value1 + Data2.Value1;
  36.    Temp.Value2 := Data1.Value2 + Data2.Value2;
  37.    Temp.Value3 := Data1.Value3 + Data2.Value3;
  38.    return Temp;
  39. end "+";
  40.  
  41.  
  42. function "-"(Data1, Data2 : DATA_STRUCTURE) return DATA_STRUCTURE is
  43. Temp : DATA_STRUCTURE;
  44. begin
  45.    Temp.Value1 := Data1.Value1 - Data2.Value1;
  46.    Temp.Value2 := Data1.Value2 - Data2.Value2;
  47.    Temp.Value3 := Data1.Value3 - Data2.Value3;
  48.    return Temp;
  49. end "-";
  50.  
  51.  
  52. function Build_Structure(Val1, Val2, Val3 : INTEGER) return
  53.                                                    DATA_STRUCTURE is
  54. Temp : DATA_STRUCTURE;
  55. begin
  56.    Temp.Value1 := Val1;
  57.    Temp.Value2 := Val2;
  58.    Temp.Value3 := Val3;
  59.    return Temp;
  60. end Build_Structure;
  61.  
  62.  
  63. procedure Decompose(Data1 : DATA_STRUCTURE;
  64.                     Val1, Val2, Val3 : out INTEGER) is
  65. begin
  66.    Val1 := Data1.Value1;
  67.    Val2 := Data1.Value2;
  68.    Val3 := Data1.Value3;
  69. end Decompose;
  70.  
  71.  
  72. function Compare_Sum(Data1, Data2 : DATA_STRUCTURE)
  73.                                                  return BOOLEAN is
  74. begin
  75.    return (Data1.Value1 + Data1.Value2 + Data1.Value3) =
  76.                 (Data2.Value1 + Data2.Value2 + Data2.Value3);
  77. end Compare_Sum;
  78.  
  79.  
  80. end Three;
  81.  
  82.  
  83.  
  84.  
  85. -- This program exercises the package Three as an illustration.
  86.  
  87. with Text_IO;   use Text_IO;
  88. with Three;     use Three;
  89.  
  90. procedure CH21_1 is
  91.  
  92.    My_Data, Extra_Data : DATA_STRUCTURE;
  93.    Temp                : DATA_STRUCTURE;
  94.  
  95. begin
  96.  
  97.    My_Data := Build_Structure(3,7,13);
  98.    Extra_Data := Build_Structure(-4,77,0);
  99.    My_Data := My_Data + Extra_Data;
  100.  
  101.    if My_Data /= Extra_Data then
  102.       Put_Line("The two structures are not equal.");
  103.    end if;
  104.  
  105.    My_Data := Extra_Data;
  106.  
  107.    if My_Data = Extra_Data then
  108.       Put_Line("The two structures are equal now.");
  109.    end if;
  110.  
  111. --       The following line is illegal with the private type.
  112. -- My_Data.Value1 := My_Data.Value1 + 13;
  113.  
  114.    Temp := Build_Structure(13,0,0);
  115.    My_Data := My_Data + Temp;
  116.  
  117.    if not Compare_Sum(My_Data, Temp) then
  118.       Put_Line("My_Data and Temp are not equal.");
  119.    end if;
  120.  
  121.    My_Data := Build_Structure(1,9,3);
  122.    if Compare_Sum(My_Data, Temp) then
  123.       Put_Line("My_Data and Temp are equal.");
  124.    end if;
  125.  
  126. end CH21_1;
  127.  
  128.  
  129.  
  130.  
  131. -- Result of execution
  132.  
  133. -- The two structures are not equal.
  134. -- The two structures are equal now.
  135. -- My_Data and Temp are not equal.
  136. -- My_Data and Temp are equal.
  137.  
  138.