Solved

Procedure POP in stack writing

Posted on 1998-12-26
4
261 Views
Last Modified: 2010-04-16
Hi,

I'm having problems in writing a stack-based calculator program. Actually, I got a unit Stackadt and program Stackcalc. The Stackcalc is for testing the unit Stackadt.

When I run the Stackcalc, it looks have problems in the  procedure POP and I don't know how to make correct ?

Also, I'm not so sure the input "#" was in correct writing or not ?


Belows is the two of file and I'll mark in *** for those statements which I've problems in its.

unit StackADT;
{Provides type Stack, a sequence permitting
 insertions and deletions only a the front.}

interface

  const
         MaxStackSize = 100;

  type
         StackElement = Integer;
         StackSize    = 0..MaxStackSize;
         StackIndex   = 1..MaxStackSize;
         Stack = record
                         size : StackSize;
                         val  : array [StackIndex]
                                of StackElement;
                 end;

  procedure MakeEmpty (var s {input/output} : Stack);
  {Make s = the empty stack.}

  procedure Push (var s {input/output} : Stack;
                  e {input} : StackElement);
  {Inserts e at front of stack s.}

  procedure Pop (var s {input/output} : Stack;
                 var v {output} : StackElement);
  {Requires s not to be empty; removes first element of s and
   stores in v.}

  function Top (s {input} : Stack) : StackElement;
  {Requires s not to be empty; returns first element of s.}

  function Empty (s {input} : Stack) : Boolean;
  {Returns (s is empty).}

implementation

  procedure MakeEmpty (var s {input/output} : Stack);
  begin {MakeEmpty}
         s.size := 0
  end; {MakeEmpty}

  procedure Push (var s {input/output} : Stack;
                  e {input} : StackElement);
  begin {Push}
         if s.size = MaxStackSize then
                     WriteLn('Error - Can''t push onto full stack.')
         else
               begin
                      s.size := s.size + 1;
                      s.val[s.size] := e;
               end
  end; {Push}

  procedure Pop (var s {input/output} : Stack;
                 var v {output} : StackElement);
  begin {Pop}
         if Empty(s) then
                     WriteLn('Error - Can''t pop empty stack.')
         else
               begin
***                   s.val[1] := 0;
***                   s.val[1] := v;
               end
  end; {Pop}

  function Top (s {input} : Stack) : StackElement;
  begin {Top}
         if Empty(s) then
                     WriteLn('Error - No top of empty stack.')
         else
***           top := s.val[1];
  end; {Top}

  function Empty (s {input} : Stack) : Boolean;
  begin {Empty}
         empty := (s.size = 0);
  end; {Empty}

end. {StackADT}


program StackCalc;
{Stack-based calculator for simple integer arithmetic.}

  uses
        StackADT;

  var
        S : Stack;
        Oper1, Oper2, Result : StackElement;
        Continue : Boolean;
        Input : String;
        Number, Error : Integer;

begin {StackCalc}
      Continue := true;
      MakeEmpty (S);
      repeat
              Write ('> ');
              ReadLn (Input);
              Val (Input, Number, Error);
              if Error = 0 then
                                 Push (S, Number)
              else
                    begin
                           if Input = '+' then
                              begin
                                     pop (s, oper1);
                                     pop (s, oper2);
                                     result := (oper1 + oper2);
                                     push (s, result);
                              end;
                           if Input = '-' then
                              begin
                                     pop (s, oper1);
                                     pop (s, oper2);
                                     result := (oper1 - oper2);
                                     push (s, result);
                              end;
                           if Input = '*' then
                              begin
                                     pop (s,oper2);
                                     pop (s,oper1);
                                     result := (oper1 * oper2);
                                     push (s, result);
                              end;
                           if Input = '/' then
                              begin
                                     pop (s,oper2);
                                     pop (s,oper1);
                                     result := (oper1 div oper2);
                                     push (s, result);
                              end;
                           if Input = '#' then
                              begin
***                                  result := (oper1 - oper2);
***                                  push (s, result);
                              end;
                           if Input = '.' then
                              begin
                                     WriteLn ('> ', Top(S))
                              end;
                           if Input = 'p' then
                              begin
                                     Pop (S, Result);
                                     WriteLn ('> ', Result)
                              end;
                           if Input = 'c' then
                              begin
                                     MakeEmpty (S);
                                     WriteLn ('> Stack Cleared')
                              end;
                           if Input = 'e' then
                              begin
                                     if Empty (S) then
                                        WriteLn ('> Stack Empty')
                                     else
                                        WriteLn ('> Stack Not Empty')
                              end;
                           if Input = 'q' then
                                     Continue := false
                    end; {else}
      until Continue = false
end. {StackCalc}


Thanks,

Zoe
0
Comment
Question by:zoe_chui
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
scrapdog earned 50 total points
ID: 1216713
Your StackADT unit only needs minor changes...see the functions below.
---------------------------------------------------------------


function Top (s {input} : Stack) : StackElement;
  begin {Top}
         if Empty(s) then
                     WriteLn('Error - No top of empty stack.')
         else
***           top := s.val[s.size];
  end; {Top}
------------------------------------

procedure Pop (var s {input/output} : Stack;
                 var v {output} : StackElement);
  begin {Pop}
         if Empty(s) then
                     WriteLn('Error - Can''t pop empty stack.')
         else
               begin
***                   v := s.val[s.size];
***                   s.size := s.size - 1 ;
               end ;
  end; {Pop}


----------------------------------------------

As far as this part goes:

                           if Input = '#' then
                              begin
***                                  result := (oper1 - oper2);
***                                  push (s, result);
                              end;

I don't know exactly what you are trying to do with the # operator, but I do know that the code you have will not do anything except compute the difference of the operands from the last operation and push the result...this doesn't seem to make sense.  Perhaps you forgot to include the pop(s, oper1) and pop(s,oper2) here?  If that is the case, then include these statements...if not, please explain what # is supposed to do, then I could help you with that part of the program.
0
 

Author Comment

by:zoe_chui
ID: 1216714
The # operator mean negative number such as -7 ,-10 etc.
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1216715
In that case you might want to try this:


if Input = '#' then
  begin
      pop (s,result);  
      push (s, -result);
  end;
0
 

Author Comment

by:zoe_chui
ID: 1216716
Thanks a lot.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

We have come a long way with backup and data protection — from backing up to floppies, external drives, CDs, Blu-ray, flash drives, SSD drives, and now to the cloud.
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now