fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3.  
  4. using namespace std;
  5.  
  6. int pri(char c){
  7. if(c == '+' || c == '-') return 1;
  8. else if(c == '*' || c == '/') return 2;
  9. else if(c == '^') return 3;
  10. return 0;
  11. }
  12.  
  13. void change(string s){
  14. stack<char> st;
  15. string out;
  16. for(int i = 0; i < s.size(); i++){
  17. if(s[i] == '(') st.push(s[i]);
  18. else if(isalnum(s[i])) out += s[i];
  19. else if(s[i] == ')'){
  20. while(!st.empty() && st.top() != '('){
  21. char c = st.top();
  22. out += c;
  23. st.pop();
  24. }
  25. st.pop();
  26. }
  27. else{
  28. while(!st.empty() && pri(s[i]) <= pri(st.top())){
  29. char c = st.top();
  30. out += c;
  31. st.pop();
  32. }
  33. st.push(s[i]);
  34. }
  35. }
  36. while(!st.empty()){
  37. char c = st.top();
  38. if(c != '(') out += c;
  39. st.pop();
  40. }
  41. cout << out << endl;
  42. }
  43.  
  44. int main() {
  45. int t; cin >> t;
  46. while(t--){
  47. string s; cin >> s;
  48. change(s);
  49.  
  50. }
  51. return 0;
  52. }
Success #stdin #stdout 0.02s 5324KB
stdin
Standard input is empty
stdout