啥也不说了 这题已经明示python eval语法糖不能用了 老实点上双栈写个表达式求值吧~
代码语言:javascript复制#include<bits/stdc .h>
using namespace std;
unordered_map<char,int>p{{' ',1},{'-',1},{'*',2},{'/',2}};
stack<int>num;
stack<char>op;
void eval(){
auto b=num.top();num.pop();
auto a=num.top();num.pop();
auto c=op.top();op.pop();
int x;
if(c=='-'){
x=a-b;
}
else if(c==' ')x=a b;
else if(c=='*')x=a*b;
else x=a/b;
num.push(x);
}
string s;
int main(){
cin>>s;
for(int i=0;s[i];i ){
char c=s[i];
if(isdigit(c)){
int j=i,sum=0;
while(j<s.size()&&isdigit(s[j])){
sum=sum*10 s[j ]-'0';
}
i=j-1;
num.push(sum);
}
else if(c=='('){
op.push(c);
}
else if(c==')'){
while(op.top()!='(')eval();
op.pop();
}
else{
while(!op.empty()&&p[op.top()]>=p[c])eval();
op.push(c);
}
}while(!op.empty())eval();
cout<<num.top()<<endl;
}