在java中使用堆栈时不会产生预期的输出Truthtable

ozxc1zmp  于 2023-02-18  发布在  Java
关注(0)|答案(1)|浏览(83)

这是我的代码:导入java实用程序 *;
公共类真值表{

public static boolean evaluateExpression(String expression, boolean p, boolean q) {
    Stack<Boolean> stack = new Stack<>();
    Stack<Character> opStack = new Stack<>();

    for (int i = 0; i < expression.length(); i++) {
        char c = expression.charAt(i);
        if (c == 'p') {
            stack.push(p);
        } else if (c == 'q') {
            stack.push(q);
        } else if (c == '¬') {
            boolean b = stack.pop();
            stack.push(!b);
        } else if (c == '∧') {
            // Check if the next character is a ¬ character.
            if (i + 1 < expression.length() && expression.charAt(i + 1) == '¬') {
                // If the next character is a ¬ character, pop the top of the stack and negate it,
                // then push the conjunction of the negated value and the value of q.
                boolean b = stack.pop();
                stack.push(!(b && q));
                // Increment the index to skip the ¬ character.
                i++;
            } else {
                // If the next character is not a ¬ character, simply push the conjunction of the
                // value of p and the value of q.
                stack.push(stack.pop() && q);
            }
        } else if (c == '∨' || c == '→' || c == '↔' || c == '⊕' || c == '⊼' || c == '⊽') {
            while (!opStack.isEmpty() && getPrecedence(c) <= getPrecedence(opStack.peek())) {
                char op = opStack.pop();
                applyOperator(op, stack);
            }
            opStack.push(c);
        }
    }

    while (!opStack.isEmpty()) {
        char op = opStack.pop();
        applyOperator(op, stack);
    }

    return stack.pop();
}

private static void applyOperator(char op, Stack<Boolean> stack) {
    boolean b1 = stack.pop();
    boolean b2 = stack.pop();
    switch (op) {
        case '∧':
            stack.push(b1 && b2);
            break;
        case '∨':
            stack.push(b1 || b2);
            break;
        case '→':
            stack.push(!b1 || b2);
            break;
        case '↔':
            stack.push(b1 == b2);
            break;
        case '⊕':
            stack.push(b1 != b2);
            break;
        case '⊼':
            stack.push((b1 && b2) || (!b1 && b2) || (b1 && !b2));
            break;
        case '⊽':
            stack.push(b1 && b2 && stack.pop());
            break;
        case '¬':
            stack.push(!b1);
            break;
    }
}

private static int getPrecedence(char op) {
    switch (op) {
        case '¬':
            return 3;
        case '∧':
            return 2;
        case '∨':
            return 1;
        case '→':
            return 0;
        case '↔':
            return 0;
        case '⊕':
            return 1;
        case '⊼':
            if (op == '∨' || op == '→' || op == '↔' || op == '⊕') {
                return 2;
            } else {
                return 3;
            }
        case '⊽':
            return 2;
        default:
            return -1;
    }
}

public static void main(String[] args) {
    String expression = "pq¬∨pq∧→";
    System.out.println("p\tq\t(" + expression + ")");
    for (boolean p : new boolean[]{true, false}) {
        for (boolean q : new boolean[]{true, false}) {
            System.out.println(p + "\t" + q + "\t" + evaluateExpression(expression, p, q));
        }
    }
}

}
预期的结果是真假真假,但它打印出假真假真,我一直在试图找出如何修复它,但我就是不能。将感谢任何帮助
我试过改变优先级,但它只产生输出True True True

g6ll5ycj

g6ll5ycj1#

我不指望很多人能帮助你,特别是如果你看不到逻辑错误,而你是作者!
编写这样的代码时,最好的选择是编写单元测试。首先测试实现的一部分,然后一点一点地给予算法表达式,并逐步处理更复杂的情况。
请注意,使用private static方法可能会影响您测试部分代码或模拟部分代码的能力。你投资是值得的。

相关问题