Advertisement
LunaeStellsr

PreInPostfix

Nov 7th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.39 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long LL;
  6.  
  7. // Return precedence of operator
  8. int op_prec(string op){
  9.     //if(op == "(" || op == ")")return 0x100;
  10.     if(op == "!")return 0x50;
  11.     if(op == "^")return 0x30;
  12.     if(op == "*" || op == "/" || op == "%")return 0x20;
  13.     if(op == "+" || op == "-")return 0x10;
  14.     if(op == "!" || op == ">" || op == "<" || op == ">=" ||
  15.        op == "<=" || op == "=="){return 0x08;}
  16.     if(op[0] == '&' || op[0] == '|' || op == ">>" || op == "<<")return 0x05;
  17.     return 0;
  18. }
  19.  
  20. bool is_operator(string op){
  21.     if(op == "+" || op == "-" || op == "*" || op == "/" ||
  22.        op == "%" || op == "^" || op == "!" || op == "&" ||
  23.        op == "&&" || op == "|" || op == "||" || op == ">" ||
  24.        op == ">" || op == ">=" || op == "<=" || op == "==" ||
  25.        op == ">>" || op == "<<"){
  26.  
  27.         return true;
  28.     }
  29.     return false;
  30. }
  31.  
  32. bool is_basic_operator(string op){
  33.     if(op == "+" || op == "-" || op == "*" || op == "/" ||
  34.        op == "%" || op == "^"){return true;}
  35.     return false;
  36. }
  37.  
  38. string infixToPostfix(string str){
  39.     str = "( " + str + " )";
  40.     stringstream ss(str);
  41.     string op, re = "";
  42.     stack<string> op_stack;
  43.     while(ss >> op){
  44.         //cout << op << ' ' << op.length() << '\n';
  45.         if(isdigit(op[0]) || isalpha(op[0])){
  46.             re += op;
  47.             re += ' ';
  48.         }
  49.         else if(op == "("){
  50.             op_stack.push("(");
  51.         }
  52.         else if(op == ")"){
  53.             while(op_stack.top() != "("){
  54.                 re += op_stack.top();
  55.                 re += ' ';
  56.                 op_stack.pop();
  57.             }
  58.             op_stack.pop();
  59.         }
  60.         else{
  61.             while(op_prec(op) <= op_prec(op_stack.top()) ){
  62.                 re += op_stack.top();
  63.                 re += ' ';
  64.                 op_stack.pop();
  65.             }
  66.             op_stack.push(op);
  67.         }
  68.     }
  69.     return re;
  70. }
  71.  
  72. map<string, int> dict;
  73.  
  74. int calc_postfix(string str){
  75.     stringstream ss(str);
  76.     stack<int> st;
  77.     while(ss >> str){
  78.         if(!is_basic_operator(str) && !isdigit(str[0])){
  79.             return 0x80000000;
  80.         }
  81.         if(isdigit(str[0])){
  82.             st.push(atoi(str.c_str()));
  83.         }
  84.         else if(str[0] == '+'){
  85.             int b = st.top(); st.pop();
  86.             int a = st.top(); st.pop();
  87.             st.push(a + b);
  88.         }
  89.         else if(str[0] == '-'){
  90.             int b = st.top(); st.pop();
  91.             int a = st.top(); st.pop();
  92.             st.push(a - b);
  93.         }
  94.         else if(str[0] == '*'){
  95.             int b = st.top(); st.pop();
  96.             int a = st.top(); st.pop();
  97.             st.push(a * b);
  98.         }
  99.         else if(str[0] == '/'){
  100.             int b = st.top(); st.pop();
  101.             int a = st.top(); st.pop();
  102.             st.push(a / b);
  103.         }
  104.         else if(str[0] == '%'){
  105.             int b = st.top(); st.pop();
  106.             int a = st.top(); st.pop();
  107.             st.push(a % b);
  108.         }
  109.         else if(str[0] == '^'){
  110.             int b = st.top(); st.pop();
  111.             int a = st.top(); st.pop();
  112.             st.push(pow(a,b));
  113.         }
  114.     }
  115.     return st.top();
  116. }
  117.  
  118. // Convert postfix to Prefix expression
  119. string postfixToPrefix(string str) {
  120.     stack<string> st;
  121.     stringstream ss(str);
  122.     while(ss >> str){
  123.         if(!is_basic_operator(str) && !isdigit(str[0])){
  124.             return "Not supported";
  125.         }
  126.         if(is_operator(str)){
  127.             string b = st.top(); st.pop();
  128.             string a = st.top(); st.pop();
  129.             st.push(str + ' ' + a + ' '+ b + ' ');
  130.         }
  131.         else{st.push(str);}
  132.     }
  133.     return st.top();
  134. }
  135.  
  136. bool process_input(string &str){
  137.     cout << "Infix: ";
  138.     bool cont = getline(cin, str);
  139.     return cont;
  140. }
  141.  
  142. int main(){
  143.     string str;
  144.     while(process_input(str)){
  145.         string result  = infixToPostfix(str);
  146.         string result2 = postfixToPrefix(result);
  147.         cout << "Postfix: " << result << '\n';
  148.         cout << "Prefix: " << result2 << '\n';
  149.         cout << "Result: ";
  150.         int c = calc_postfix(result);
  151.         if(c == 0x80000000){cout << "Not supported\n";}
  152.         else{cout << c << '\n';}
  153.         cout << "---------------\n";
  154.     }
  155.     return 0;
  156. }
  157. /*
  158. 2 + 3 * 4
  159. 2 * ( 3 + 4 ) * 5 + 2
  160. 5 * 6 + 7
  161. ! ( A && ! ( ( B < C ) || ( C > D ) ) ) || ( C >> 2 + 2 > E )
  162.  
  163. 234*+
  164. 234+*5*2+
  165. 56*7+
  166. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement