this is the one I tried..
but it just can store digit up to 9...
I need to store large number.
can anyone help me??
#include <iostream>
#include <assert.h>
#include <cctype>
#include <cstdlib>
using namespace std;
#if !defined STACK_H
#define STACK_H
const int maxStack = 100;
class IStack
{
friend class StackSeq; // give it access to private members
public:
IStack (): _top (0) {}
void Push ( int i );
int Pop ();
int Top () const;
bool IsFull () const;
bool IsEmpty () const;
private:
int _arr [maxStack];
int _top;
};
class StackSeq
{
public:
StackSeq (IStack const & stack);
int AtEnd () const;
void Advance ();
int GetNum () const;
private:
IStack const & _stack; // reference to stack
int _iCur; // current index into stack
};
#endif
#if !defined INPUT_H
#define INPUT_H
const int maxBuf = 200;
// Tokens are tokNumber, tokError, +, -, *, /.
const int tokNumber = 1;
const int tokError = 2;
// Gets input from stdin, converts to token.
class Input
{
public:
Input ();
int Token () const { return _token; }
int Number () const;
private:
int _token;
char _buf [maxBuf];
};
#endif
#if !defined CALC_H
#define CALC_H
class Calculator
{
public:
bool Execute (Input const & input);
// give access to stack
IStack const & GetStack () { return _stack; }
private:
int Calculate (int n1, int n2, int token) const;
IStack _stack;
};
#endif
void IStack::Push ( int i )
{
// Do not overflow.
assert ( _top < maxStack );
_arr [ _top ] = i;
++_top;
}
int IStack::Pop ()
{
// Do not Pop an empty stack
assert ( _top > 0 );
--_top;
return _arr [ _top ];
}
int IStack::Top () const
{
// Don't call Top on an empty stack
assert ( _top > 0 );
return _arr [ _top - 1 ];
}
bool IStack::IsFull () const
{
assert (_top <= maxStack);
return _top == maxStack;
}
bool IStack::IsEmpty () const
{
assert (_top >= 0);
return _top == 0;
}
// stack sequencer
StackSeq::StackSeq (IStack const & stack )
: _iCur (0), _stack (stack)
{}
int StackSeq::AtEnd () const
{
return _iCur == _stack._top; // friend: can access _top
}
void StackSeq::Advance ()
{
assert (!AtEnd ());
++_iCur;
}
int StackSeq::GetNum () const
{
return _stack._arr [_iCur];
}
Input::Input ()
{
cin >> _buf;
// first char of input is usually enough to decide
// what token it is
int c = _buf [0];
if (isdigit (c))
_token = tokNumber;
else if (c == '+' || c == '*' || c == '/')
_token = c;
else if (c == '-') // allow entering negative numbers
{
if (isdigit (_buf[1]))
_token = tokNumber;
else
_token = c;
}
else
_token = tokError;
}
// call only when token is tokNumber to retrieve numeric value
int Input::Number () const
{
assert (_token == tokNumber);
return atoi (_buf); // convert string to integer
}
// Act depending on input:
// if it's a number, push it on the stack,
// if it's a binary operator, pop two numbers ,
// execute the operation and push the result.
// Returns true if success, false if error.
bool Calculator::Execute (Input const & input)
{
int token = input.Token ();
bool status = false; // assume failure
if (token == tokError)
{
cout << "Unknown token\n";
}
else if (token == tokNumber)
{
if (_stack.IsFull ())
{
cout << "Stack is full\n";
}
else
{
_stack.Push (input.Number());
status = true;
}
}
else
{
assert (token == '+' || token == '-'
|| token == '*' || token == '/');
if (_stack.IsEmpty())
{
cout << "Stack is empty\n";
}
else
{
int num2 = _stack.Pop ();
int num1;
// Special case when only one number on the stack:
// use this number for both operands.
if ( _stack.IsEmpty () )
num1 = num2;
else
num1 = _stack.Pop ();
_stack.Push (Calculate (num1, num2, token));
status = true;
}
}
return status;
}
// Private method used to perform binary operation on operands
int Calculator::Calculate (int num1, int num2, int token) const
{
int result;
if (token == '+')
result = num1 + num2;
else if (token == '-')
result = num1 - num2;
else if (token == '*')
result = num1 * num2;
else if (token == '/')
{
if (num2 == 0)
{
cout << "Division by zero\n";
result = 0;
}
else
result = num1 / num2;
}
return result;
}
int main ()
{
Calculator TheCalculator;
bool status;
do
{
// Prompt for input
cout << "Input > ";
Input input;
status = TheCalculator.Execute (input);
if (status)
{
for (StackSeq seq (TheCalculator.GetStack ());
!seq.AtEnd ();
seq.Advance () )
{
cout << " " << seq.GetNum () << endl;
}
}
} while (status);
return 0;
}