栈操作的问题
来源:互联网 发布:borderlands 2 mac 编辑:程序博客网 时间:2024/06/15 11:59
题意:给出一个表达式,包含运算符+,-,*,min,max,求其结果
思路:用两个栈,一个操作数栈,一个操作符栈,当处理+,-,*操作符时,如果处理的操作符优先级比栈顶操作符优先级低,就入栈,否则,出栈;在处理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_JUDGEifstream fin("f:\\oj\\uva_in.txt");streambuf *old = cin.rdbuf(fin.rdbuf());#endifSolution 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_JUDGEcin.rdbuf(old);#endifreturn 0;}
0 0
- 栈操作的问题
- 基于栈操作的迷宫问题求解
- bit操作的问题
- 位操作的问题
- strcpy操作的问题
- 集合的操作问题
- 操作Word的权限问题
- 并发操作的一致性问题
- 操作符重载的问题
- javascript - iframe的操作问题
- 文件操作eof()的问题
- c++文件操作的问题
- 移位操作的奇怪问题
- 关于操作计划的问题
- 文件读取操作的问题
- 文件操作EOF()的问题
- 文件操作EOF的问题
- 有关SVN操作的问题
- 【逻辑思考】真正改变命运的其实并不是知识,而是这些知识带给你的能力的提高。也就是说,转化为能力的知识,才能够改变你的命运。
- Android OpenGL入门示例:绘制三角形和正方形 (附完整源码)
- js延迟加载的方式有哪些?
- ubuntu 查看内存最大容量
- ThinkPHP 公共函数整理
- 栈操作的问题
- 《Reverse Engineering for Beginners》 - 第1章 代码模式 - 笔记(1.13)
- Android 自定义Dialog背景透明及显示位置设置
- 【Java每日一题】20161219
- Java 冒泡排序、选择排序、快速排序、归并排序
- iOS设计模式—工厂模式
- JAVA多线程之CALLABLE接口的实现
- 字节流总结
- Oracle创建唯一索引遇到的问题(ORA-01452: 无法 CREATE UNIQUE INDEX; 找到重复的关键字)