Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. apsed: 90:35:07RunningRemaining: 05:24:52
  2. Overview
  3. Problem
  4. Status
  5. Rank (90:35:03)
  6. 0 Comments
  7. Previous12345…Next
  8. Username
  9. Prob
  10. Result
  11. Time
  12. (ms) Mem
  13. (MB) Lang
  14. Submit Time
  15. Rat_rex
  16. A
  17. Accepted
  18. 40
  19. C++
  20. 2 days ago
  21.  
  22. All Copyright Reserved Β© 2010-2020 Xu Han
  23. Server Time: 2020-01-26 12:35:08 BST
  24.  
  25. #23829771 | Rat_rex's solution for [Problem A]
  26. Status
  27. Accepted
  28. Time
  29. 40ms
  30. Length
  31. 1414
  32. Lang
  33. C++11 5.3.0
  34. Submitted
  35. 2020-01-24 23:30:23
  36. Shared
  37.  
  38. Select Code
  39. #include <bits/stdc++.h>
  40. using namespace std;
  41.  
  42. int Priority(char c)
  43. {
  44. if (c == '(')
  45. return 1;
  46. if (c == '+' or c == '-')
  47. return 2;
  48. if (c == '*' or c == '/')
  49. return 3;
  50. }
  51.  
  52. int main()
  53. {
  54. int t;
  55. int count = 0;
  56. cin >> t;
  57. cin.ignore();
  58. cin.ignore();
  59.  
  60. while (t--)
  61. {
  62. string ss = "";
  63.  
  64. string str;
  65. stack<char> st;
  66.  
  67. while (1)
  68. {
  69. getline(cin, str);
  70.  
  71. char ch = str[0];
  72.  
  73. if (str.size() == 0 || !cin)
  74. break;
  75.  
  76. if (isdigit(ch))
  77. {
  78. ss += ch;
  79. }
  80. else if (ch == '+' or ch == '-' or ch == '/' or ch == '*')
  81. {
  82. while (!st.empty() and Priority(ch) <= Priority(st.top()))
  83. {
  84. ss += st.top();
  85. st.pop();
  86. }
  87.  
  88. st.push(ch);
  89. }
  90.  
  91. else if (ch == ')')
  92. {
  93. while (st.top() != '(')
  94. {
  95. ss += st.top();
  96. st.pop();
  97. }
  98. st.pop();
  99. }
  100.  
  101. else if (ch == '(')
  102. st.push('(');
  103. }
  104.  
  105. while (!st.empty())
  106. {
  107. ss += st.top();
  108. st.pop();
  109. }
  110.  
  111. cout << ss << endl;
  112. if (t)
  113. cout << endl;
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement