将中缀转换为后缀

时间:2018-11-08 19:05:19

标签: java stack postfix-notation infix-notation

我正在尝试将某些数据从infix转换为posfix,但是我遇到的问题是假定转换的后缀未打印任何内容。这就是我所拥有的

public static void main (String[] args){ 

    for(int i = 0; i < myStatement.length; i++){//number of statements to be evaluated
        String expression = myStatement[i];//creates variable for first statement
        Stack211 <Character> st = new Stack211<Character>();//stack from generic stack
        System.out.println("infix = " + myStatement[i]);//infix statements before evaluation

        for(int j = 0; j < expression.length() && correct; j++){
            char c = expression.charAt(j);
            if(c >= '0' && c <= '9'){//checks if there is a number in the stack then adds it
                post += c;
            }
            if(c == ')' || c == '}'){//if the character is a right bracket
                boolean correct = testBrackets(c, j, st);
                post += st.pop();
            }
            if(c == '+' || c == '-' || c == '*' || c == '/'){//operator? push or pop
                post += st.pop();
                st.push(c);
            }
        }
        while(!st.isEmpty()){
            char p = st.pop();
            post += p; 
        }//end of the first statement and postfix produced
        System.out.println("Postfix = " + post);

我相信错误是在while(!st.isEmpty)发生的,但是我不知道为什么。我尝试在这里查看其他答案,但没有找到任何答案。任何帮助将不胜感激。

0 个答案:

没有答案