括号检查Java

时间:2014-09-14 03:01:36

标签: java arrays stack

我试图看看在括号的情况下我的方程式是否平衡。但我遇到方程式问题,x=(1+2))+3。它总是有效的,即使它不是。有帮助吗?

public boolean checkEquation(String infix){
    for(int j=0;j<infix.length();j++){    // loop until the end of the line for a string (expression)
    // Check for parentheses
        if( infix.charAt(j)=='('){
            operatorStack.push(infix.charAt(j)); // push the parentheses to stack
        } 
        // Check for braces
        else if( infix.charAt(j)=='[' ){
            operatorStack.push(infix.charAt(j)); // push the braces to stack
        }
        // Check for bracket
        else if( infix.charAt(j)=='{' ){
            operatorStack.push(infix.charAt(j)); // push the bracket to stack
        }

        /* for reverse cases */
        // Check for reverse parentheses
        else if( infix.charAt(j)==')' ){
            if(operatorStack.isStackEmpty()){
                System.out.println(operatorStack.pop());
                return false;
            }
            if(operatorStack.peek()!='('){
                System.out.println(operatorStack.pop());
                return false;
            }
        }
        // Check for reverse braces
        else if( infix.charAt(j)==']' ){
            if(operatorStack.isStackEmpty()){
                return false;
            }
            if(operatorStack.peek()!='['){
                return false;
            }
        }
        // Check for reverse bracket
        else if( infix.charAt(j)=='}' ){
            if(operatorStack.isStackEmpty()){
                return false;
            }
            if(operatorStack.peek()!='{'){
                return false;
            }
        }
            // ignore other characters for now
    } // close for loop

    return operatorStack.isStackEmpty();

} // end of checkEquation method

1 个答案:

答案 0 :(得分:0)

这是适用于我的样本的更改代码。请参阅添加的更改注释。

public static Scanner input;
public static Stack<Character> operatorStack;

public static void main(String[] args) {
    operatorStack = new Stack<>(); // <- I used Stack, because I don't know what kind of stack you used
    System.out.println(checkEquation("x=(1+2))+3"));        
    System.out.println(checkEquation("x=((1+2))+3"));
    System.out.println(checkEquation("x={[((1+2))]+3}"));
    System.out.println(checkEquation("x=()   ()   ()"));
    System.out.println(checkEquation("x=()   )(   ()"));
    System.out.println("-----");
    System.out.println(checkEquation("x=((1+2)*3"));
    System.out.println(checkEquation("x=(1+2))*3"));
    System.out.println(checkEquation("x=(z*j)/(b*8)^2"));
    System.out.println(checkEquation("x=((p*2)"));
}

public static boolean checkEquation(String infix) { // <- don't mind that "static". You don't need that
    operatorStack.clear(); // <- clear the stack to remove old data
    for (int j = 0; j < infix.length(); j++) {

        // check for opening parentheses omitted

        else if (infix.charAt(j) == ')') {
            if (operatorStack.isEmpty()) { // <- different check for empty stack (due to the different stack type)
                // System.out.println(operatorStack.pop()); // <- removed unwanted pop() call
                return false;
            }
            if (operatorStack.pop() != '(') { // <- changed from peek() to pop()
                // System.out.println(operatorStack.pop());
                return false;
            }
        }
        else if (infix.charAt(j) == ']') {
            if (operatorStack.isEmpty()) { // <- different check for empty stack (due to the different stack type)
                return false;
            }
            if (operatorStack.pop() != '[') { // <- changed from peek() to pop()
                return false;
            }
        }
        else if (infix.charAt(j) == '}') { 
            if (operatorStack.isEmpty()) { // <- different check for empty stack (due to the different stack type)
                return false;
            }
            if (operatorStack.pop() != '{') { // <- changed from peek() to pop()
                return false;
            }
        }
    }
    return operatorStack.isEmpty();
}

测试结果是:

false
true
true
true
false
-----
false
false
true
false

如果您发现某个表达不起作用,请随时添加评论。