我的方法比较麻烦,事实上是可以简化的
这里有一个别人写的:https://blog.youkuaiyun.com/hling_so/article/details/52869890
#include <iostream>
#include <stack>
#include <iomanip>
using namespace std;
stack<double> ch;
int main(){
int n;//测试次数
cin>>n;
string s;
double t,t1,t2;
for(int i;i<n;i++){
cin>>s;
for(int j=0;j<s.length();j++){
if(s[j]=='*'){
t1=ch.top();
ch.pop();
t2=ch.top();
ch.pop();
ch.push(t1*t2);
}
else if(s[j]=='+'){
t1=ch.top();
ch.pop();
t2=ch.top();
ch.pop();
ch.push(t1+t2);
}
else if(s[j]=='-'){
t1=ch.top();
ch.pop();
t2=ch.top();
ch.pop();
ch.push(t2-t1);
}
else if(s[j]=='/'){
t1=ch.top();
ch.pop();
t2=ch.top();
ch.pop();
ch.push(t2/t1);
}
else{
t=s[j]-96;
ch.push(t);
}
}
t=ch.top();
ch.pop();
cout << setprecision(2) << setiosflags(ios::fixed | ios::showpoint)<<t<<endl;
}
}