Hi,
Please help me debug this code. I made a Stack template with two parameters: type and size. I am using it two times: first in conversion from infix expression to postfix (here, <char,50> stack is used), then in evaluation of postfix expression (here, <long double, 50> stack is used).
There is an error while using 2nd stack. how to debug it.
#include <iostream>
#include <cctype>
#include <cmath>
using namespace std;
template <class T,int size>
class Stack {
private:
int top;
T *nodes;
public:
Stack ();
int empty (void);
void push(T &);
T pop(void);
T pop(int &);
~Stack ();
};
template <class T, int size> Stack<T,size>::Stack ()
{
top = -1;
nodes = new T[size];
};
template <class T, int size> Stack<T,size>::~Stack ()
{
delete nodes;
};
template <class T, int size> int Stack<T, size>::empty()
{
if (top == -1)
return 1;
else
return 0;
}
template <class T, int size> void Stack<T,size>::push(T & j)
{
if (top == size - 1) {
cout << "Stack overflow" << endl;
return;
}
nodes[++top] = j;
};
template <class T, int size> T Stack<T,size>::pop(void)
{
T p;
if (empty ()) {
cout << "Stack underflow" << endl;
return p;
}
p = nodes[top--];
return p;
};
//popandtest
template <class T, int size> T Stack<T,size>::pop(int & und)
{
T p;
if (empty ()) {
und = 1;
return p;
}
und = 0;
p = nodes[top--];
return p;
};
void postfix(char *infix, char *postr);
int isoperand(char op);
int isoperator(char op);
long double eval(char *postr);
long double oper(int sym, long double op1, long double op2);
int priority(char op);
int priority(char op) {
if (op == '^' || op == '$')
return 3;
else if (op == '%' || op == '*' || op == '/')
return 2;
else if (op == '+' || op == '-')
return 1;
else
return 0;
}
int isoperator(char op) {
if (op == '^' || op == '$' || op == '%' || op == '/' || op == '*' || op == '+' || op == '-')
return 1;
else
return 0;
}
int isoperand(char op) {
if (isalnum(op))
return 1;
else
return 0;
}
void postfix(char *infix, char *postr)
{
int position, und;
int outpos = 0;
char topsymb;
Stack<char,50> opstk;
for (position = 0; infix[position] != '\0'; position++) {
if (isoperand(infix[position]))
{
postr[outpos++] = infix[position];
}
else if (infix[position] == '(')
{
opstk.push(infix[position]);
}
else if (isoperator(infix[position]))
{
int prty = priority(infix[position]);
topsymb = opstk.pop(und);
while (!und && priority(topsymb) >= prty) {
postr[outpos++] = topsymb;
topsymb = opstk.pop(und);
}
if (!und)
opstk.push(topsymb);
opstk.push(infix[position]);
}
else if (infix[position] == ')')
{
while ((topsymb = opstk.pop()) != '(')
postr[outpos++] = topsymb;
}
}
while (!opstk.empty())
postr[outpos++] = opstk.pop();
postr[outpos] = '\0';
}
long double oper(int symb, long double op1, long double op2)
{
switch (symb) {
case '+':
return op1+op2;
break;
case '-':
return op1-op2;
break;
case '*':
return op1*op2;
break;
case '/':
return op1/op2;
break;
case '^': case '$':
return pow(op1,op2);
default:
cout << "Invalid operator\n";
}
}
long double eval(char *postr)
{
int c, position;
long double opnd1, opnd2, value;
Stack<long double, 50> opndstk;
for (position = 0; (c = postr[position]) != '\0'; position++)
if (isoperand(c))
opndstk.push((long double) (c-'0'));
else {
opnd2 = opndstk.pop();
opnd1 = opndstk.pop();
value = oper(c, opnd1, opnd2);
opndstk.push(value);
}
return (opndstk.pop());
}
int main(void)
{
char in[250], post[250];
long double res;
cin >> in;
cout << in << endl;
postfix (in, post);
res = eval(post);
cout << res << endl;
return 0;
}