【问题标题】:C++ simple operations (+,-,/,*) evaluation classC++ 简单操作 (+,-,/,*) 评估类
【发布时间】:2010-12-28 09:14:15
【问题描述】:

我正在寻找一个可以整合到我正在从事的项目中的 C++ 类。 我需要的功能是将字符串运算评估为数字形式:例如“2 + 3*7”应该评估为 23。

我确实意识到我所要求的是一种解释器,并且有构建它们的工具,由于我在 CS 方面的背景很差,所以如果你能指出我的现成课程,我将不胜感激。

【问题讨论】:

标签: c++ calculator


【解决方案1】:

这应该完全符合您的要求。您可以在以下位置进行实时测试:http://www.wowpanda.net/calc

它使用Reverse Polish Notation并支持:

  • 运算符优先级(5 + 5 * 5 = 30 而不是 50)
  • 括号 ((5 + 5) * 5 = 50)
  • 以下运算符:+、-、*、/

编辑:您可能希望删除底部的 Abs();根据我的需要,0 - 5 应该是 5 而不是 -5!

static bool Rpn(const string expression, vector<string> &output)
{
    output.clear();
    char *end;
    vector<string> operator_stack;
    bool expecting_operator = false;

    for (const char *ptr = expression.c_str(); *ptr; ++ptr) {
        if (IsSpace(*ptr))
            continue;

        /* Is it a number? */
        if (!expecting_operator) {
            double number = strtod(ptr, &end);
            if (end != ptr) {
                /* Okay, it's a number */
                output.push_back(boost::lexical_cast<string>(number));
                ptr = end - 1;
                expecting_operator = true;
                continue;
            }
        }

        if (*ptr == '(') {
            operator_stack.push_back("(");
            expecting_operator = false;
            continue;
        }

        if (*ptr == ')') {
            while (operator_stack.size() && operator_stack.back() != "(") {
                output.push_back(operator_stack.back());
                operator_stack.pop_back();
            }

            if (!operator_stack.size())
                return false; /* Mismatched parenthesis */

            expecting_operator = true;
            operator_stack.pop_back(); /* Pop '(' */
            continue;
        }

        if (*ptr == '+' || *ptr == '-') {
            while (operator_stack.size() && IsMathOperator(operator_stack.back())) {
                output.push_back(operator_stack.back());
                operator_stack.pop_back();
            }

            operator_stack.push_back(boost::lexical_cast<string>(*ptr));
            expecting_operator = false;
            continue;
        }

        if (*ptr == '*' || *ptr == '/') {
            while (operator_stack.size() && (operator_stack.back() == "*" || operator_stack.back() == "/")) {
                output.push_back(operator_stack.back());
                operator_stack.pop_back();
            }

            operator_stack.push_back(boost::lexical_cast<string>(*ptr));
            expecting_operator = false;
            continue;
        }

        /* Error */
        return false;
    }

    while (operator_stack.size()) {
        if (!IsMathOperator(operator_stack.back()))
            return false;

        output.push_back(operator_stack.back());
        operator_stack.pop_back();
    }

    return true;
} // Rpn

/***************************************************************************************/

bool Calc(const string expression, double &output)
{
    vector<string> rpn;

    if (!Rpn(expression, rpn))
        return false;

    vector<double> tmp;
    for (size_t i = 0; i < rpn.size(); ++i) {
        if (IsMathOperator(rpn[i])) {
            if (tmp.size() < 2)
                return false;
            double two = tmp.back();
            tmp.pop_back();
            double one = tmp.back();
            tmp.pop_back();
            double result;

            switch (rpn[i][0]) {
                case '*':
                    result = one * two;
                    break;

                case '/':
                    result = one / two;
                    break;

                case '+':
                    result = one + two;
                    break;

                case '-':
                    result = one - two;
                    break;

                default:
                    return false;
            }

            tmp.push_back(result);
            continue;
        }

        tmp.push_back(atof(rpn[i].c_str()));
        continue;
    }

    if (tmp.size() != 1)
        return false;

    output = Abs(tmp.back());
    return true;
} // Calc

/***************************************************************************************/

【讨论】:

  • 因为我的基础数学不及格(计算器工作正常,但我显然没有).. 编辑了我的帖子
  • “5 + 5 * 5” = 20(或 15)怎么算?按照正常的优先级规则,即 (5*5)+5,应该是 30。如果你颠倒优先级,它会变成“(5 + 5) * 5”,应该是 50...
  • @GrayWizardx,你的意思不是“我你
【解决方案2】:

boost::spirit 附带一个计算器示例,可以满足您的需要: http://www.boost.org/doc/libs/1_33_1/libs/spirit/example/fundamental/ast_calc.cpp

【讨论】:

    【解决方案3】:

    muParser 是用 C++ 编写的,可以满足您的需求。

    【讨论】:

      【解决方案4】:

      C++ in Action 除了是一本很棒的 C++ 书籍外,还包括一个功能齐全的计算器,可以满足您的需要(实际上还有更多)。这本书可以在线免费获得

      【讨论】:

        猜你喜欢
        • 2019-08-12
        • 1970-01-01
        • 1970-01-01
        • 2011-12-08
        • 2012-10-07
        • 2013-05-17
        • 2013-07-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多