Advertisement
STANAANDREY

AOCMMXX d18p1

Dec 18th, 2020
1,100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. unsigned it;
  4. string s;
  5.  
  6. int64_t eval() {
  7.  
  8.     int64_t r = 0;
  9.     char op = '+';
  10.     while (it < s.size() && s[it] != ')') {
  11.         if (strchr("+*", s[it])) {
  12.             op = s[it];
  13.             it++;
  14.             continue;
  15.         }
  16.         if (s[it] == ' ') {
  17.             it++;
  18.             continue;
  19.         }
  20.         int64_t nr = 0;
  21.         bool dig = false;
  22.         while (isdigit(s[it])) {
  23.             nr = nr * 10 + s[it] - '0';
  24.             it++;
  25.             dig = true;
  26.         }
  27.         if (s[it] == '(') {
  28.             it++;
  29.             dig = true;
  30.             nr = eval();
  31.             it++;
  32.         }
  33.         if (!dig)
  34.             continue;
  35.         if (op == '+')
  36.             r += nr;
  37.         else
  38.             r *= nr;
  39.     }
  40.     return r;
  41. }
  42.  
  43. int main()
  44. {
  45.     freopen("text.in", "r", stdin);
  46.     int64_t r = 0;
  47.     while (getline(cin, s)) {
  48.         it = 0;
  49.         r += 1LL * eval();
  50.     }
  51.     cout << r << endl;
  52.     return 0;
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement