Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Procedure POP in stack writing

Posted on 1998-12-26
4
Medium Priority
?
278 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Mailbox Corruption is a nightmare every Exchange DBA wishes he never has. Recovering from it can be super-hectic if not entirely futile. And though techniques like the New-MailboxRepairRequest cmdlet have been designed to help with fixing minor corr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

885 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