?
Solved

Procedure POP in stack writing

Posted on 1998-12-26
4
Medium Priority
?
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
scrapdog earned 200 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

718 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