简体   繁体   中英

Infix to Postfix notation not respecting second set of parentheses

Having trouble getting the correct outcome of Infix: (A+B)/(CD) Postfix: AB+CD-/

I keep getting Postfix: AB+C/D-

I do know that the issue is coming from it not being able to pop the last operators from the stack before pushing '(' This is why I added the if statement in the first else if condition. That also doesn't work. What is it exactly that I am doing wrong? Is there another way into tackling this problem?

#include <iostream>
#include <stack>
#include <sstream>
#include <string>
using namespace std;

int precedence(char x) {
    int op;
    if (x == '(' || x==')')
        op = 1;
    else if (x == '^')
        op = 2;
    else  if (x == '*')
        op = 3;
    else  if ( x == '/')
        op = 4;
    else  if (x == '+')
        op = 5;
    else  if (x == '-')
        op = 6;
    return op;
}

int main() 
{
    string getInfix;
    cout << "Infix: ";
    getline(cin, getInfix);
    stack<char> opStack;
    stringstream showInfix;


    for (unsigned i = 0; i < getInfix.length(); i++) 
    {
        if (getInfix[i] == '+' || getInfix[i] == '-' || getInfix[i] == '*' || getInfix[i] == '/'  || getInfix[i] == '^') 
        {
            while (!opStack.empty() && precedence(opStack.top() <= precedence(getInfix[i])) 
            {
                showInfix << opStack.top();
                opStack.pop();
            }
            opStack.push(getInfix[i]);
        }
        else if (getInfix[i] == '(') 
        {
            opStack.push(getInfix[i]);
            opStack.pop();

            if (getInfix[i]=='(' && !opStack.empty()) 
            {
                opStack.push(getInfix[i]);
                opStack.pop();
            }
        }        
        else if (getInfix [i]==')') 
        {                   
          showInfix << opStack.top();
          opStack.pop();
        }
        else 
        {
            showInfix << getInfix[i];
        }
    }

    while (!opStack.empty()) 
    {
        showInfix << opStack.top();
        opStack.pop();
    }

    cout << "Postfix: "<<""<<showInfix.str() << endl;

    cin.ignore ( numeric_limits< streamsize >:: max(),'\n');
    return 0;
}

You didn't set op

const int precedence(const char x) noexcept(true) {
  switch (x) {
    case '(': case ')':
      return 1;
    case '^':
      return 2;
    case '*':
      return 3;
    case '/':
      return 4;
    case '+':
      return 5;
    case '-':
      return 6;
  }  
  return -1;
}

It returns -1 but I'll let you figure that part out. It doesn't answer the question. I just stopped after I saw you could be reading garbage values.

The problem comes from this line (!opStack.empty() && precedence(opStack.top() <=precedence(getInfix[i]))

You are popping the last operator you found without checking if you are in a parenthesis statement or not. You need to take parentheses characters into account before adding an operator to the output string.

Not related to your problem but some advices :

  • indent your code, simplifies visibility and trust me, saves you (and us) time.
  • Do not push and then pop for ( or ) characters, this is just like ignoring them.
  • You are missing a ) on this line I imagine it's a copy/paste problem : while (!opStack.empty() && precedence(opStack.top() <=precedence(getInfix[i]))
  • You do realize you test precedence for ( and ) but you are never actually calling that method with that type of character?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM