0227.cpp (465B)
1 class Solution { 2 public: 3 int calculate(string s) { 4 stringstream ss("+" + s); 5 char op; 6 int n, last, ans = 0; 7 while (ss >> op >> n) { 8 if (op == '+' || op == '-') { 9 n = op == '+' ? n : -n; 10 ans += n; 11 } else { 12 n = op == '*' ? last * n : last / n; 13 ans = ans - last + n; 14 } 15 last = n; 16 } 17 return ans; 18 } 19 };