0

我有一个问题,我在这个函数中找不到错误,它有时可以很好地处理任何输入,但是当输入中有括号时它会被缓存{我想知道这段代码的错误在哪里以及如何修复它,并且有没有更好的方法来代替这种方式}

    public static String Converting_infix_expressions_to_postfix_expressions(String infix) throws Exception{
    StringTokenizer st = new StringTokenizer(infix);
    int numOF_tokens = st.countTokens();
    String postfix = "" ;
    for (int i = 1; i <= numOF_tokens; i++) {
        String term = st.nextToken();
        try {  // if it is an Float there is no problem will happen
            float x =  Float.parseFloat(term);
            postfix += x +" " ;
            System.out.println("term is number " + term);
        } catch (Exception e) {
            System.out.println("term is symbol " + term);
            if(stack.isEmpty())
                stack.push(term);
            else if(term == "(")
                stack.push(term);
            else if(term == ")"){
                while((String)stack.peek() != "(")
                    postfix += stack.pop() +" ";
                stack.pop();
            }

            else{
                int x = 0,y = 0;
                switch(term){
                case "+": x = 1; break;
                case "-": x = 1; break;
                case "*": x = 2; break;
                case "/": x = 2; break;
                }
                switch((String)stack.peek()){
                case "+": y = 1; break;
                case "-": y = 1; break;
                case "*": y = 2; break;
                case "/": y = 2; break;
                }
                if(x > y)
                    stack.push(term);
                else {
                    int x1 = x , y1 = y;
                    boolean puchedBefore = false;
                    while(x1 <= y1){
                        postfix += stack.pop() +" ";
                        if(stack.isEmpty() || stack.peek() == "(" ){
                            stack.push(term);
                            puchedBefore = true;
                            break;
                        }
                        else{
                            switch(term){
                            case "+": x1 = 1; break;
                            case "-": x1 = 1; break;
                            case "*": x1 = 2; break;
                            case "/": x1 = 2; break;
                            }
                            switch((String)stack.peek()){
                            case "+": y1 = 1; break;
                            case "-": y1 = 1; break;
                            case "*": y1 = 2; break;
                            case "/": y1 = 2; break;
                            }
                        }
                    }
                    if(!puchedBefore)
                        stack.push(term);
                }   
            }
        }
    }
    while(!stack.isEmpty()){
        postfix += stack.pop() +" ";
    }
    System.out.println("The postfix expression is : " + postfix);
    return postfix;
}
4

1 回答 1

1

您的代码有几个问题。

  1. 您应该制作一个自定义字符串标记器,因为括号和数字之间可能没有空格。例如:(5 + 6)
  2. 未正确使用 try-catch 块。考虑一下,首先检查该字段是否是符号,然后继续将其解析为浮点数。这样您就可以避免大部分代码出现在 catch 块中。
  3. 您所指的错误可以通过对第 18 行的以下更改来修复。

    while(!stack.isEmpty() && (String)stack.peek() != "(")

于 2012-05-03T15:42:55.530 回答