Advertisement
STANAANDREY

AOCMMXX d18p2

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