c++ - Infix to Postfix - Deleting Parenthesis -
having trouble getting parenthesis pop string. enter infix expression such (a+b)*c , ab+c*. instead, (ab)+c*. appreciated.
string postfx::converttopostfix(postfx c,string in) { stack<char> s; string postfx = ""; (int = 0; i<in.length();i++) { if (in[i] == ' ' || in[i] == ',') continue; else if ((in[i]) == '+' || in[i] == '-' || in[i] == '/' || in[i] == '*') { while (!s.empty() && s.top() != '(' && c.precedence(s.top(), in[i])) { postfx += s.top(); s.pop(); } s.push(in[i]); } else if ((in[i]) != '+' || in[i] != '-' || in[i] != '/' || in[i] != '*') { postfx += in[i]; } else if (in[i] == '(') { s.push(in[i]); } else if (in[i] == ')') { while (!s.empty() && s.top() != '(') { postfx += s.top(); s.pop(); } s.pop(); } } while (!s.empty()) { postfx += s.top(); s.pop(); } return postfx; }
i think your
else if ((in[i]) != '+' || in[i] != '-' || in[i] != '/' || in[i] != '*') line catches brackets well,
else if (in[i] == '(') and below never gets executed.
i think should move
else if ((in[i]) != '+' || in[i] != '-' || in[i] != '/' || in[i] != '*') to last option in chained if.
moreover, if catches absolutely any symbol (because symbol either not equal '+', or not equal '-'). need replace || &&; if anyway have if last in chained if, need no condition there @ all, like:
if ((in[i] == ' ')|| ... else if ((in[i] == '+')|| ... else if (in[i] == '(') ... else if (in[i] == ')') ... else postfx += in[i]; // no condition here p.s. if wrap initial expression in brackets:
in = "(" + in + ")" before loop, not need final while (!s.empty()) loop.
Comments
Post a Comment