home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog2 / answers / ch16_2.ada < prev   
Encoding:
Text File  |  1991-07-01  |  1.7 KB  |  78 lines

  1.                      -- Chapter 16 - Programming example 2
  2. with Text_IO;
  3. use Text_IO;
  4. with CharStak;
  5. use CharStak;
  6.  
  7. procedure CH16_2 is
  8.  
  9.    package Int_IO is new Text_IO.Integer_IO(INTEGER);
  10.    use Int_IO;
  11.  
  12.    Example : constant STRING := "This is the first test.";
  13.    Another : constant STRING :=
  14.                   "This is another test and this should not fit.";
  15.    Number_Of_Blank_Spaces : INTEGER;
  16.  
  17.    procedure Fill_The_Stack(Input_Line : STRING) is
  18.    begin
  19.       Clear_Stack;
  20.       for Index in 1..Input_Line'LAST loop
  21.          if Is_Full then
  22.             Put_Line("The stack is full, no more added.");
  23.             exit;
  24.          else
  25.             Push(Input_Line(Index));
  26.          end if;
  27.       end loop;
  28.    end Fill_The_Stack;
  29.  
  30.    procedure Empty_The_Stack is
  31.    Char : CHARACTER;
  32.    begin
  33.       loop
  34.          if Is_Empty then
  35.             New_Line;
  36.             Put_Line("The stack is empty.");
  37.             exit;
  38.          else
  39.             Pop(Char);
  40.             Put(Char);
  41.          end if;
  42.       end loop;
  43.    end Empty_The_Stack;
  44.  
  45. begin
  46.  
  47.    Put_Line(Example);
  48.    Fill_The_Stack(Example);
  49.    Number_Of_Blank_Spaces := Room_Left;
  50.    Put("Room left on stack =");
  51.           Put(Number_Of_Blank_Spaces); New_Line;
  52.    Empty_The_Stack;
  53.  
  54.    New_Line;
  55.    Put_Line(Another);
  56.    Fill_The_Stack(Another);
  57.    Put("Room left on stack ="); Put(Room_Left); New_Line;
  58.    Empty_The_Stack;
  59.  
  60. end CH16_2;
  61.  
  62.  
  63.  
  64.  
  65. -- Result of execution
  66.  
  67. -- This is the first test.
  68. -- Room left on stack =      2
  69. -- .tset tsrif eht si sihT
  70. -- The stack is empty.
  71. --
  72. -- This is another test and should not fit.
  73. -- The stack is full, no more added.
  74. -- Room left on stack =      0
  75. --  dna tset rehtona si sihT
  76. -- The stack is empty.
  77.  
  78.