code doesn't give any error but i didn't get the desired output
here's the code
//Evaluating Postfix expression
#include <iostream>
#include <process.h>
#include <string>
#include <math.h>
using namespace std;
int calc (int op1, int op2, char ch);
template <class Type>
class Stack
{
private:
Type *stack;
int top;
int Maxsize;
public:
Stack(int Max)
{
stack = new Type[Max];
top = -1;
Maxsize = Max;
}
bool isempty()
{
return (top==-1);
}
bool isfull()
{
if (top == Maxsize)
return true;
else
return false;
}
void push(Type val)
{
if (!isfull())
{
top++;
stack[top] = val;
}
else
{
cout << "stack overflow";
exit(1);
}
}
Type pop()
{
if (!isempty())
{
int x = stack[top];
top--;
return x;
}
else
{
cout << "stack underflow";
exit(1);
}
}
Type stacktop()
{
if (!isempty())
return stack[top];
else
{
cout << "stack underflow";
exit(1);
}
}
};
void main()
{
Stack<int> s(4);
string s1,exp;
int op1, op2, r;
cout << "Enter prefix exp";
cin >>exp;
for (int i=0; i< exp.length(); i++)
{
char symb= exp[i];
s.push(symb);
}
for (int j=s.stacktop();!s.isempty(); j--)
{
s1=s.pop();
}
for (int k=0; k< s1.length(); k++)
{
char symb= s1[i];
if (symb >='0' && symb <= '9')
s.push(symb - '0');
else
{
op2 = s.pop();
op1 = s.pop();
r = calc(op1, op2, symb);
s.push(r);
}
}
cout << "Result is "<< s.pop();
}
int calc (int op1, int op2, char ch)
{
switch (ch)
{
case '+': return op1+op2; break;
case '-': return op1-op2; break;
case '*': return op1*op2; break;
case '/': return op1/op2; break;
case '$': return pow(op1,op2); break;
}
}
This post has been edited by Salem_c: 10 October 2012 - 01:01 PM
Reason for edit:: added [code][/code] tags - learn to use them yourself

New Topic/Question
Reply



MultiQuote




|