diaomingyang
2025-06-07 22:57:29
发布于:广东
给我的那个啥子同学的代码,别人不用管
#include <bits/stdc++.h>
using namespace std;
stack<int> num;
stack<char> op;
int get(char op){
if(op=='+'||op=='-'){
return 0;
}
return 1;
}
void calc(){
int r=num.top();num.pop();
int l=num.top();num.pop();
if(op.top()=='+') num.push(l+r);
if(op.top()=='-') num.push(l-r);
if(op.top()=='*') num.push(l*r);
if(op.top()=='/') num.push(l/r);
op.pop();
}
int main(){
string s;
cin>>s;
for(int i=0;i<s.length();i++){
if(s[i]>='0' && s[i]<='9'){
num.push(s[i]-'0');
}else{
if(s[i]=='('){
op.push(s[i]);
}else if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/'){
while(op.size()&&op.top()!='('&&get(op.top())>=get(s[i])){
calc();
}
op.push(s[i]);
}
else if(s[i]==')'){
while(op.top()!='('){
calc();
}
op.pop();
}
}
}
while(op.size()){
calc();
}
cout<<num.top();
return 0;
}
这里空空如也
有帮助,赞一个