Ciemny_Cygan

O2P

Sep 17th, 2020 (edited)
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. string PostfixToInfix();
  6. int n,m;
  7.  
  8. string PostfixToInfix(int m){
  9.  
  10.         char c;
  11.         string a="", b="", oper="", out="";
  12.         stack<string> stk;
  13.  
  14.          for (int j = 0; j < m + 1; j++) {
  15.             c = cin.get();
  16.  
  17.             switch(c) {
  18.                 case ' ':
  19.                     stk.push(oper);
  20.                     oper = "";
  21.                     break;
  22.  
  23.                 case '+' : case '-' : case '*' : case '/' : case '%' :
  24.                     if (oper != "") {
  25.                         stk.push(oper);
  26.                         oper = "";
  27.                     }
  28.  
  29.                     b = stk.top();
  30.                     stk.pop();
  31.                     a = stk.top();
  32.                     stk.pop();
  33.  
  34.                     oper = "(" + a + c + b + ")";
  35.                     stk.push(oper);
  36.                     oper = "";
  37.                     break;
  38.  
  39.                 default:
  40.                     oper += c;
  41.             }
  42.         }
  43.  
  44.         out = stk.top();
  45.         stk.pop();
  46.  
  47. out = out.substr(1, out.length()-2) + '\n';
  48. return out;
  49.  
  50. }
  51.  
  52. int main () {
  53.     ios_base::sync_with_stdio(0);
  54.  
  55.     cin >> n;
  56.     int tab[n];
  57.     string tab2[n];
  58.  
  59.     for (int i = 0; i < n; i++) {
  60.             cin >> tab[i];
  61.             m = tab[i];
  62.             tab2[i] = PostfixToInfix(m);
  63.     }
  64.  
  65.     for (int j = 0; j < n; j++) {
  66.             cout << tab2[j];
  67.     }
  68.     return 0;
  69. }
Add Comment
Please, Sign In to add comment