Advertisement
selebry

SFSBHOIFGSHIUFHSOPFoiwsnlgtosHoighdfjklyeroiio

Apr 19th, 2022
1,022
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.92 KB | None | 0 0
  1. //#include<iostream>
  2. //#include<stack>
  3. //#include<locale>      //for function isalnum()
  4. //using namespace std;
  5. //
  6. //short privilege(char ch) {
  7. //    if (ch == '+' || ch == '-') {
  8. //        return 1;              
  9. //    }
  10. //    if (ch == '*' || ch == '/') {
  11. //        return 2;            
  12. //    }
  13. //    if (ch == '^') {
  14. //        return 3;            
  15. //    }
  16. //    return 0;
  17. //}
  18. //
  19. //string inToPost(string infix) {
  20. //    stack<char> stk;
  21. //                  //add some extra character to avoid underflow
  22. //    string postfix = "";         //initially the postfix string is empty
  23. //    string::iterator it;
  24. //
  25. //    for (it = infix.begin(); it != infix.end(); it++) {
  26. //        if (isalnum(char(*it)))
  27. //            postfix += *it;      //add to postfix when character is letter or number
  28. //        else if (*it == '(')
  29. //            stk.push('(');
  30. //        else if (*it == '^')
  31. //            stk.push('^');
  32. //        else if (*it == ')') {
  33. //            while (!stk.empty() && stk.top() != '(') {
  34. //                postfix += stk.top(); //store and pop until ( has found
  35. //                stk.pop();
  36. //            }
  37. //            stk.pop();          //remove the '(' from stack
  38. //        }
  39. //        else {
  40. //            if (stk.empty()||privilege(*it) > privilege(stk.top()))
  41. //                stk.push(*it); //push if precedence is high
  42. //            else {
  43. //                while (!stk.empty() && privilege(*it) <= privilege(stk.top())) {
  44. //                    postfix += stk.top(); //store and pop until higher precedence is found
  45. //                    stk.pop();
  46. //                }
  47. //                stk.push(*it);
  48. //            }
  49. //        }
  50. //    }
  51. //
  52. //    while (!stk.empty()) {
  53. //        postfix += stk.top(); //store and pop until stack is not empty.
  54. //        stk.pop();
  55. //    }
  56. //
  57. //    return postfix;
  58. //}
  59. //
  60. //int main() {
  61. //    string infix = "A+B/C*(D-A)^F^H";
  62. //    cout << "Postfix Form Is: " << inToPost(infix) << endl;
  63. //}
  64. #include<iostream>
  65. #include<stack>
  66. #include<locale>      //for function isalnum()
  67. using namespace std;
  68.  
  69. int preced(char ch) {
  70.  
  71.     if (ch == '+' || ch == '-') {
  72.         return 1;              //Precedence of + or - is 1
  73.     }
  74.     else if (ch == '*' || ch == '/') {
  75.         return 2;            //Precedence of * or / is 2
  76.     }
  77.     else if (ch == '^') {
  78.         return 3;            //Precedence of ^ is 3
  79.     }
  80.     else {
  81.         return 0;
  82.     }
  83. }
  84.  
  85. string inToPost(string infix) {
  86.     stack<char> stk;
  87.     stk.push('#');               //add some extra character to avoid underflow
  88.     string postfix = "";         //initially the postfix string is empty
  89.     string::iterator it;
  90.  
  91.     for (it = infix.begin(); it != infix.end(); it++) {
  92.         if (isalnum(char(*it)))
  93.             postfix += *it;      //add to postfix when character is letter or number
  94.         else if (*it == '(')
  95.             stk.push('(');
  96.         else if (*it == '^')
  97.             stk.push('^');
  98.         else if (*it == ')') {
  99.             while (stk.top() != '#' && stk.top() != '(') {
  100.                 postfix += stk.top(); //store and pop until ( has found
  101.                 stk.pop();
  102.             }
  103.             stk.pop();          //remove the '(' from stack
  104.         }
  105.         else {
  106.             if (preced(*it) > preced(stk.top()))
  107.                 stk.push(*it); //push if precedence is high
  108.             else {
  109.                 while (stk.top() != '#' && preced(*it) <= preced(stk.top())) {
  110.                     postfix += stk.top(); //store and pop until higher precedence is found
  111.                     stk.pop();
  112.                 }
  113.                 stk.push(*it);
  114.             }
  115.         }
  116.     }
  117.  
  118.     while (stk.top() != '#') {
  119.         postfix += stk.top(); //store and pop until stack is not empty.
  120.         stk.pop();
  121.     }
  122.  
  123.     return postfix;
  124. }
  125.  
  126. int main() {
  127.     string infix = "A+B/C*(D-A)^F^H";
  128.     cout << "Postfix Form Is: " << inToPost(infix) << endl;
  129. }
  130. from pythonds.basic.stack import Stack
  131.  
  132. def infixToPostfix(infixexpr) :
  133.     prec = {}
  134.     prec["^"] = 4
  135.     prec["*"] = 3
  136.     prec["/"] = 3
  137.     prec["+"] = 2
  138.     prec["-"] = 2
  139.     prec["("] = 1
  140.     opStack = Stack()
  141.     postfixList = []
  142.     tokenList = infixexpr.split()
  143.  
  144.     for token in tokenList :
  145. if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789" :
  146.     postfixList.append(token)
  147.     elif token == '(' :
  148.     opStack.push(token)
  149.     elif token == ')' :
  150.     topToken = opStack.pop()
  151.     while topToken != '(' :
  152.         postfixList.append(topToken)
  153.         topToken = opStack.pop()
  154. else:
  155. while (not opStack.isEmpty()) and \
  156. (prec[opStack.peek()] >= prec[token]) :
  157.     postfixList.append(opStack.pop())
  158.     opStack.push(token)
  159.  
  160.     while not opStack.isEmpty() :
  161.         postfixList.append(opStack.pop())
  162.         return " ".join(postfixList)
  163.  
  164.         print(infixToPostfix("A + B / C * ( D - A ) ^ F ^ H "))
  165.         print(infixToPostfix("( A + B ) * C - ( D - E ) * ( F + G )"))
  166.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement