我需要帮助否定这种逻辑

时间:2013-09-17 17:55:43

标签: if-statement boolean-logic

(previous = 4 || previous = 5) && current = 2的反面是什么?

我知道我可以把它写成!((previous = 4 || previous = 5) && current = 2),但现在它变成了一种能够找到答案的东西。如何在括号内分配负数?

这就是我的想法,但对我来说似乎并不合适。

!((previous = 4 || previous = 5) && current = 2

!(previous = 4 || previous = 5) || current != 2

(previous != 4 && previous != 5) || current != 2

2 个答案:

答案 0 :(得分:2)

德摩根定律指出,当你采用表达式的补语时(也就是当你否定它时),你可以简单地交换所有的OR和AND,并否定每个术语以获得所需的结果。

例如

!(A && B) = !A || !B

!(A || B) = !A && !B

!(A && !B) = !A || B

因此,应用这两个简单的规则,

!((previous = 4 || previous = 5) && current = 2) 
=
!(previous = 4 || previous = 5) || current != 2
=
(previous != 4 && previous != 5) || current != 2

(多行分解,因为一行太长)

注意上面,因为我们有一个表达式作为其中一个术语,我们递归地应用了DeMorgan定律(准确地说是两次)。

更多例子

!(A > B || A + B == 0) = A <= B && A + B != 0

!(A >= 2 && A < 8) = A < 2 || A >= 8

答案 1 :(得分:0)

您可以使用此等式!(a && b) = !a || !b

获得答案

一步一步:

!((previous = 4 || previous = 5) && current = 2)
!(previous = 4 || previous = 5) || !(current = 2)
(previous != 4 && previous != 5) || current != 2