C++简单运算(+,-,/,*)评估类

发布于 2024-08-15 08:48:06 字数 154 浏览 8 评论 0原文

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

我确实意识到我所要求的是一种解释器,并且有工具可以构建它们,由于我的计算机科学背景很差,所以如果您能给我指点现成的课程,我将不胜感激。

I am looking for a C++ class I can incorporate into a project I am working on.
the functionality I need is evaluation of string operations to numerical form: for example "2 + 3*7" should evaluate to 23.

I do realize what I am asking is a kind of an interpreter, and that there are tools to build them, by my background in CS is very poor so I would appreciate if you can point me to a ready made class .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风铃鹿 2024-08-22 08:48:06

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

它使用 逆波兰表示法并支持:

  • 运算符优先级 (5 + 5 * 5 = 30 not 50)
  • Parens (( 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

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

This should do exactly what you want. You can test it live at: http://www.wowpanda.net/calc

It uses Reverse Polish Notation and supports:

  • Operator precedence (5 + 5 * 5 = 30 not 50)
  • Parens ((5 + 5) * 5 = 50)
  • The following operators: +, -, *, /

EDIT: you'll probably want to remove the Abs() at the bottom; for my needs 0 - 5 should be 5 and not -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

/***************************************************************************************/
七色彩虹 2024-08-22 08:48:06

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

boost::spirit comes with a calculator example which would do what you need:
http://www.boost.org/doc/libs/1_33_1/libs/spirit/example/fundamental/ast_calc.cpp

生寂 2024-08-22 08:48:06

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

muParser is written in C++ and does just what you need.

清风挽心 2024-08-22 08:48:06

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

C++ in Action, in addition to being a great book on C++, includes a fully working calculator, doing what you need (and actually much more). And the book is available for free online

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文