Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string convertPostfix(string infix) {
- string postFix;
- stack<char> numStack;
- for (int index = 0; index < infix.length(); index++) {
- if (isdigit(infix.at(index))) {
- postFix += (infix.at(index));
- }
- else if (infix.at(index) == '(') {
- numStack.push(infix.at(index));
- }
- else if (infix.at(index) == '+' || infix.at(index) == '-' || infix.at(index) == '*' || infix.at(index) == '/') {
- if (!numStack.empty()) {
- while (!numStack.empty() && (hasHigherPrecedence(numStack.top(), infix.at(index)))) {
- postFix += numStack.top();
- numStack.pop();
- }
- numStack.push(infix.at(index));
- }
- else if (numStack.empty())
- numStack.push(infix.at(index));
- }
- else if (infix.at(index) == ')') {
- while (numStack.empty() == false && numStack.top() != '(') {
- postFix += numStack.top();
- numStack.pop();
- }
- numStack.pop();
- }
- }
- while (!numStack.empty()) {
- if (numStack.top() != '(' && numStack.top() != ')') {
- postFix += numStack.top();
- numStack.pop();
- }
- else {
- numStack.pop();
- }
- }
- return postFix;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement