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

  1.                        -- Chapter 21 - Programming exercise 2
  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 array(1..3) of INTEGER;
  21. end Three;
  22.  
  23.  
  24.  
  25. package body Three is
  26.  
  27. function "+"(Data1, Data2 : DATA_STRUCTURE) return DATA_STRUCTURE is
  28. Temp : DATA_STRUCTURE;
  29. begin
  30.    Temp(1) := Data1(1) + Data2(1);
  31.    Temp(2) := Data1(2) + Data2(2);
  32.    Temp(3) := Data1(3) + Data2(3);
  33.    return Temp;
  34. end "+";
  35.  
  36.  
  37. function "-"(Data1, Data2 : DATA_STRUCTURE) return DATA_STRUCTURE is
  38. Temp : DATA_STRUCTURE;
  39. begin
  40.    Temp(1) := Data1(1) - Data2(1);
  41.    Temp(2) := Data1(2) - Data2(2);
  42.    Temp(3) := Data1(3) - Data2(3);
  43.    return Temp;
  44. end "-";
  45.  
  46.  
  47. function Build_Structure(Val1, Val2, Val3 : INTEGER) return
  48.                                                    DATA_STRUCTURE is
  49. Temp : DATA_STRUCTURE;
  50. begin
  51.    Temp(1) := Val1;
  52.    Temp(2) := Val2;
  53.    Temp(3) := Val3;
  54.    return Temp;
  55. end Build_Structure;
  56.  
  57.  
  58. procedure Decompose(Data1 : DATA_STRUCTURE;
  59.                     Val1, Val2, Val3 : out INTEGER) is
  60. begin
  61.    Val1 := Data1(1);
  62.    Val2 := Data1(2);
  63.    Val3 := Data1(3);
  64. end Decompose;
  65.  
  66.  
  67. function Compare_Sum(Data1, Data2 : DATA_STRUCTURE)
  68.                                                  return BOOLEAN is
  69. begin
  70.    return (Data1(1) + Data1(2) + Data1(3)) =
  71.                                   (Data2(1) + Data2(2) + Data2(3));
  72. end Compare_Sum;
  73.  
  74. end Three;
  75.  
  76.  
  77.  
  78.  
  79. -- This program exercises the package Three as an illustration.
  80.  
  81. with Text_IO;   use Text_IO;
  82. with Three;     use Three;
  83.  
  84. procedure CH21_2 is
  85.  
  86.    My_Data, Extra_Data : DATA_STRUCTURE;
  87.    Temp                : DATA_STRUCTURE;
  88.  
  89. begin
  90.  
  91.    My_Data := Build_Structure(3,7,13);
  92.    Extra_Data := Build_Structure(-4,77,0);
  93.    My_Data := My_Data + Extra_Data;
  94.  
  95.    if My_Data /= Extra_Data then
  96.       Put_Line("The two structures are not equal.");
  97.    end if;
  98.  
  99.    My_Data := Extra_Data;
  100.  
  101.    if My_Data = Extra_Data then
  102.       Put_Line("The two structures are equal now.");
  103.    end if;
  104.  
  105. --       The following line is illegal with the private type.
  106. -- My_Data.Value1 := My_Data.Value1 + 13;
  107.  
  108.    Temp := Build_Structure(13,0,0);
  109.    My_Data := My_Data + Temp;
  110.  
  111.    if not Compare_Sum(My_Data, Temp) then
  112.       Put_Line("My_Data and Temp are not equal.");
  113.    end if;
  114.  
  115.    My_Data := Build_Structure(1,9,3);
  116.    if Compare_Sum(My_Data, Temp) then
  117.       Put_Line("My_Data and Temp are equal.");
  118.    end if;
  119.  
  120. end CH21_2;
  121.  
  122.  
  123.  
  124.  
  125. -- Result of execution
  126.  
  127. -- The two structures are not equal.
  128. -- The two structures are equal now.
  129. -- My_Data and Temp are not equal.
  130. -- My_Data and Temp are equal.
  131.  
  132.