Hi, prime_tang. Your code is really well-written. I have understood it and rewritten it below. I hope it is OK.

class Solution {
public:
int calculate(string s) {
int i = 0, res = 0, sign = 1;
int num = extract_num(s, i);
while (i < (int)s.length()) {
if (s[i] == '+' || s[i] == '-') {
char op = s[i];
res += num * sign;
num = extract_num(s, ++i);
sign = (op == '+' ? 1 : -1);
}
else if (s[i] == '*')
num *= extract_num(s, ++i);
else if (s[i] == '/')
num /= extract_num(s, ++i);
}
res += num * sign;
return res;
}
private:
int extract_num(string& s, int &i) {
int num = 0;
while (i < (int)s.length()) {
if (isdigit(s[i])) num = num * 10 + s[i] - '0';
else if (s[i] != ' ') return num;
i++;
}
return num;
}
};