使用堆栈时未获得预期的输出

时间:2018-04-17 19:12:57

标签: java stack

我正在开发一个转换中缀表达式并输出后缀表达式的程序。下面的代码运行没有错误,但没有返回我期望的输出。

例如,如果我提供元素' infix'使用字符串1 + 2,我喜欢输出为1 2 +。现在它出现为1 2+(1和2之间有两个空格,2和+之间没有空格)。任何人都可以向我提供一些关于如何在字符串中的每个字符之间返回一个字符串的字符串的指导/想法吗?

public static String infixConvert(String infix) {

    //create a string to contain the converted formula, and a stack to make the conversion in

    String convertedPostfix = "";
    Stack<Character> mathFunction = new Stack<Character>();
    char poppedVal;

    //start a loop to process the conversion from infix to postfix

    for (int x = 0; x < infix.length(); x++) {

        //create an object to reference the character being evaluated

        char character = infix.charAt(x);

        //Call the numOrChar method to first check if the character is a number or a math function. If it's a number, add the character to convertedPostfix

        if (numOrChar(character) == false)

            convertedPostfix = convertedPostfix + character;

        //If the character is a right-side bracket (but not a left-side one), then increment convertedPostFix by the poppedVal

        else if (character == ')')
            while ((poppedVal = mathFunction.pop()) != '(')
                convertedPostfix = convertedPostfix + poppedVal;

        //For all other scenarios, update convertedPostFix to include the top element in the stack (should be a math function)

        else {
                while (!mathFunction.isEmpty() && character != '(' && priority(mathFunction.peek()) >= priority(character))
                    convertedPostfix = convertedPostfix + mathFunction.pop();
                        mathFunction.push(character);
            }
    }

    //Pop the remaining operators in the stack

    while(!mathFunction.isEmpty())
        convertedPostfix = convertedPostfix + mathFunction.pop();
    return convertedPostfix;

    }

    //Method to determine if the character is a number or non-numeric character

    public static boolean numOrChar(char x) {
        return priority(x) > 0;
    }

    //Method to determine the importance of the non-numerical characters in the string

    public static int priority(char x) {
        if (x == '(')
            return 1;

        else if (x == ')')
            return 1;

        else if (x == '+')
            return 2;

        else if (x == '-')
            return 2;

        else if (x == '*')
            return 3;

        else if (x == '/')
            return 3;

            else return 0;

    }

编辑我在方法的顶部添加了一个新的字符串,用于从输入的字符串中删除空格。

public static String infixConvert(String infix) {

    //create a string to contain the converted formula, and a stack to make the conversion in

    String convertedPostfix = "";
    Stack<Character> mathFunction = new Stack<Character>();
    char poppedVal;
    String infixNoSpace = infix.replaceAll("\\s","");

    //start a loop to process the conversion from infix to postfix

    for (int x = 0; x < infixNoSpace.length(); x++) {

        //create an object to reference the character being evaluated

        char character = infixNoSpace.charAt(x);

0 个答案:

没有答案