Solved

Procedure POP in stack writing

Posted on 1998-12-26
4
262 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi Thead Pool or (only run last procedure called) 13 806
WebCam and Delphi 2 2,568
Utf8 encode 2 568
code issue 8 100
Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
There are many Password Managers (PM) out there to choose from. PM's can help with your password habits and routines, but they should not be a crutch you rely on too heavily. I also have an article for company/enterprise PM's.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

26 Experts available now in Live!

Get 1:1 Help Now