问题 A: 简单计算器
时间限制: 1 Sec 内存限制: 32 MB
提交: 631 解决: 287
[提交][状态][讨论版][命题人:外部导入]
题目描述
读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
输入
测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
输出
对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
样例输入
30 / 90 - 26 + 97 - 5 - 6 - 13 / 88 * 6 + 51 / 29 + 79 * 87 + 57 * 92 0
样例输出
12178.21
#include<iostream>
#include<stack>
#include<string>
#include<stdlib.h>
#include<cstdio>
using namespace std;
/*判断符号间的优先关系函数
*1表示>,0表示=,-1表示<
*c1栈内的算符,c2栈外的算符
此题于7.1 1918再次出现
用了结构体 map queue写法
*/
int Judge(char c1,char c2)
{
int a1,a2;
if('+'==c1||'-'==c1) a1 = 3;
if('*'==c1||'/'==c1)a1 = 5;
if('('==c1) a1 = 1;
if(')'==c1) a1 = 7;
if('#'==c1) a1 = 0;
if('+'==c2||'-'==c2)a2 = 2;
if('*'==c2||'/'==c2)a2 = 4;
if('('==c2) a2 = 6;
if(')'==c2) a2 = 1;
if('#'==c2) a2 = 0;
if(a1>a2) return 1;
if(a1==a2) return 0;
if(a1<a2) return -1;
}
//符号运算函数
double run(char c ,double d1,double d2)
{
switch (c)
{
case '+':
return d1+d2;
break;
case '-':
return d1-d2;
break;
case'*' :
return d1*d2;
break;
case '/':
return d1/d2;
break;
default:
return 0.0;
break;
}
}
int main()
{
char op[8] = "+-*/()#";
string str;
while (getline(cin, str), str != "0") {
for (string::iterator it = str.begin(); it != str.end(); it++) {
if (*it == ' ') str.erase(it);
}
//给表达式字符串str添加'#'结束标识符
str.append(1, '#');
stack<char> OPTR;//运算符栈
stack<double> OPND;//操作数栈
int a = -1;
//先将#符号入栈
OPTR.push('#');
while (true)
{
int b = a + 1;
a = str.find_first_of(op, a + 1);
if (a == string::npos) break;
if (a != b)
{
string ss(str, b, a - b);
double d = atof(ss.c_str());
//数据先入栈
OPND.push(d);
}
//运算符优先级比较
int ju = Judge(OPTR.top(), str[a]);
if (-1 == ju)//栈外优先级大直接入栈
{
OPTR.push(str[a]);
}
if (0 == ju)//栈内外优先级相等则出栈
{
OPTR.pop();
}
if (1 == ju)//栈内优先级大,出栈进行运算
{
double d1 = OPND.top();
OPND.pop();
double d2 = OPND.top();
OPND.pop();
d1 = run(OPTR.top(), d2, d1);
//运算结果入栈
OPND.push(d1);
OPTR.pop();
a--;
}
}
//删除表达式最后的'#'结束标识符
str.erase(str.length() - 1, 1);
printf("%.2f\n", OPND.top());
}
}