读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
输入描述:
测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
输出描述:
对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
示例1
输出
复制3.0013.36#include<iostream> #include<string> #include<stack> #include<iomanip> using namespace std; char str[220]; bool ret; int pos; int mag[][5] { 1,0,0,0,0, 1,0,0,0,0, 1,0,0,0,0, 1,1,1,0,0, 1,1,1,0,0, }; stack<int>op; stack<double>num; void get(int& pos, bool& ret, int& i) { if (i == 0 && op.empty() == true) { ret = true; pos = 0; return; } if (str[i] == 0) { ret = true; pos = 0; return; } if (str[i] >= '0' && str[i] <= '9') { ret = false; pos = 0; for (; str[i] != ' ' && str[i] != 0; i++) { pos = pos * 10 + str[i] - '0'; } if (str[i] == ' ') i++; return; } else { ret = true; if (str[i] == '+') { pos = 1; } else if (str[i] == '-') { pos = 2; } else if (str[i] == '*') { pos = 3; } else if (str[i] == '/') { pos = 4; } i += 2; return; } } int main() { while (cin.getline(str, 220)) { if (str[0] == '0' && str[1] == 0)break; int pos, idx = 0; bool ret; while (!op.empty())op.pop(); while (!num.empty())num.pop(); while (true) { get(pos, ret, idx); if (ret == false) num.push((double)pos); else { double temp; if (op.empty() == true || mag[pos][op.top()] == 1) op.push(pos); else { while (mag[pos][op.top()] == 0) { int t = op.top(); op.pop(); double b = num.top(); num.pop(); double a = num.top(); num.pop(); switch (t) { case 1:temp = a + b; break; case 2:temp = a - b; break; case 3:temp = a * b; break; case 4:temp = a / b; break; default :break; } num.push(temp); } op.push(pos); } } if (op.size() == 2 && op.top() == 0) break; } cout <<fixed <<setprecision(2) << num.top() << endl; } return 0; }
2.栈和队列一起应用#include<iostream> #include<stack> #include<queue> #include<map> #include<string> using namespace std; struct node { bool flag; char op; double x; }; string s; queue<node>q; stack<node>opo; map<char, int>op; void change() { node temp; int len = s.size(); for (int i = 0; i < len;) { if (s[i] >= '0' && s[i] <= '9') { temp.flag = true; temp.x = (double)(s[i++] - '0'); while(i<len&& s[i] >= '0' && s[i] <= '9') temp.x = temp.x*10+ (double)(s[i++] - '0'); q.push(temp); } else { temp.flag = false; while (!opo.empty() && op[s[i]] <= op[opo.top().op]) { q.push(opo.top()); opo.pop(); } temp.op = s[i]; opo.push(temp); i++; } } while (!opo.empty()) { q.push(opo.top()); opo.pop(); } } double add() { double a, b; node cur; node cur1; while (!q.empty()) { cur1 = q.front(); q.pop(); if (cur1.flag) { opo.push(cur1); } else { b = opo.top().x; opo.pop(); a = opo.top().x; opo.pop(); cur.flag = true; switch (cur1.op) { case'+': cur.x = a+b; break; case'-': cur.x = a - b; break; case'*': cur.x = a* b; break; case'/': cur.x = a/ b; break; default: break; } opo.push(cur); } } return opo.top().x; } int main() { op['+'] = op['-'] = 1; op['*'] = op['/'] = 2; FILE* O; freopen_s(&O, "input.txt", "r", stdin); while (getline(cin, s),s!="0") { for(string::iterator it=s.begin();it!=s.end();it++) { if (*it ==' ') s.erase(it); } while (!opo.empty())opo.pop(); change(); printf("%.2f\n", add()); } return 0; }