中缀表达式转后缀表达式
2015-11-29 23:33
369 查看
使用c++11, 实现一个简单的计算器,计算数学上常用的中缀表达式的值。
最重要的演示中缀表达式转换为后缀表达式算法。目前支持+-* / 任意四则运算,除法为计算机的整除法,要改为浮点除法也是很容易的事。
输入为常用的数学中缀表达式,
输入为表达式的值。
计算分为以下几步:
预处理,将字符串转化为独立的操作数和操作符,同时去掉空格,存放在字符串vector里。
中缀表达式转后缀表达式。中缀表达式虽然最合适人类阅读,但后缀(或者前缀)表达式更方便计算机计算,同时后缀表达式不需要中缀表达式里改变计算优先级的括号()。这是最重要的一步。
最后通过扫描后缀表达式计算表达式的值。
github https://github.com/gjwang/Calculator.git
代码如下
// // main.cpp // Calculator // // Created by gjwang on 11/22/15. // Copyright (c) 2015 gjwang. All rights reserved. // #include <iostream> #include <cstdlib> #include <vector> #include <map> #include <string> #include <cassert> using namespace std; string expr = "(3+2*5 + (80-79)*2*2)*2"; string expr0 = "(3+2)*(4-2)/(8+7)"; string expr1 = "9-5+2"; string expr2 = "(9-5)+2"; string expr3 = "(9-(5+2))"; string expr4 = "(((9- ((((5+2)))) )))"; string expr5 = " (9- ( 20 + 10 ) ) * 2 "; vector<string> infixExprVec={ expr, expr0, expr1, expr2, expr3, expr4, expr5, }; typedef int (*BiOpdFuns)(int, int); typedef map<const string, BiOpdFuns> BiOpdFunsMap_t; BiOpdFunsMap_t BiOpdFunsMap; const string operatorAdd = "+"; const string operatorMinus = "-"; const string operatorMult = "*"; const string operatorDiv = "/"; inline int addOpd(int a, int b){ return a + b; } inline int minusOpd(int a, int b){ return a - b; } inline int multOpd(int a, int b){ return a * b; } inline int divOpd(int a, int b){ return a / b; } inline bool isBlank(const char c){ return c == ' '; } inline bool isDigit(const char c){ return c >= '0' && c <= '9'; } inline int charToDigit(const char c){ return c - '0'; } inline bool isOperand(const char c){ //For now return isDigit(c); } inline bool isOperator(const char c){ return c == '+' || c == '-' || c == '*' || c == '/'; } bool isOperator(const BiOpdFunsMap_t& map, const string& s){ return map.find(s) != map.end(); } int GetPriority(const char c){ if (c == '+' || c == '-') { return 10; } else if ( c == '*' || c == '/') { return 11; } else { assert(false); return 0; } } int GetPriority(const string& s){ if (s.compare("+") == 0 || s.compare("-") == 0) { return 10; } else if ( s.compare("*") == 0 || s.compare("/") == 0) { return 11; } else { assert(false); return 0; } } vector<string> preProcessInfixExpr(const string& infixExpr){ vector<string> infixExprVec; string tmp(""); for_each(infixExpr.begin(), infixExpr.end(), [&](char c){ if (isDigit(c)) { tmp.push_back(c); }else{ if (tmp.length() != 0) { infixExprVec.push_back(tmp); tmp.clear(); } if ( !isBlank(c) ) { //skip any blank infixExprVec.push_back(string(1, c)); } } }); //push the last operand into stack if (tmp.length() != 0) { infixExprVec.push_back(tmp); } return infixExprVec; } vector<string> convertInfixToPostfixExprStack(const vector<string>& infixExpr){ vector<string> postFixExpr(128); vector<string> opStack(32); vector<string>::const_iterator it = infixExpr.begin(); for (; it != infixExpr.end(); it++) { if (it->compare("(") == 0) { opStack.push_back(*it); }else if (it->compare(")") == 0) { //pop all operator within () for (auto it=opStack.rbegin(); it->compare("(") != 0; it++) { postFixExpr.push_back(opStack.back()); opStack.pop_back();//pop operator } opStack.pop_back();//pop "(" }else if (isOperator(BiOpdFunsMap, *it)) { if (!opStack.empty() && isOperator(BiOpdFunsMap, opStack.back()) && GetPriority(opStack.back()) >= GetPriority(*it) ) { //meet an operator, it's priority is great_or_equal to the last one's postFixExpr.push_back(opStack.back()); opStack.pop_back(); opStack.push_back(*it); }else { opStack.push_back(*it); } }else{ //if (isOperand(*it)) { postFixExpr.push_back(*it); } } postFixExpr.insert(postFixExpr.end(), opStack.rbegin(), opStack.rend()); return postFixExpr; } int calcutorPostfixExpr(const vector<string>& postfixExpr){ int ret = 0; vector<string> opdStack(32); //operand stack vector<string> opStack(32); //operator stack for_each(postfixExpr.begin(), postfixExpr.end(), [&](const string s){ if (isOperator(BiOpdFunsMap, s)){ auto opd2 = opdStack.back(); opdStack.pop_back(); auto opd1 = opdStack.back(); opdStack.pop_back(); auto it = BiOpdFunsMap.find(s); if (it != BiOpdFunsMap.end()) { auto operand = it->second(stoi(opd1), stoi(opd2)); //cout << "operand=" << operand << " "; opdStack.push_back(to_string(operand)); ret = operand; } else { //should not happen assert(false); } }else{ //s is operand opdStack.push_back(s); } }); return ret; } int main(int argc, const char * argv[]) { BiOpdFunsMap.insert(make_pair(operatorAdd, addOpd)); BiOpdFunsMap.insert(make_pair(operatorMinus, minusOpd)); BiOpdFunsMap.insert(make_pair(operatorMult, multOpd)); BiOpdFunsMap.insert(make_pair(operatorDiv, divOpd)); for_each(infixExprVec.begin(), infixExprVec.end(), [](const string& inFixExp){ for_each(inFixExp.begin(), inFixExp.end(), [](char c){ cout << c; }); cout << " = "; auto infixExprString = preProcessInfixExpr(inFixExp); auto postfixExpr =convertInfixToPostfixExprStack(infixExprString); for_each(postfixExpr.begin(), postfixExpr.end(), [](const string& expr){ cout << expr; }); //cout << endl; int ret = calcutorPostfixExpr(postfixExpr); cout << " = " << ret << endl; }); return 0; }
测试输出:
(3+2*5 + (80-79)*2*2)*2 = 325*8079-2*2*++2* = 34 (3+2)*(4-2)/(8+7) = 32+42-*87+/ = 0 9-5+2 = 95-2+ = 6 (9-5)+2 = 95-2+ = 6 (9-(5+2)) = 952+- = 2 (((9- ((((5+2)))) ))) = 952+- = 2 (9- ( 20 + 10 ) ) * 2 = 92010+-2* = -42
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性
- C++ Custom Control控件向父窗体发送对应的消息