leetcode227

class Solution {
public:
stack<int> OPD;
stack<char> OPR;
int calculate(int num1, int num2, char opr){
switch (opr){
case '+':{
return num1 + num2;
}
case '-':{
return num1 - num2;
}
case '*':{
return num1*num2;
}
case '/':{
return num1 / num2;
}
}
}
int calculate(string s) {
int len = s.size();
int i=;
while(i<len){
if (s[i] == ' '){
i++;
}
else if (s[i] == '+' || s[i] == '-' || s[i]=='*' || s[i]=='/'){
OPR.push(s[i]);
i++;
}
else{
int num = ;
while (i < len && s[i] >= '' && s[i] <= ''){
num = num * + (s[i] - '');
i++;
}
if (!OPR.empty() && (OPR.top()=='/' || OPR.top()=='*')){
num = calculate(OPD.top(),num,OPR.top());
OPR.pop();
OPD.pop();
}
OPD.push(num);
}
}
int res=;
while (!OPR.empty()){
int tmp=OPD.top();
char ch=OPR.top();
if(ch=='-'){
tmp=-tmp;
}
res+=tmp;
OPD.pop();
OPR.pop();
}
res+=OPD.top();
return res;
}
};
上一篇:*JavaScript标准参考教程 - 阮一峰


下一篇:python3 下列表与字典转换