OO Stack Driver
MC logo
 

OO Stack Driver

Ada Code Examples


<<OO String Stack Body Download EOF Exception Example>>
--
-- Use the general stack package.
--
with Gnat.Io; use Gnat.Io;
with GenStack; use GenStack;
with IStack; use IStack;
with SStack; use SStack;

procedure UseStack is
   -- Print the stack of integers.
   procedure PrintInt(S: in out Stack) is
      I: IntStackData;
   begin
      while not Empty(S) loop
        Pop(S, I);
        Put(IValue(I)); Put(" ");
      end loop;
      New_Line;
   end PrintInt;

   -- Print the stack of strings.
   procedure PrintStr(S: in out Stack) is
      I: StrStackData;
   begin
      while not Empty(S) loop
        Pop(S, I);
        Put_Line(SValue(I));
      end loop;
   end PrintStr;

   -- Test vars.
   In_Int: Integer;
   In_String: String(1..50);
   I: Integer;
   R, S: Stack;
begin
   -- Get some integers.
   loop
      -- Read and integer, stopping at -1.
      Put("> ");
      Get(In_Int);
      exit when In_Int = -1;

      Push(R, IDat(In_Int));
   end loop;

   -- Print 'em, twice.
   S := R;
   PrintInt(R);
   PrintInt(S);

   -- Get some strings.
   Get_Line(In_String, I);
   New_Line;
   loop
      -- Read a string
      Put("> ");
      Get_Line(In_String, I);
      exit when In_String(1..I) = "stop";

      Push(R, SDat(In_String(1..I)));
   end loop;

   S := R;
   New_Line;
   PrintStr(R);
   New_Line;
   PrintStr(S);

end UseStack;
<<OO String Stack Body EOF Exception Example>>