charstack.adb
package body CharStack is
Maximum_Size : constant := 25;
Stack_List : STRING(1..Maximum_Size); -- The stack itself, purposely
-- defined very small.
Top_Of_Stack : INTEGER := 0; -- This will always point to
-- the top entry on the stack.
procedure Push(In_Char : in CHARACTER) is
begin
if not Is_Full then
Top_Of_Stack := Top_Of_Stack + 1;
Stack_List(Top_Of_Stack) := In_Char;
end if;
end Push;
procedure Pop(Out_Char : out CHARACTER) is
begin
if Is_Empty then
Out_Char := ' ';
else
Out_Char := Stack_List(Top_Of_Stack);
Top_Of_Stack := Top_Of_Stack - 1;
end if;
end Pop;
function Is_Empty return BOOLEAN is
begin
return Top_Of_Stack = 0;
end Is_Empty;
function Is_Full return BOOLEAN is
begin
return Top_Of_Stack = Maximum_Size;
end Is_Full;
function Current_Stack_Size return INTEGER is
begin
return Top_Of_Stack;
end Current_Stack_Size;
procedure Clear_Stack is
begin
Top_Of_Stack := 0;
end Clear_Stack;
end CharStack;
charstack.ads
package CharStack is
procedure Push(In_Char : in CHARACTER); -- In_Char is added to the
-- stack if there is room.
procedure Pop(Out_Char : out CHARACTER); -- Out_Char is removed from
-- stack and returned if a
-- character is on stack.
-- else a blank is returned
function Is_Empty return BOOLEAN; -- TRUE if stack is empty
function Is_Full return BOOLEAN; -- TRUE if stack is full
function Current_Stack_Size return INTEGER;
procedure Clear_Stack; -- Reset the stack to empty
end CharStack;
main.adb
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
with CharStack;
use CharStack;
procedure TryStack is
Example : constant STRING := "This is the first test.";
Another : constant STRING :=
"This is another test and this should not fit.";
procedure Fill_The_Stack(Input_Line : STRING) is
begin
Clear_Stack;
for Index in 1..Input_Line'LAST loop
if Is_Full then
Put_Line("The stack is full, no more added.");
exit;
else
Push(Input_Line(Index));
end if;
end loop;
end Fill_The_Stack;
procedure Empty_The_Stack is
Char : CHARACTER;
begin
loop
if Is_Empty then
New_Line;
Put_Line("The stack is empty.");
exit;
else
Pop(Char);
Put(Char);
end if;
end loop;
end Empty_The_Stack;
begin
Put_Line(Example);
Fill_The_Stack(Example);
Empty_The_Stack;
New_Line;
Put_Line(Another);
Fill_The_Stack(Another);
Empty_The_Stack;
end TryStak;