comp.lang.ada
 help / color / mirror / Atom feed
* simple newbie program.
@ 2002-11-28  7:26 prashna
  0 siblings, 0 replies; 2+ messages in thread
From: prashna @ 2002-11-28  7:26 UTC (permalink / raw)


Hi all,

have look at following code.

package stack is
   procedure initialise_stack;
   procedure push (item : in integer);
   procedure pop (item : out integer);
   procedure display;
end stack;

package body stack is
   stack_size : constant := 100;
   type stack_size_type is range 0..stack_size;

   top   : stack_size_type;
   stack : array (stack_size_type'range) of integer;

   procedure initialise_stack is separate;
   procedure push (item : in integer) is separate;
   procedure pop (item : out integer) is separate;
   procedure display is separate;
end stack;

separate (stack)
procedure initialise_stack is
begin
 top := 0;
end initialise_stack;

separate (stack)
procedure push (item : in integer) is
begin
   stack(top) := item;
   top := top + 1;
end;

separate (stack)
procedure pop ( item : out integer) is
begin
   top := top - 1;
   item := stack(top);
end pop;

with text_io; use text_io;
separate (stack)
procedure display is
begin
   for i in reverse stack_size_type'first .. top-1 loop
      put(integer'image(stack(i)));
   end loop;
end display;

with text_io;use text_io;
with stack;
procedure stack_user is
   item   : integer;
   choice : integer range 1..5;

   package int_io is new text_io.integer_io(integer);
   use int_io;
begin
   loop
      put_line("Enter choice");
      put_line("1 to reset the stack");
      put_line("2 to push to the stack");
      put_line("3 to pop from the stack");
      put_line("4 to display stack");
      put_line("5 to quit");

      get(choice);

      case choice is
      when 1 => stack.initialise_stack;
      when 2 => 
         put_line("Enter item to be pushed");
         get(item);
         stack.push (item);
      when 3 =>
         stack.pop(item);
         put_line("poped item is ");
         put(item);new_line;
      when 4 =>
         stack.display;
      when 5=>
           exit;
      end case;
   end loop;
end;

I know there will be bugs in this, Please help me to improve this stack program.

thanks



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: simple newbie program.
@ 2002-11-28  7:35 Grein, Christoph
  0 siblings, 0 replies; 2+ messages in thread
From: Grein, Christoph @ 2002-11-28  7:35 UTC (permalink / raw)


> package body stack is
>    stack_size : constant := 100;
>    type stack_size_type is range 0..stack_size;
> 
>    top   : stack_size_type;
>    stack : array (stack_size_type'range) of integer;
                                   ~~~~~~
     stack : array (stack_size_type) of integer;

But the size is not stack_size, it's stack_size+1, because you start at 0.

Overflow and underflow are not handled.

>    package int_io is new text_io.integer_io(integer);

There exists a pre-instantiation of Ada.Text_IO.Integer_IO (Integer):
    Ada.Integer_Text_IO;



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-11-28  7:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-11-28  7:35 simple newbie program Grein, Christoph
  -- strict thread matches above, loose matches on Subject: below --
2002-11-28  7:26 prashna

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox