数据结构栈表达式求值#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include <iostream>
#include<queue>
#include<stack>
#include<string>
using namespace std;
stack<double> data;
stack<char> ope;
int main()
{
string a;
while(getline(cin,a),!((a.size()==1)&&a[0] =='0'))
{
for(int i=0;i<a.size();i++)
{
if(a[i]==' ')
a.erase(i,1);
}
for(int i=0;i<a.size();)
{
double data1=0;
int wei = 10;
if(a[i]>='0'&&a[i]<='9')
{
data1=0;
while(a[i]>='0'&&a[i]<='9')
{
//cout<<i<<endl;
data1*=10;
data1 += (a[i]-'0');
i++;
}
//cout<<data1<<endl;
data.push(data1);
}
else
//cout<<data.top()<<endl;
ope.push(a[i++]);
// cout<<ope.top()<<endl;
if((!ope.empty())&&ope.top()=='*')
{
data1=0;
wei = 1;
while(a[i]>='0'&&a[i]<='9')
{
data1*=10;
data1 += (a[i]-'0');
i++;
}
double aa;
aa = data1*data.top();
data.pop();
data.push(aa);
ope.pop();
//ope.push('+');
}
if((!ope.empty())&&ope.top()=='/')
{
data1=0;
wei = 1;
while(a[i]>='0'&&a[i]<='9')
{
data1*=10;
data1 += (a[i]-'0');
i++;
}
double aa;
aa = data.top()/data1;
data.pop();
data.push(aa);
ope.pop();
//ope.push('+');
}
}
//cout<<"ok\n";
if(!ope.empty()&&ope.top()=='\n') ope.pop();
//cout<<ope.top()<<endl;
double ans = 0;
while(!ope.empty())
{
if(ope.top()=='+')
{
ans += data.top();
ope.pop();
data.pop();
}
else if(ope.top()=='-')
{
ans -= data.top();
ope.pop();
data.pop();
}
//cout<<ope.size()<<endl;
}
ans += data.top();
printf("%.2lf\n",ans);
}
}
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include <iostream>
#include<queue>
#include<stack>
#include<string>
using namespace std;
stack<double> data;
stack<char> ope;
int main()
{
string a;
while(getline(cin,a),!((a.size()==1)&&a[0] =='0'))
{
for(int i=0;i<a.size();i++)
{
if(a[i]==' ')
a.erase(i,1);
}
for(int i=0;i<a.size();)
{
double data1=0;
int wei = 10;
if(a[i]>='0'&&a[i]<='9')
{
data1=0;
while(a[i]>='0'&&a[i]<='9')
{
//cout<<i<<endl;
data1*=10;
data1 += (a[i]-'0');
i++;
}
//cout<<data1<<endl;
data.push(data1);
}
else
//cout<<data.top()<<endl;
ope.push(a[i++]);
// cout<<ope.top()<<endl;
if((!ope.empty())&&ope.top()=='*')
{
data1=0;
wei = 1;
while(a[i]>='0'&&a[i]<='9')
{
data1*=10;
data1 += (a[i]-'0');
i++;
}
double aa;
aa = data1*data.top();
data.pop();
data.push(aa);
ope.pop();
//ope.push('+');
}
if((!ope.empty())&&ope.top()=='/')
{
data1=0;
wei = 1;
while(a[i]>='0'&&a[i]<='9')
{
data1*=10;
data1 += (a[i]-'0');
i++;
}
double aa;
aa = data.top()/data1;
data.pop();
data.push(aa);
ope.pop();
//ope.push('+');
}
}
//cout<<"ok\n";
if(!ope.empty()&&ope.top()=='\n') ope.pop();
//cout<<ope.top()<<endl;
double ans = 0;
while(!ope.empty())
{
if(ope.top()=='+')
{
ans += data.top();
ope.pop();
data.pop();
}
else if(ope.top()=='-')
{
ans -= data.top();
ope.pop();
data.pop();
}
//cout<<ope.size()<<endl;
}
ans += data.top();
printf("%.2lf\n",ans);
}
}