class Solution {
public:intcalculate(string str){
str+='+';
stack<int> stk;int num =0;int n = str.length();
char c ='+';for(int i =0; i < n; i++){if(isdigit(str[i])){
num = num*10-'0'+ str[i];continue;}elseif(str[i]==' ')continue;switch(c){case'+': stk.push(num);break;case'-': stk.push(-num);break;case'*': num *= stk.top(), stk.pop(), stk.push(num);break;case'/': num = stk.top()/num, stk.pop(), stk.push(num);break;}
c = str[i];
num =0;}int res =0;while(stk.size()){
res += stk.top();
stk.pop();}return res;}};