当前位置:编程学习 > C/C++ >>

利用两个栈求解算数表达式的源代码

说明:
1、只包含了小括号(),和+、-、*、/二元操作符的四则运算
2、求它更通用的求解方法可以参考递归求解、通过表达式树求解的方法
 
[cpp]
#include <cctype> 
#include <map> 
#include <stack> 
#include <string> 
#include <iostream> 
 
using namespace std; 
 
//弹出操作符栈的一个操作符,弹出操作数栈的一个操作数,计算结果 
void Compute(stack<char> &operators, stack<double> &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<char,int> &priority_tbl, double &result) 

    stack<double> operands;//操作数栈 
    stack<char> 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<char, int> 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('/',

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,