C ++简单运算(+,-,/,*)评估类 [英] C++ simple operations (+,-,/,*) evaluation class

查看:94
本文介绍了C ++简单运算(+,-,/,*)评估类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个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.

我确实意识到了什么我问的是一种口译员,并且有构建它们的工具,因为我在CS方面的背景很差,因此,如果您能指点我准备好的班级,我将不胜感激。

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 .

推荐答案

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

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

它使用反向波兰符号并支持:


  • 运算符优先级(5 + 5 * 5 = 30而不是50)

  • 父母((5 + 5)* 5 = 50)

  • 以下运算符:+,-,*,/

编辑:您可能要删除底部的Abs();满足我的需要0-5应该是5而不是-5!

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

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

这篇关于C ++简单运算(+,-,/,*)评估类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆