Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to create a function that will evaluate a postfix expression and sol

ID: 3769867 • Letter: I

Question

I'm trying to create a function that will evaluate a postfix expression and solve it. It keeps returning zero and i'm not sure why. Help? Thanks!

int evaluatePostfix(string postExpr)
{
   Stack operandStack;
   int result;
   char x;
   for(int i = 0; i < postExpr.size(); i++)
   {
       while(!postExpr.empty())
       {
           while(x == ' ')
           {
               x++;
           }
           x = postExpr[i+1];
//cout << "x : " << x << endl;
           if(isdigit(x))
           {
               operandStack.push(x);
           }
           else
           {
               int op1 = operandStack.pop();
               int op2 = operandStack.pop();
//cout << "op1 : " << op1 << endl;
               //op1 ~operator~ op2
               switch(x)
               {
                   case '+':
                       result = op2 + op1;
                       break;

                   case '-':
                       result = op2 - op1;
                       break;

                   case '/':
                       result = op2 / op1;
                       break;

                   case '*':
                       result = op2 * op1;
                       break;

                   default:
                       cout << " Invalid Operator" << endl;
                       return 0;
               }
               operandStack.pop(result);
           }
       }
   }
   result = operandStack.pop();
   return result;
}

Stack::Stack()
{
   top = -1;
}

void Stack::push(char item)
{
   cstack.push_back(item);
   top++;
}

bool Stack::empty()
{
   if (top == -1) return true;
   else return false;
}

bool Stack::pop()
{
   try {
       if (cstack.size()==0) throw 0;
   } catch (int err) {
       //cout << "stack is empty." << endl;
       //cout << "Cannot pop an item."<< endl;
       return false;
   }
   cstack.pop_back();
   top--;
   return true;
}

------------------.h----------------------

#ifndef STACK_H
#define   STACK_H

#include <iostream>
#include <vector>

using namespace std;

class Stack
{
public:
   Stack();
   void push(char item);
   bool pop();
   char stackTop();
   bool empty();
private:
   int top;
   vector<char> cstack;
};


#endif   /* STACK_H */

Explanation / Answer

#include <iostream>
#include <vector>
using namespace std;
class Stack
{
public:
Stack();
void push(char item);
char pop();
char stackTop();
bool empty();
private:
int top;
vector<char> cstack;
};


int evaluatePostfix(string postExpr)
{
Stack operandStack;
int result;
char x;
for(int i = 0; i < postExpr.size(); i++)
{
x = postExpr[i];
cout << "x : " << x << endl;
if(isdigit(x))
{
operandStack.push(x);
cout<<x<<" is pushed ";
}
else
{
int op1 = operandStack.pop()-'0';
int op2 = operandStack.pop()-'0';
cout << "op1 : " << op1 << endl;
//op1 ~operator~ op2
switch(x)
{
case '+':
result = op2 + op1;
break;
case '-':
result = op2 - op1;
break;
case '/':
result = op2 / op1;
break;
case '*':
result = op2 * op1;
break;
default:
cout << " Invalid Operator " << x<<endl;
return 0;
}
operandStack.push(result);
}
}
result = operandStack.pop();
return result;
}

Stack::Stack()
{
top = -1;
}
void Stack::push(char item)
{
cstack.push_back(item);
top++;
}
bool Stack::empty()
{
if (top == -1) return true;
else return false;
}
char Stack::pop()
{
try {
if (cstack.size()==0) throw 0;
} catch (int err) {
//cout << "stack is empty." << endl;
//cout << "Cannot pop an item."<< endl;
return false;
}
char val = cstack[top];
cstack.pop_back();
top--;
return val;
}

int main()
{
cout<<evaluatePostfix("23+");
}