rt,在bdfs看的不是很懂,做了好几遍不是WA就是RE,用栈做的,有dalao帮忙看一下吗?
code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
stack<int> s;
int a,b,num,ans = 0;
string str;
cin>>str;
for (long long i = 0; i < str.length(); i++)
{
if (str[i] == '+'){
a = s.top();
s.pop();
b = s.top();
s.pop();
s.push(b + a);
}
else if (str[i] == '-'){
a = s.top();
s.pop();
b = s.top();
s.pop();
s.push(b - a);
}
else if (str[i] == '*'){
a = s.top();
s.pop();
b = s.top();
s.pop();
s.push(b * a);
}
else if (str[i] == '/'){
a = s.top(); s.pop();
b = s.top(); s.pop();
s.push(b / a);
}
else if (str[i] == ' '){
if (ans != 0)
s.push(ans);
ans = 0;
}
else{
num = str[i] - '0';
ans = ans * 10 + num;
}
}
double ansans = s.top();
printf("%.6lf\n", ansans);
return 0;
}
在线等,急!