fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define tatlem "test"
  4. #define MOD 2021
  5. #define enl endl
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. ios_base::sync_with_stdio(false);
  11. cout.tie(0);
  12. cin.tie(0);
  13. freopen("test.inp","r",stdin);
  14. freopen("test.out","w",stdout);
  15. int t;
  16. cin >> t;
  17. while(t--)
  18. {
  19. string s;
  20. cin >> s;
  21. stack<char> st;
  22. for (char c : s)
  23. {
  24. if (isalnum(c))
  25. {
  26. cout << c;
  27. }
  28. else if (c == '(')
  29. {
  30. st.push(c);
  31. }
  32. else if (c == ')')
  33. {
  34. while (!st.empty() && st.top() != '(')
  35. {
  36. cout << st.top();
  37. st.pop();
  38. }
  39. if (!st.empty() && st.top() == '(')
  40. {
  41. st.pop();
  42. }
  43. }
  44. else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^')
  45. {
  46. int gt;
  47. if (c == '+' || c == '-') gt = 1;
  48. else if (c == '*' || c == '/') gt = 2;
  49. else if (c == '^') gt = 3;
  50. while (!st.empty())
  51. {
  52. int gtt;
  53. if (st.top() == '+' || st.top() == '-') gtt = 1;
  54. else if (st.top() == '*' || st.top() == '/') gtt = 2;
  55. else if (st.top() == '^') gtt = 3;
  56. if (st.top() != '(' && gtt >= gt)
  57. {
  58. cout << st.top();
  59. st.pop();
  60. }
  61. else
  62. {
  63. break;
  64. }
  65. }
  66. st.push(c);
  67. }
  68. }
  69. while (!st.empty())
  70. {
  71. cout << st.top();
  72. st.pop();
  73. }
  74. cout << enl;
  75. }
  76. return 0;
  77. }
  78.  
Success #stdin #stdout 0.01s 5268KB
stdin
Standard input is empty
stdout
Standard output is empty