need some help !!!
Dec 14, 2013 at 4:15pm UTC
i try to write a postfix calculator program using stack in C++
the in put must be the infix expression .......... but can dont know how to write a infix expression in put ...... please help me !!!
this is my code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <conio.h>
using namespace std;
class Calculator
{
private :
int Calculate (int n1, int n2, int token) const ;
IStack _stack;
public :
bool Execute (Input const & input);
// give access to stack
IStack const & GetStack () { return _stack; }
};
const int maxBuf = 100;
const int tokNumber = 1;
const int tokError = 2;
class Input
{
public :
Input ();
int Token () const { return _token; }
int Number () const ;
private :
int _token;
char _buf [maxBuf];
};
const int maxStack = 16;
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
};
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;
}
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
}
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];
}
void main ()
{
Calculator TheCalculator;
bool status;
do
{
// Prompt for input
cout << "> " ;
Input input;
status = TheCalculator.Execute (input);
if (status)
{
for (StackSeq seq (TheCalculator.GetStack ());
!seq.AtEnd ();
seq.Advance () )
{
cout << " " << seq.GetNum () << endl;
}
}
} while (status);
}
Dec 14, 2013 at 4:45pm UTC
:( any one here ......... my deadline incoming
Dec 15, 2013 at 2:07pm UTC
:( no one here ........... if u have no time to fix it plz give me some hint
Topic archived. No new replies allowed.