fork download
  1.  
  2.  
  3. // Saif Khan - 21/03/2017
  4.  
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. #define rd freopen("in.txt", "r", stdin)
  10. #define wr freopen("out.txt", "w", stdout)
  11. #define ios ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
  12. #define mset(x, v) memset(x, v, sizeof(x))
  13. #define FOR(n) for (int i = 0; i < n; i++)
  14.  
  15. using namespace std;
  16.  
  17. vector<string> split(string s) {
  18. string tnum = "";
  19. vector<string> v;
  20. for (int i = 0; i < s.size(); i++) {
  21. if (s[i] >= '0' && s[i] <= '9') {
  22. tnum += s[i];
  23. } else {
  24. if (tnum != "") {
  25. if (v.back() == "-" && v.size() - 2 >= 0 && v[v.size() - 2] == "(") {
  26. v.pop_back();
  27. tnum = "-" + tnum;
  28. }
  29. v.push_back(tnum);
  30. tnum = "";
  31. }
  32. string ts = "";
  33. ts += s[i];
  34. v.push_back(ts);
  35. }
  36. }
  37. return v;
  38. }
  39.  
  40. bool check(string s) { return s == "+" || s == "-" || s == "*" || s == "/"; }
  41.  
  42. bool checkPriority(string a, string b) {
  43. int aa = -1;
  44. int bb = -1;
  45. if (a == "+" || a == "-")
  46. aa = 1;
  47. else if (a == "*" || a == "/")
  48. aa = 2;
  49. if (b == "+" || b == "-")
  50. bb = 1;
  51. else if (b == "*" || b == "/")
  52. bb = 2;
  53. return aa >= bb;
  54. }
  55.  
  56. vector<string> infixToPostfix(vector<string> v) {
  57. stack<string> st;
  58. vector<string> postfix;
  59. for (int i = 0; i < v.size(); i++) {
  60. if (check(v[i])) {
  61. while (!st.empty() && st.top() != "(" && checkPriority(st.top(), v[i])) {
  62. postfix.push_back(st.top());
  63. st.pop();
  64. }
  65. st.push(v[i]);
  66. } else if (v[i] == "(") {
  67. st.push(v[i]);
  68. } else if (v[i] == ")") {
  69. while (!st.empty() && st.top() != "(") {
  70. postfix.push_back(st.top());
  71. st.pop();
  72. }
  73. st.pop();
  74. } else {
  75. postfix.push_back(v[i]);
  76. }
  77. }
  78. while (!st.empty()) {
  79. postfix.push_back(st.top());
  80. st.pop();
  81. }
  82. return postfix;
  83. }
  84.  
  85. double mathOperation(double t1, double t2, string s) {
  86. if (s == "*") return t2 * t1;
  87. if (s == "/") return t2 / t1;
  88. if (s == "+") return t2 + t1;
  89. if (s == "-") return t2 - t1;
  90. return 0;
  91. }
  92.  
  93. double toDouble(string s) {
  94. stringstream ss;
  95. ss << s;
  96. double d;
  97. ss >> d;
  98. return d;
  99. }
  100.  
  101. string toString(double d) {
  102. stringstream ss;
  103. ss << d;
  104. return ss.str();
  105. }
  106.  
  107. string postfixEvaluation(vector<string> v) {
  108. stack<double> s;
  109. double d;
  110. for (int i = 0; i < v.size(); i++) {
  111. if (check(v[i])) {
  112. double d1 = s.top();
  113. if (d1 == 0.0) {
  114. return "Infinity";
  115. }
  116. s.pop();
  117. double d2 = s.top();
  118. s.pop();
  119. d = mathOperation(d1, d2, v[i]);
  120. s.push(d);
  121. } else {
  122. s.push(toDouble(v[i]));
  123. }
  124. }
  125. return toString(d);
  126. }
  127.  
  128. string calculate(string s) {
  129. vector<string> v;
  130. v = split(s);
  131. v = infixToPostfix(v);
  132. s = postfixEvaluation(v);
  133. return s;
  134. }
  135.  
  136. int main() {
  137. ios;
  138. int n;
  139. cin >> n;
  140. string s;
  141. while (n--) {
  142. cin >> s;
  143. string sum = calculate(s);
  144. if (sum != "Infinity") {
  145. cout << fixed << setprecision(1) << toDouble(sum) << '\n';
  146. } else {
  147. cout << sum << '\n';
  148. }
  149. }
  150. return 0;
  151. }
Success #stdin #stdout 0s 16080KB
stdin
8
(310+14)
(9-((-5)/2))
((123+(4*5))-7)
((9-((5/(8-3))*2))+6)
(5+(4/(2+(-2))))
((3+((3*(5+4))/2))-7)
((14-5)/(9-6))
((-6)+((7*2)/14))
stdout
324.0
11.5
136.0
13.0
Infinity
9.5
3.0
-5.0