给大家推荐个靠谱的公众号程序员探索之路,大家一起加油
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stack>
#define N 1000
using namespace std;
char s[N],b[3];
stack<double>num;
stack<char>st;
int main()
{
while(gets(s))
{
int k=strlen(s);
double a1,a2;
char s1[10],s2[10];
for(int i=k-1;i>=0;i--)
{
//if(s[i]==' ') continue;
if(s[i]>='0'&&s[i]<='9'||s[i]=='.')//倒着来
{
int l=0;
while(s[i]!=' ')
{
st.push(s[i]);
i--;
}
while(!st.empty())
{
b[l]=st.top();
st.pop();
l++;
}
b[l]='\0';
num.push(atof(b));
}
else
{
switch(s[i])
{
case '+' :
a1=num.top();
num.pop();
a2=num.top();
num.pop();
num.push(a2+a1);
break;
case '-' :
a1=num.top();
num.pop();
a2=num.top();
num.pop();
num.push(a1-a2);//顺序不能错
break;
case '*' :
a1=num.top();
num.pop();
a2=num.top();
num.pop();
num.push(a2*a1);
break;
case '/' :
a1=num.top();
num.pop();
a2=num.top();
num.pop();
num.push(a1/a2);//顺序不能错
break;
}
}
}
printf("%0.2lf\n",num.top());
num.pop();
}
return 0;
}