说明: 1、只包含了小括号(),和+、-、*、/二元操作符的四则运算 2、求它更通用的求解方法可以参考递归求解、通过表达式树求解的方法 [cpp] #include #include #include #include #include using namespace std; //弹出操作符栈的一个操作符,弹出操作数栈的一个操作数,计算结果 void Compute(stack &operators, stack &operands) { double op2 = operands.top(); operands.pop(); double op1 = operands.top(); operands.pop(); char optr = operators.top(); double result; switch(optr) { case '+': result = op1 + op2; case '-': result = op1 - op2; case '*': result = op1 * op2; case '/': result = op1 / op2; } operands.push(result); operators.pop(); } //通过操作符栈和操作数栈求解表达式,表达式正确返回true,结果存在result里,错误返回false bool ComputeExpr(const string &expr, map &priority_tbl, double &result) { stack operands;//操作数栈 stack operators;//操作符栈 string inner_expr(expr + '$');//加个哨兵 operators.push('#');//哨兵 for(int i = 0; i < inner_expr.length(); ++i) { char ch = inner_expr[i]; if(isdigit(ch))//操作数直接入操作数栈 operands.push(ch - 0x30); else//操作符 { switch(ch) { case '(': operators.push(ch); break; case ')': //求解当前最内部的括号表达式 while(operators.top() != '(') { if(operators.top() == '#' || operands.size() < 2) return false; Compute(operators, operands); } operators.pop(); break; case '$': //此时,正确表达式已经不存在括号了,计算 while(operators.top() != '#') { if(operands.size() < 2) return false; Compute(operators, operands); } if(operands.size() == 1) { result = operands.top(); return true; } return false; default: //一般的二元操作符求解 while(priority_tbl[ch] <= priority_tbl[operators.top()]) { if(operands.size() < 2) return false; Compute(operators, operands); } operators.push(ch); } } } } int main(int argc, char *argv[]) { const string expr_str("((5+6)*7/(2-9)*9)+5"); //优先级表,数字越大,优先级越高 map priority_tbl; priority_tbl.insert(make_pair('+',2)); priority_tbl.insert(make_pair('-',2)); priority_tbl.insert(make_pair('*',3)); priority_tbl.insert(make_pair('/',3)); priority_tbl.insert(make_pair('(',1)); priority_tbl.insert(make_pair('#',0)); double result; bool ret = ComputeExpr(expr_str, priority_tbl, result); if(ret) { cout << expr_str << " = " << result << endl; } else { cout << "表达式错误!" << endl; } return 0; }
作者:Challenge_C_PlusPlus