2013-05-15 128 views
0

任何人都可以解释为什么这两个陈述不相等?如果不是(a而不是b),如果(不是a和b)

if not(a and not b): 
// do some stuff 


if (not a and b): 
// do some stuff 

我试图通过将第一条语句更改为第二条语句来使我的程序更易于理解,但它不起作用。我不完全明白为什么。

+0

两件事情:1)确认的优先级不同的逻辑运算符是你认为它们是什么(包括左向右关联)2)如果你真的很困惑,做一个真值表(所有可能的输入的结果)。验证并比较。 – Patashu

+0

你忘了完全否定,如果你编写一个程序来遍历真值表,那么会更容易 – 2013-05-15 01:49:58

回答

5

你应该看看德摩根Thereom,其中一半是(一)

not(p and q) -> not(p) or not(q) 

就这个如何适用于你而言[R情况,只是aqnot(b)替换p

not(a and not b) -> not(a) or not(not(b)) 
       -> not(a) or b 

(一)另一半是:

not(p or q) -> not(p) and not(q) 
1

if not(a and not b)if (not a) or b相同,不是你写的。

+0

你是怎么得到这个结果的?是否有重写规则? –

+1

@ 9位 - 是的规则是德摩根定律 - http://en.wikipedia.org/wiki/De_Morgan's_laws – shf301

1

您还需要翻转“和”对“或”因De Morgan's law

if not(a and not b) 

成为

if (not a or b) 
+0

由于摩根定律,您可以添加到您的答案' – Bill

+1

@bill done,cheers – Craig

相关问题