栈操作的问题
2016-12-19 08:59
78 查看
题意:给出一个表达式,包含运算符+,-,*,min,max,求其结果
思路:用两个栈,一个操作数栈,一个操作符栈,当处理+,-,*操作符时,如果处理的操作符优先级比栈顶操作符优先级低,就入栈,否则,出栈;在处理min,max操作符时,如果当前处理的为min或者max,将min或者max,(两个入栈,当处理逗号时,就出栈,直到栈顶操作符为左括号,并将逗号入栈;而当处理右括号时,出栈直到栈顶为逗号。当处理完毕后,操作符栈可能不为空,需要出栈处理
代码如下:
验证代码如下:
1 * 2 + max(3,4)
min(2+3, max(4, 5))
思路:用两个栈,一个操作数栈,一个操作符栈,当处理+,-,*操作符时,如果处理的操作符优先级比栈顶操作符优先级低,就入栈,否则,出栈;在处理min,max操作符时,如果当前处理的为min或者max,将min或者max,(两个入栈,当处理逗号时,就出栈,直到栈顶操作符为左括号,并将逗号入栈;而当处理右括号时,出栈直到栈顶为逗号。当处理完毕后,操作符栈可能不为空,需要出栈处理
代码如下:
#include <iostream> #include <stack> #include <string> #include <map> #include <cstring> #include <cctype> #include <algorithm> #include <fstream> using namespace std; const int MAX_LEN = 100; class Solution { public: int Calculate(char *pszExpr) { map<string, int> pPriority; pPriority["+"] = 1; pPriority["-"] = 1; pPriority["*"] = 2; pPriority["("] = 0; stack<string> operStack; stack<int> numStack; int len = strlen(pszExpr); for (int i = 0; i < len; i++) { if (isspace(pszExpr[i])) continue; //数字 if (isdigit(pszExpr[i])) { int j = i; int sum = 0; while (j < len && isdigit(pszExpr[j])) { sum = sum * 10 + (pszExpr[j] - '0'); j++; } numStack.push(sum); i = j - 1; continue; } if (pszExpr[i] == '-' || pszExpr[i] == '+' || pszExpr[i] == '*') { if (operStack.empty()) { string tmp; tmp += pszExpr[i]; operStack.push(tmp); } else { string curOper; curOper += pszExpr[i]; if (pPriority[curOper] > pPriority[operStack.top()]) { operStack.push(curOper); } else { while(!operStack.empty() && pPriority[curOper] <= pPriority[operStack.top()]) { int ans = popCal(numStack, operStack); numStack.push(ans); } operStack.push(curOper); } } } else if (pszExpr[i] == 'm') { char tmp[4] = { 0 }; strncpy(tmp, &pszExpr[i], 3); string strtmp = tmp; operStack.push(strtmp); operStack.push("("); i += 3; continue; } else if (pszExpr[i] == ',') { while (!operStack.empty() && operStack.top() != "(") { int ans = popCal(numStack, operStack); numStack.push(ans); } operStack.push(","); } else if (pszExpr[i] == ')') { while (!operStack.empty() && operStack.top() != ",") { int ans = popCal(numStack, operStack); numStack.push(ans); } operStack.pop();//弹出, operStack.pop();//弹出( int ans = popCal(numStack, operStack); numStack.push(ans); } } while (!operStack.empty()) { int ans = popCal(numStack, operStack); numStack.push(ans); } return numStack.top(); } private: int cal(int num1, int num2, string oper) { if (oper == "+") { return num1 + num2; } else if (oper == "-") { return num1 - num2; } else if (oper == "*") { return num1 * num2; } else if (oper == "min") { return min(num1, num2); } else if (oper == "max") { return max(num1, num2); } } int popCal(stack<int>& numstack, stack<string>& operstack) { int num2 = numstack.top(); numstack.pop(); int num1 = numstack.top(); numstack.pop(); string op = operstack.top(); operstack.pop(); int ans = cal(num1, num2, op); return ans; } };
验证代码如下:
1 * 2 + max(3,4)
min(2+3, max(4, 5))
int main() { #ifndef ONLINE_JUDGE ifstream fin("f:\\oj\\uva_in.txt"); streambuf *old = cin.rdbuf(fin.rdbuf()); #endif Solution solver; char s[MAX_LEN]; while (cin.getline(s, MAX_LEN)) { cout << "s:" << s << endl; int ans = solver.Calculate(s); cout << "ans:" << ans << endl; } #ifndef ONLINE_JUDGE cin.rdbuf(old); #endif return 0; }
相关文章推荐
- "BOF或EOF中有一个是真,或者当前的记录已被删除,所需的操作要求一个当前的记录"问题解决
- JAVA与C++::关于JNI中文字符串操作问题总结
- sql service2000的安装 操作 常见问题
- [.net中]数据操作应该注意的问题
- 在使用SPS操作文件的时候碰到这样的问题
- 关于在ASP.NET中以DCOM方式操作Excel的几个问题
- 触发器(当2个表中的相应值改变时同时改变一个表中的一个字段)(同时有处理“无法解决 equal to 操作的排序规则冲突”问题)
- [原创]两个listbox之间操作数据交换的问题!
- 关于数字证书操作的问题
- 有关SQL排序规则---------即"无法解决 equal to 操作的排序规则冲突"等等的菜问题.
- 对于多台电脑同时操作数据的问题
- word和ie在多线程中的操作的问题解决办法
- [求助]关于eclipse下开发串口操作插件的问题
- 解决ASP 2.0中GridView控件的删除、插入、编辑命令操作客户端确认问题的另一方法
- 操作Word的权限问题
- 使用JSP开发应用程序时的编码(中文等乱码)问题的建议解决方案(包括接受request和数据库操作)
- 字符集问题的初步探讨(七)----关于字符集更改的内部操作
- 存储过程中操作了链接服务器时要注意的问题
- 摘录:ADO操作数据的几个问题
- 关于文件操作的问题