076.字符串堆栈的实现及其应用

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;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值