Made an infix to postfix converter and calculator using an array based stack in c++. The calculations don't work. Only returns the top of the stack

125 views Asked by At

My problem is that my evaluatePostfix() function is not calculating operands. It only returns the top of the stack in the converted postfix expression.

So for example, my output is:

Enter a infix expression: 1+4
your postfix expression is: 
14+

your result is: 
4

Why am I not getting 5?

Here's my main():

int main()
{
      
      string infixExp = "";
      cout << "Enter a infix expression: ";
      cin >> infixExp;

      cout << "your postfix expression is: " << endl;
      infixToPostfix(infixExp);

      cout << endl;

      cout << "your result is: " << endl;
      cout << evaluatePostfix(infixExp) << endl;
}

Here's evaluatePostfix():


int evaluatePostfix(string expression)
{
      ArrayStack<int> S;

      for (int i = 0; i < expression.length(); i++)
      {
            if (expression[i] == ' ' || expression[i] == ',') continue;

            else if(IsOperator(expression[i]))
            {
                  int operand2 = S.peek(); 
                  S.pop();
                  int operand1 = S.peek(); 
                  S.pop();

                  int result = PerformOperation(expression[i], operand1, operand2);

                  S.push(result);
            }

            else if(IsNumericDigit(expression[i]))
            {
                  int operand = 0;

                  while (i < expression.length() && IsNumericDigit(expression[i]))
                  {
                        operand = operand * 10 + expression[i] - '0';
                        i++;
                  }

                  i--;

                  S.push(operand);
            }
      }
      return S.peek();
}

Here's PerformOperation(), IsOperator(), IsNumericDigit(). These are all in evaluatePostfix.

bool IsNumericDigit(char C)
{
      if (C >= '0' && C <= '9')
      {
            return true;
      }

      else 
      {
            return false;
      }
}

bool IsOperator(char C)
{
      if (C == '+' || C == '-' || C == '*' || C == '/')
      {
            return true;
      }

      else 
      {
            return false;
      }
}

int PerformOperation(char operation, int operand1, int operand2)
{
      if (operation == '+')
      {
            return operand1 + operand2;
      }

      else if (operation == '-')
      {
            return operand1 - operand2;
      }

      else if (operation == '*')
      {
            return operand1 * operand2;
      }

      else if (operation == '/')
      {
            return operand1 / operand2;
      }

      else 
      {
            cout << "error" << endl;
      }

      return -1;
}

Lastly, here is infixToPostfix():

void infixToPostfix(string s)
{
       ArrayStack<char> stackPtr;

       string postfixExp;

       for (int i = 0; i < s.length(); i++)
       {
            char ch = s[i];

            if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9'))
            {
                  postfixExp += ch;
            }

            else if (ch == '(')
            {
                  stackPtr.push('(');
            }

            else if (ch == ')')
            {
                  while(stackPtr.peek() != '(')
                  {
                        postfixExp += stackPtr.peek();
                        stackPtr.pop();
                  }
                  stackPtr.pop();
            }

            else 
            {
                  while (!stackPtr.isEmpty() && prec(s[i]) <= prec(stackPtr.peek()))
                  {
                        postfixExp += stackPtr.peek();
                        stackPtr.pop();
                  }
                  stackPtr.push(ch);
            }
       }

       while (!stackPtr.isEmpty())
       {
            postfixExp += stackPtr.peek();
            stackPtr.pop();
       }
       
       cout << postfixExp << endl;
}

0

There are 0 answers