Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define f first
  7. #define s second
  8. #define pb push_back
  9. #define ll long long
  10. #define mp make_pair
  11.  
  12. int n;
  13. vector<char> oper;
  14. vector<int> val;
  15.  
  16. inline void operation() {
  17. char c = oper.back();
  18. oper.pop_back();
  19. int b = val.back();
  20. val.pop_back();
  21. int a = val.back();
  22. val.pop_back();
  23. if (c == '*') {
  24. val.pb(a * b);
  25. }
  26. if (c == '/') {
  27. val.pb(a / b);
  28. }
  29. if (c == '+') {
  30. val.pb(a + b);
  31. }
  32. if (c == '-') {
  33. val.pb(a - b);
  34. }
  35. //cerr << c << ' ' << val.back() << endl;
  36. }
  37.  
  38. int main() {
  39. freopen("evalhard.in", "r", stdin);
  40. freopen("evalhard.out", "w", stdout);
  41. ios_base::sync_with_stdio(0);
  42. cin.tie(0);
  43. string s;
  44. cin >> s;
  45. s = s + ')';
  46. int n = s.size();
  47. oper.pb('(');
  48. for (int i = 0; i < n; ++i) {
  49. if ('0' <= s[i] && s[i] <= '9') {
  50. int v = s[i] - '0';
  51. while ('0' <= s[i + 1] && s[i + 1] <= '9') {
  52. ++i;
  53. v *= 10;
  54. v += s[i] - '0';
  55. }
  56. val.pb(v);
  57. }
  58. else {
  59. if (s[i] != ')') {
  60. if (s[i] != '(') {
  61. while (oper.back() != '(' && (oper.back() == '/' || oper.back() == '*' || s[i] == '+' || s[i] == '-')) {
  62. operation();
  63. }
  64. }
  65. oper.pb(s[i]);
  66. }
  67. else {
  68. while (oper.back() != '(') {
  69. operation();
  70. }
  71. oper.pop_back();
  72. }
  73. }
  74. }
  75. assert(val.size() == 1);
  76. assert(oper.size() == 0);
  77. cout << val.back();
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement