Solved

Procedure POP in stack writing

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Enable/Disable the Windows 7 'START' button 9 1,479
Regular Expression to Split a String 7 110
Thread safe  opinion 7 139
build html 5 client that connect to indy Tidtcpserver 3 395
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

774 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