【问题标题】:Infix to postfix - Including negative values后缀中缀 - 包括负值
【发布时间】:2023-03-26 20:22:01
【问题描述】:

我正在制作一个将中缀数学字符串转换为后缀的函数。 这就是我所拥有的:

std::string toPostfix(std::string& infixStr, std::string& postfixStr, std::string& first_nr, std::string& second_nr, char oper, char prev_oper) {

    //std::cout << "[" << infixStr << "]" << std::endl;

    if (infixStr == "") {
        if (second_nr == "") {
            second_nr = "0";
        }

        if (oper == '\0') {
            oper = prev_oper;

            if (prev_oper == '-') {
                second_nr = first_nr;
                first_nr = "0";
            }
        }


        postfixStr += first_nr + " " + second_nr + " " + oper;

        std::cout << " end: " << postfixStr << std::endl ;
        return postfixStr;
    }


    char c = infixStr[0];

    if (isOperator(c)) {
        ///////////////
        if (postfixStr == "") {
            if (c == '-') {

            }
        }
        //////////////////

        if (oper != '\0') {
            if (first_nr == "") {
                if (oper == '-') {
                    /////////////////////////
                }
            }

            postfixStr += first_nr + " " + second_nr + " " + oper + " ";

            first_nr = "";
            second_nr = "";
            oper = '\0';
            prev_oper = c;
        } else {

            oper = c;
        }
    } else {
        if (oper == '\0') {
            first_nr += c;
        } else {
            second_nr += c;
        }
    }

    infixStr = infixStr.erase(0, 1);
    return toPostfix(infixStr, postfixStr, first_nr, second_nr, oper, prev_oper);
}

这在某些情况下有效,但是例如此输入字符串0+1-5+2 输出为0 1 + 5 2 +- 被忽略。正确的输出应该是0 1 + -5 2 + 我做错了什么?我认为我需要区分运算符中的减号,以及使值变为负数时的减号。

【问题讨论】:

  • 我建议您阅读this,并重新考虑您发布的整个代码。这不是您应该将中缀转换为后缀的方式。试图在这里和那里修补东西,然后在其他地方出现泄漏——这就是你编写代码的路径。

标签: c++ postfix-notation


【解决方案1】:
/*
 * Function to convert an infix string to postfix notation
 * :param infix: Infix string
 * :return string: returns postfix string
 *
 * Example:
 * std::string s = "5+5";
 * toPostfix(s) == "5 5 +"
 *
 */
std::string toPostfix(std::string& infix) {
    std::string postfix = "";

    //Stack to hold operators and nr is a helper string to
    //group digits in numbers
    std::stack<char> stack;
    std::string nr = "";

    //If first character is a minus-operator (AKA a negative number)
    //add "0"
    if (infix[0] == '-') {
        infix = "0" + infix;
    }

    //Looping over infix string
    for (int i = 0; i < infix.size(); i++) {
        //If current evaluated character ain't an operator, it's a digit
        if (!isOperator(infix[i])) {
            //If digit is in a group of digits (AKA a number) put the whole number in nr
            while (!isOperator(infix[i]) && i < infix.size()) {
                nr += infix[i];
                i++;
            }

            i--;

            //Append the number to the postfix string
            postfix += nr + " ";
            nr = "";
        } else {
            //This block is executed when evaluated character is an operator

            //If stack is empty, or the evaluated operator is higher than the one in the stack
            //push it to the stack (Needs to be appended to the postfix string later)
            if (stack.size() == 0 || precedence(infix[i]) > precedence(stack.top())) {
                stack.push(infix[i]);
            } else {
                //While the stack contacts a higher or equally high precedence as currently
                //evaluated operator
                while (precedence(stack.top()) >= precedence(infix[i])) {
                    //We append the top of the stack to the postfix string
                    postfix += stack.top();
                    postfix += ' ';

                    stack.pop();
                    if (stack.size() == 0) {
                        break;
                    }
                }

                //Push evaluated operator to stack
                stack.push(infix[i]);
            }
        }
    }

    //Append all remaining operators to the postfix string
    while (stack.size() != 0) {
        postfix += stack.top();
        stack.pop();
    }

    return postfix;
}

【讨论】:

    猜你喜欢
    • 2018-04-02
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    • 2015-06-08
    • 2011-04-28
    相关资源
    最近更新 更多