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

  1.                                        -- Chapter 21 - Program 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.  
  11. type DATA_STRUCTURE is
  12.    record
  13.       Value1 : INTEGER;
  14.       Value2 : INTEGER;
  15.       Value3 : INTEGER;
  16.    end record;
  17.  
  18. function "+"(Data1, Data2 : DATA_STRUCTURE) return DATA_STRUCTURE;
  19. function "-"(Data1, Data2 : DATA_STRUCTURE) return DATA_STRUCTURE;
  20. function Build_Structure(Val1, Val2, Val3 : INTEGER) return
  21.                                                    DATA_STRUCTURE;
  22. procedure Decompose(Data1 : DATA_STRUCTURE;
  23.                     Val1, Val2, Val3 : out INTEGER);
  24. end Three;
  25.  
  26.  
  27.  
  28. package body Three is
  29.  
  30. function "+"(Data1, Data2 : DATA_STRUCTURE) return DATA_STRUCTURE is
  31. Temp : DATA_STRUCTURE;
  32. begin
  33.    Temp.Value1 := Data1.Value1 + Data2.Value1;
  34.    Temp.Value2 := Data1.Value2 + Data2.Value2;
  35.    Temp.Value3 := Data1.Value3 + Data2.Value3;
  36.    return Temp;
  37. end "+";
  38.  
  39.  
  40. function "-"(Data1, Data2 : DATA_STRUCTURE) return DATA_STRUCTURE is
  41. Temp : DATA_STRUCTURE;
  42. begin
  43.    Temp.Value1 := Data1.Value1 - Data2.Value1;
  44.    Temp.Value2 := Data1.Value2 - Data2.Value2;
  45.    Temp.Value3 := Data1.Value3 - Data2.Value3;
  46.    return Temp;
  47. end "-";
  48.  
  49.  
  50. function Build_Structure(Val1, Val2, Val3 : INTEGER) return
  51.                                                    DATA_STRUCTURE is
  52. Temp : DATA_STRUCTURE;
  53. begin
  54.    Temp.Value1 := Val1;
  55.    Temp.Value2 := Val2;
  56.    Temp.Value3 := Val3;
  57.    return Temp;
  58. end Build_Structure;
  59.  
  60.  
  61. procedure Decompose(Data1 : DATA_STRUCTURE;
  62.                     Val1, Val2, Val3 : out INTEGER) is
  63. begin
  64.    Val1 := Data1.Value1;
  65.    Val2 := Data1.Value2;
  66.    Val3 := Data1.Value3;
  67. end Decompose;
  68.  
  69. end Three;
  70.  
  71.  
  72.  
  73.  
  74. -- This program exercises the package Three as an illustration.
  75.  
  76. with Text_IO;   use Text_IO;
  77. with Three;     use Three;
  78.  
  79. procedure NoPrivat is
  80.  
  81.    My_Data, Extra_Data : DATA_STRUCTURE;
  82.  
  83. begin
  84.  
  85.    My_Data := Build_Structure(3,7,13);
  86.    Extra_Data := Build_Structure(-4,77,0);
  87.    My_Data := My_Data + Extra_Data;
  88.  
  89.    if My_Data /= Extra_Data then
  90.       Put_Line("The two structures are not equal.");
  91.    end if;
  92.  
  93.    My_Data := Extra_Data;
  94.  
  95.    if My_Data = Extra_Data then
  96.       Put_Line("The two structures are equal now.");
  97.    end if;
  98.  
  99.    My_Data.Value1 := My_Data.Value1 + 13;
  100.  
  101. end NoPrivat;
  102.  
  103.  
  104.  
  105.  
  106. -- Result of execution
  107.  
  108. -- The two structures are not equal.
  109. -- The two structures are equal now.
  110.  
  111.