Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. string convertPostfix(string infix) {
  2. string postFix;
  3. stack<char> numStack;
  4.  
  5. for (int index = 0; index < infix.length(); index++) {
  6. if (isdigit(infix.at(index))) {
  7. postFix += (infix.at(index));
  8. }
  9.  
  10. else if (infix.at(index) == '(') {
  11. numStack.push(infix.at(index));
  12. }
  13.  
  14. else if (infix.at(index) == '+' || infix.at(index) == '-' || infix.at(index) == '*' || infix.at(index) == '/') {
  15. if (!numStack.empty()) {
  16. while (!numStack.empty() && (hasHigherPrecedence(numStack.top(), infix.at(index)))) {
  17. postFix += numStack.top();
  18. numStack.pop();
  19. }
  20. numStack.push(infix.at(index));
  21. }
  22. else if (numStack.empty())
  23. numStack.push(infix.at(index));
  24. }
  25. else if (infix.at(index) == ')') {
  26. while (numStack.empty() == false && numStack.top() != '(') {
  27. postFix += numStack.top();
  28. numStack.pop();
  29. }
  30. numStack.pop();
  31. }
  32. }
  33.  
  34. while (!numStack.empty()) {
  35. if (numStack.top() != '(' && numStack.top() != ')') {
  36. postFix += numStack.top();
  37. numStack.pop();
  38. }
  39. else {
  40. numStack.pop();
  41. }
  42. }
  43. return postFix;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement