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

  1.                      -- Chapter 17 - Programming example 1
  2. with Text_IO;
  3. use Text_IO;
  4.  
  5. procedure CH17_1 is
  6.  
  7.    package Int_IO is new Text_IO.Integer_IO(INTEGER);
  8.    use Int_IO;
  9.  
  10.    procedure Divide_Loop(Index : in     INTEGER) is
  11.    Divide_Result : INTEGER;
  12.    begin
  13.       Put("Index is");
  14.       Put(Index,3);
  15.       Put(" and the answer is");
  16.       Divide_Result := 25/(4 - Index);
  17.       Put(Divide_Result);
  18.       New_Line;
  19.    exception
  20.       when Numeric_Error => Put(" Divide by zero error");
  21.                             Put_Line(" in loop 1.");
  22.    end Divide_Loop;
  23.  
  24.    procedure New_Divide_Loop(Index : in     INTEGER) is
  25.       My_Own_Exception : exception;
  26.       Divide_Result : INTEGER;
  27.    begin
  28.       Put("Index is");
  29.       Put(Index,3);
  30.       Put(" and the answer is");
  31.       if Index = 4 then
  32.          raise My_Own_Exception;
  33.       end if;
  34.       Divide_Result := 25/(2 - Index);
  35.       Put(Divide_Result);
  36.       New_Line;
  37.    exception
  38.       when My_Own_Exception => Put(" Divide by zero error");
  39.                                Put_Line(" in loop 3.");
  40.       when Numeric_Error => Put_Line("This shouldn't happen.");
  41.                             Put_Line("But is included anyway.");
  42.       when others => Put_Line("Some other exception found.");
  43.    end New_Divide_Loop;
  44.  
  45. begin
  46.    Put_Line("Begin program here.");
  47.    for Count in 1..6 loop              -- begin loop number 1
  48.       Divide_Loop(Count);
  49.    end loop;
  50.    Put_Line("End of first loop.");
  51.  
  52.    for Count in 1..6 loop              -- begin loop number 2
  53.       declare
  54.          Divide_Result : INTEGER;
  55.       begin
  56.          Put("Count is");
  57.          Put(Count,3);
  58.          Put(" and the answer is");
  59.          Divide_Result := 25/(4 - Count);
  60.          Put(Divide_Result);
  61.          New_Line;
  62.       exception
  63.          when Numeric_Error => Put(" Divide by zero error");
  64.                                Put_Line(" in loop 2.");
  65.       end;
  66.    end loop;
  67.    Put_Line("End of second loop.");
  68.  
  69.    for Count in 1..6 loop              -- begin loop number 3
  70.       New_Divide_Loop(Count);
  71.    end loop;
  72.    Put_Line("End of program.");
  73.  
  74. end CH17_1;
  75.  
  76.  
  77.  
  78.  
  79. -- Result of Execution
  80.  
  81. -- Begin program here.
  82. -- Index is  1 and the answer is     8
  83. -- Index is  2 and the answer is    12
  84. -- Index is  3 and the answer is    25
  85. -- Index is  4 and the answer is Divide by zero error in loop 1.
  86. -- Index is  5 and the answer is   -25
  87. -- Index is  6 and the answer is   -12
  88. -- End of first loop.
  89. -- Count is  1 and the answer is     8
  90. -- Count is  2 and the answer is    12
  91. -- Count is  3 and the answer is    25
  92. -- Count is  4 and the answer is Divide by zero error in loop 2.
  93. -- Count is  5 and the answer is   -25
  94. -- Count is  6 and the answer is   -12
  95. -- End of second loop.
  96. -- Index is  1 and the answer is    25
  97. -- Index is  2 and the answer isThis shouldn't happen.
  98. -- But is included anyway.
  99. -- Index is  3 and the answer is   -25
  100. -- Index is  4 and the answer is Divide by zero error in loop 3.
  101. -- Index is  5 and the answer is    -8
  102. -- Index is  6 and the answer is    -6
  103. -- End of program.
  104.  
  105.