简单计算器


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12466    Accepted Submission(s): 4098


Problem Description


读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。


 



Input


测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。


 



Output


对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。


 



Sample Input


1 + 2 4 + 2 * 5 - 7 / 11 0


 



Sample Output


3.00 13.36



/*
HDU 1237 表达式求值
现将下表达式求值的思想:
1.将中缀表达式转为后缀表达式
设置两个栈,一个用来保存后缀式的栈,一个用来暂时保存运算符的栈,
将中序表达式一个一个字符地读入,遇到数字字符就直接压入后缀式栈,
遇到运算符时就先暂时保存到运算符栈中,
等到下次读到字符时将运算符栈中的运算符与之比较优先级,
若运算符栈里的运算符的优先级高于这次读到的运算符就将运算符栈中的运算符进栈,
否则将这个运算符压入运算符栈。
4 + 2 * 5 - 7 / 11
425*711/-+
2.后缀表达式求值
遇到字符是个操作符,弹出2个操作数,运算完结果压入

或者运算符栈里的优先级高 直接弹出2个数计算 后再压入
425 遇到- 14 7 11 14 0.636
+* ==>*计算 +计算 -/ ==> - ==> 13.36
*/
#include<iostream>
#include<stdio.h>
#include<stack>
#include<cstring>
using namespace std;
char str[205];

int change(char c)
{
if(c=='+') return 1;
if(c=='-') return 2;
if(c=='*') return 3;
if(c=='/') return 4;
return -1;
}

int cmp(int a,int b)
{
if(a==1||a==2) a=1;
else a=2;
if(b==1||b==2) b=1;
else b=2;

if(a>=b)
return 1;
return 0;

}
void cal()
{
stack<double>a; //后缀结果栈
stack<int>b; // 运算符栈
int i,len=strlen(str);
double s;

for(i=0;i<len;i++)
{
if(str[i]==' ')
continue;
if(str[i]>='0'&&str[i]<='9') //数字
{
s=0;
while(str[i]>='0'&&str[i]<='9')
{
s=s*10+str[i]-'0';
i++;
}
a.push(s);
}
if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/')
{
int temp;
temp=change(str[i]);
if(b.empty())
{
b.push(temp);
continue;
}
else
{
int t=b.top();//运算符栈里的优先级大于当前的,将运算符栈里的压入到后缀
while(cmp(t,temp))
{
double a1=a.top(); a.pop(); //弹出2个操作数
double b1=a.top(); a.pop();

int c=b.top(); b.pop();//弹出运算符栈里的运算符

switch(c)
{
case 1:
a.push(a1+b1);
break;
case 2:
a.push(b1-a1);
break;
case 3:
a.push(a1*b1);
break;
case 4:
a.push(b1/a1);
break;
}

if(b.empty()) //如果这样写要判断为空否
break;
else //否则继续比较运算符栈顶运算
t=b.top();

}
b.push(temp); // 将这个运算符压入运算符栈
}

}
}
while(!b.empty())
{
double a1=a.top(); a.pop(); //弹出2个操作数
double b1=a.top(); a.pop();

int c=b.top(); b.pop();//弹出运算符栈里的运算符

switch(c)
{
case 1:
a.push(a1+b1);
break;
case 2:
a.push(b1-a1);
break;
case 3:
a.push(a1*b1);
break;
case 4:
a.push(b1/a1);
break;
}
}
printf("%.2lf\n",a.top());
}

int main()
{
while(gets(str),strcmp(str,"0"))
{
cal();
}
return 0;
}