fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <stack>
  5. #include <sstream>
  6. using namespace std;
  7. int main(){
  8. int t;
  9. cin >> t;
  10. cin.ignore();
  11. while (t--){
  12. string tokens;
  13. cin >> tokens;
  14. stack<string> st;
  15. for (int i = 0; i <= tokens.size() - 1; i++) {
  16. if (tokens[i] == '+') {
  17. string a = st.top(); st.pop();
  18. string b = st.top(); st.pop();
  19. st.push("(" + b + "+" + a + ")");
  20. }
  21. else if (tokens[i] == '-') {
  22. string a = st.top(); st.pop();
  23. string b = st.top(); st.pop();
  24. st.push("(" + b + "-" + a + ")");
  25. }
  26. else if (tokens[i] == '*') {
  27. string a = st.top(); st.pop();
  28. string b = st.top(); st.pop();
  29. st.push("(" + b + "*" + a + ")");
  30. }
  31. else if (tokens[i] == '/') {
  32. string a = st.top(); st.pop();
  33. string b = st.top(); st.pop();
  34. st.push("(" + b + "/" + a + ")");
  35. }
  36. else {
  37. st.push(string(1,tokens[i]));
  38. }
  39. }
  40. if (st.size() == 1) {
  41. cout << st.top() << endl;
  42. }
  43. }
  44.  
  45. }
  46.  
  47.  
Success #stdin #stdout 0.01s 5272KB
stdin
2
ab+ef*g*-
wlrb+-*
stdout
((a+b)-((e*f)*g))
(w*(l-(r+b)))