逻辑: is ( A && !(B || C)) || ( B || C ) 与 ( A || B || C ) 相同吗?
我遇到过一些 obj-c 代码,我想知道是否有办法简化它:
#if ( A && !(B || C)) || ( B || C )
这是否相同?
#if ( A || B || C )
如果没有,是否有另一种更容易阅读的方式来表述它?
[编辑] 在提出问题之前,我尝试了真值表,但我认为我一定遗漏了一些东西,因为我怀疑 Foundation.framework/Foundation.h 会采用这种更复杂的形式。有充分的理由吗?
这是原始代码(来自 Foundation.h):
#if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)) || (TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)
I've encountered some obj-c code and I'm wondering if there's a way to simplify it:
#if ( A && !(B || C)) || ( B || C )
is this the same as?
#if ( A || B || C )
If not, is there another way to formulate it that would be easier to read?
[edit]
I tried the truth table before asking the question, but thought I had to be missing something because I doubted that Foundation.framework/Foundation.h would employ this more complex form. Is there a good reason for it?
Here's the original code (from Foundation.h):
#if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)) || (TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
是的。就像其他人说的,你可以用真值表来表示。德摩根规则也能有所帮助。
但是,我认为最好的选择是使用卡诺地图。学习需要几分钟,但卡诺图可以让您始终如一地找到布尔逻辑的最小表达式。真值表可以验证最小化,但他们无法将其提供给您。
我是这样得到的:
首先,表格布局:
现在,考虑你的方程,B || C总是会导致一个事实:
这样就只剩下两种情况了。无论哪种情况,右侧的计算结果都是 false。对于 000,左侧的计算结果也为 false(0 && !(whatever) 为 false)。对于 100, 1 && !(0 ||| 0) 计算结果为 true。因此,该陈述是正确的。填写:
现在,我们只需“掩盖”所有真相即可。 “C”将覆盖底行。 “B”将覆盖中间的方块(四个值)。因此,“B || C”覆盖了除右上角方块之外的所有区域。现在,“A”将覆盖右侧的四格正方形。没关系,这是多余的。因此,“A || B || C”覆盖了所有正确的方格并忽略了唯一错误的方格。
Yes. Like others said, you can truth table it. The De Morgan rules can also help.
However, I think the best option is to use a Karnaugh Map. It takes a few minutes to learn, but Karnaugh Maps allow you to consistently find the most minimal expression for boolean logic. Truth tables can verify a minimization, but they can't give it to you.
Here's how I got it:
First, the table layout:
Now, considering your equation, B || C will always cause a truth:
This leaves only two cases. In either case, the right side evaluates to false. For 000, the left side also evaluates to false (0 && !(whatever) is false). For 100, 1 && !(0 ||| 0) evaluates to true. Thus, the statement is true. Filling in:
Now, we only need to "cover" all the truths. "C" will cover the bottom row. "B" will cover the middle square (of four values). Thus, "B || C" covers all but the top right square. Now, "A" will cover the right four-space square. It's OK that this is redundant. Thus, "A || B || C" covers all the true squares and omits the only false one.
拿起笔+纸+尝试一下,只有8种可能的输入
Get pen + paper + try it, there are only 8 possible inputs
他们是一样的。您可以使用真值表生成器来测试它。这两个表达式仅在一种情况下给出
false
,即A
、B
和C
为false< /代码>。
They are the same. You can use Truth Table Generator to test it. Both these expressions give
false
only in one case, whenA
,B
andC
arefalse
.根据最后两列,我会说是的。
Based on the last two columns, I would say yes.
是的,是一样的。使用德摩根规则:
(A && !(B || C)) || (B || C) = (A && !B && !C) || (B || C)。
因此,当 A = 1 且 B, C = 0 时,第二部分将为真。如果不是这种情况,则当 B || 时,第二部分 (B || C) 将为真。 C. 所以它等于第一个。
Yes it is the same. Using De Morgan rules:
(A && !(B || C)) || (B || C) = (A && !B && !C) || (B || C).
So the second will be true when A = 1 and B, C = 0. If that is not the case the second part (B || C) will be true when B || C. So it is equal to the first.
你也可以说:
(A && !(B || C)) || (B || C) 重写为 (A && !W) || W (1)
(1) 重写为 (A && !W) || (A || !A || W) (2)
(2) 重写 (A && !W) || (A || W) || (!A || W) (3)
(3) 重写 (A && !W) || !(A && !W) || (A || W) (4)
(4) 导致 A || W,然后A ||乙|| C
You could also say :
(A && !(B || C)) || (B || C) rewrites to (A && !W) || W (1)
(1) rewrites to (A && !W) || (A || !A || W) (2)
(2) rewrites (A && !W) || (A || W) || (!A || W) (3)
(3) rewrites (A && !W) || !(A && !W) || (A || W) (4)
(4) leads to A || W and then A || B || C
是的,这两个表达式是等价的。 (我刚刚编写了几个函数来测试所有八种可能性。)
Yes, the two expressions are equivalent. (I just wrote a couple of functions to test all eight possibilities.)