# Include <iostream. h>
# Include <stdlib. h>
Const int maxstacksize = 50;
Typedef double datatype;
Class Stack
{
PRIVATE:
Datatype stacklist [maxstacksize];
Int top;
Public:
Stack (void );
Void push (const datatype & item );
Datatype POP (void );
Void clearstack (void );
Datatype PEEK (void) const;
Int stackempty (void) const;
Int stackfull (void) const;
};
STACK: Stack ()
{
Top =-1;
}
Void Stack: Push (const datatype & item)
{
If (Top = maxstacksize-1)
{
Cerr <"stack overflow" <Endl;
Exit (1 );
}
Stacklist [++ top] = item;
}
Datatype Stack: Pop ()
{
Datatype temp;
If (Top =-1)
{
Cerr <"attempt to pop an empty stack" <Endl;
Exit (1 );
}
Temp = stacklist [top --];
Return temp;
}
Datatype Stack: PEEK (void) const
{
Datatype temp;
If (Top =-1)
{
Cerr <"attempt to pop an empty stack" <Endl;
Exit (1 );
}
Temp = stacklist [Top];
Return temp;
}
Int Stack: stackempty () const
{
Return Top =-1;
}
Int Stack: stackfull () const
{
Return Top = maxstacksize-1;
}
Void Stack: clearstack ()
{
Top =-1;
}
Class Calculator
{
PRIVATE:
Stack S;
Void enter (double num );
Bool gettwooperands (double & opnd1, double & opnd2 );
Void compute (char OP );
Public:
// Calculator (void );
Void run (void );
Void clear (void );
};
Void calculator: enter (double num)
{
S. Push (Num );
}
Bool calculator: gettwooperands (double & opnd1, double & opnd2)
{
If (S. stackempty ())
{
Cerr <"missing operand! "<Endl;
Return false;
}
Opnd1 = S. Pop ();
If (S. stackempty ())
{
Cerr <"missing operand! "<Endl;
Return false;
}
Opnd2 = S. Pop ();
Return true;
}
Void calculator: Compute (char OP)
{
Bool result;
Double operand1, operand2;
Result = gettwooperands (operand1, operand2 );
If (result)
{
Switch (OP)
{
Case '+ ':
S. Push (operand2 + operand1 );
Break;
Case '-':
S. Push (operand2-operand1 );
Break;
Case '*':
S. Push (operand2 * operand1 );
Break;
Case '/':
S. Push (operand2/operand1 );
Break;
}
}
Else
{
S. clearstack ();
}
}
Void calculator: Run ()
{
Char C;
Double newoperand;
While (CIN> C, C! = ')
{
Switch (c)
{
Case '+ ':
Case '-':
Case '*':
Case '/':
Compute (C );
Break;
Default:
Cin. putback (C );
Cin> newoperand;
Enter (newoperand );
Break;
}
}
If (! S. stackempty ())
{
Cout <S. Peek () <Endl;
}
}
Void calculator: clear ()
{
S. clearstack ();
}
Void main ()
{
Calculator calc;
Calc. Run ();
}