https://leetcode-cn.com/problems/calculator-lcci
class Solution {
public int calculate(String s) {
Stack<Integer> nums=new Stack<>();
Stack<Character> symbol=new Stack<>();
int left=0;
int right=0;
for(int i=0;i<s.length();i++){
StringBuilder str=new StringBuilder();
while(i<s.length()&&s.charAt(i)>='0'&&s.charAt(i)<='9'){
str.append(s.charAt(i));
i++;
}
if(!symbol.isEmpty()&&str.length()>0){
char sym= symbol.peek();
right=Integer.valueOf(str.toString());
switch (sym){
case '/':
left=nums.pop();
nums.push(left/right);
symbol.pop();
break;
case '*':
left=nums.pop();
nums.push(left*right);
symbol.pop();
break;
default:
if(sym=='-'){
nums.push(-Integer.valueOf(str.toString()));
}else{
nums.push(Integer.valueOf(str.toString()));
}
break;
}
}else{
if(str.length()>0){
if(!symbol.isEmpty()&&symbol.peek()=='-'){
nums.push(-Integer.valueOf(str.toString()));
}else{
nums.push(Integer.valueOf(str.toString()));
}
}
}
if(i<s.length()){
if(s.charAt(i)==' '){
continue;
}else{
symbol.push(s.charAt(i));
}
}
}
while(!symbol.isEmpty()){
symbol.pop();
right=nums.pop();
left=nums.pop();
nums.push(left+right);
}
return nums.pop();
}
}