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

  1.                                        -- Chapter 16 - Program 1
  2. package CharStak is
  3.  
  4. procedure Push(In_Char : in CHARACTER);  -- In_Char is added to the
  5.                                          -- stack if there is room.
  6.  
  7. procedure Pop(Out_Char : out CHARACTER); -- Out_Char is removed from
  8.                                          -- stack and returned if a
  9.                                          -- character is on stack.
  10.                                          -- else a blank is returned
  11.  
  12. function Is_Empty return BOOLEAN;        -- TRUE if stack is empty
  13.  
  14. function Is_Full return BOOLEAN;         -- TRUE if stack is full
  15.  
  16. function Current_Stack_Size return INTEGER;
  17.  
  18. procedure Clear_Stack;                   -- Reset the stack to empty
  19.  
  20. end CharStak;
  21.  
  22.  
  23.  
  24.  
  25.  
  26. package body CharStak is
  27.  
  28. Maximum_Size : constant := 25;
  29. Stack_List : STRING(1..Maximum_Size); -- The stack itself, purposely
  30.                                       -- defined very small.
  31. Top_Of_Stack : INTEGER := 0;          -- This will always point to
  32.                                       -- the top entry on the stack.
  33.  
  34. procedure Push(In_Char : in CHARACTER) is
  35. begin
  36.    if not Is_Full then
  37.       Top_Of_Stack := Top_Of_Stack + 1;
  38.       Stack_List(Top_Of_Stack) := In_Char;
  39.    end if;
  40. end Push;
  41.  
  42.  
  43. procedure Pop(Out_Char : out CHARACTER) is
  44. begin
  45.    if Is_Empty then
  46.       Out_Char := ' ';
  47.    else
  48.       Out_Char := Stack_List(Top_Of_Stack);
  49.       Top_Of_Stack := Top_Of_Stack - 1;
  50.    end if;
  51. end Pop;
  52.  
  53.  
  54. function Is_Empty return BOOLEAN is
  55. begin
  56.    return Top_Of_Stack = 0;
  57. end Is_Empty;
  58.  
  59.  
  60. function Is_Full return BOOLEAN is
  61. begin
  62.    return Top_Of_Stack = Maximum_Size;
  63. end Is_Full;
  64.  
  65.  
  66. function Current_Stack_Size return INTEGER is
  67. begin
  68.    return Top_Of_Stack;
  69. end Current_Stack_Size;
  70.  
  71.  
  72. procedure Clear_Stack is
  73. begin
  74.    Top_Of_Stack := 0;
  75. end Clear_Stack;
  76.  
  77. end CharStak;
  78.  
  79.