fork download
  1. #include <iostream>
  2. #include <stack>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. bool hasHigherPreference(char s_top, char c){
  8.  
  9. if ((c == '*' || c== '/') && (s_top == '+' || s_top == '-'))
  10. return true;
  11.  
  12. return false;
  13. }
  14.  
  15. bool isOperator(char c){
  16.  
  17. if (c == '+' || c == '-' || c == '*' || c== '/')
  18. return true;
  19.  
  20.  
  21. return false;
  22.  
  23. }
  24. int main() {
  25. int t;
  26. char c;
  27. stack<char> s;
  28. string postfix = "";
  29.  
  30. s.push('(');
  31. s.push('+');
  32. while(s.top() != '('){
  33. s.pop();
  34. }
  35. s.pop();
  36. cout << s.empty() << endl;
  37. return 0;
  38. cin >> t;
  39. cin.ignore(1000, '\n');
  40.  
  41. while (t--) {
  42. cin.ignore(1000, '\n');
  43.  
  44. while (cin.peek() != '\n' && cin.peek() != -1) {
  45. cin >> c;
  46. cin.ignore(100, '\n');
  47.  
  48. if (c == '(')
  49. s.push(c);
  50. else if(c==')'){
  51. while(s.top() != '('){
  52. postfix += s.top();
  53. s.pop();
  54. }
  55. s.pop();
  56. } else if (!isOperator(c)) {
  57. postfix += c;
  58. } else if (s.empty() || hasHigherPreference(s.top(),c)){
  59. s.push(c);
  60. } else {
  61. postfix += s.top();
  62. s.pop();
  63. s.push(c);
  64. }
  65. }
  66.  
  67. while(!s.empty()){
  68. postfix += s.top();
  69. s.pop();
  70. }
  71.  
  72.  
  73.  
  74. cout << postfix << endl;
  75. }
  76. }
Success #stdin #stdout 0s 4272KB
stdin
Standard input is empty
stdout
1