逻辑: is ( A && !(B || C)) || ( B || C ) 与 ( A || B || C ) 相同吗?

发布于 2024-10-12 23:50:54 字数 482 浏览 4 评论 0原文

我遇到过一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

ˇ宁静的妩媚 2024-10-19 23:50:54

是的。就像其他人说的,你可以用真值表来表示。德摩根规则也能有所帮助。

但是,我认为最好的选择是使用卡诺地图。学习需要几分钟,但卡诺图可以让您始终如一地找到布尔逻辑的最小表达式。真值表可以验证最小化,但他们无法将其提供给您。

我是这样得到的:

首先,表格布局:

         AB
     00   01   11   10
  0|    |    |    |    |
C 1|    |    |    |    |

现在,考虑你的方程,B || C总是会导致一个事实:

         AB
     00   01   11   10
  0|    |  T |  T |    |
C 1|  T |  T |  T |  T |

这样就只剩下两种情况了。无论哪种情况,右侧的计算结果都是 false。对于 000,左侧的计算结果也为 false(0 && !(whatever) 为 false)。对于 100, 1 && !(0 ||| 0) 计算结果为 true。因此,该陈述是正确的。填写:

         AB
     00   01   11   10
  0|  F |  T |  T |  T |
C 1|  T |  T |  T |  T |

现在,我们只需“掩盖”所有真相即可。 “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:

         AB
     00   01   11   10
  0|    |    |    |    |
C 1|    |    |    |    |

Now, considering your equation, B || C will always cause a truth:

         AB
     00   01   11   10
  0|    |  T |  T |    |
C 1|  T |  T |  T |  T |

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:

         AB
     00   01   11   10
  0|  F |  T |  T |  T |
C 1|  T |  T |  T |  T |

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.

凉城 2024-10-19 23:50:54

拿起笔+纸+尝试一下,只有8种可能的输入

Get pen + paper + try it, there are only 8 possible inputs

迷离° 2024-10-19 23:50:54

他们是一样的。您可以使用真值表生成器来测试它。这两个表达式仅在一种情况下给出 false,即 ABCfalse< /代码>。

They are the same. You can use Truth Table Generator to test it. Both these expressions give false only in one case, when A, B and C are false.

半枫 2024-10-19 23:50:54
A | B | C | (B || C) | (!(B || C)) | (A && !(B || C)) | (A && (!(B || C)) || (B || C) | (A || B || C)
------------------------------------------------------------------------------------------------------
T | T | T |     T    |       F     |         F        |                 T             |         T      
T | T | F |     T    |       F     |         F        |                 T             |         T 
T | F | T |     T    |       F     |         F        |                 T             |         T 
T | F | F |     F    |       T     |         T        |                 T             |         T 
F | T | T |     T    |       F     |         F        |                 T             |         T 
F | T | F |     T    |       F     |         F        |                 T             |         T 
F | F | T |     T    |       F     |         F        |                 T             |         T 
F | F | F |     F    |       T     |         F        |                 F             |         F 

根据最后两列,我会说是的。

A | B | C | (B || C) | (!(B || C)) | (A && !(B || C)) | (A && (!(B || C)) || (B || C) | (A || B || C)
------------------------------------------------------------------------------------------------------
T | T | T |     T    |       F     |         F        |                 T             |         T      
T | T | F |     T    |       F     |         F        |                 T             |         T 
T | F | T |     T    |       F     |         F        |                 T             |         T 
T | F | F |     F    |       T     |         T        |                 T             |         T 
F | T | T |     T    |       F     |         F        |                 T             |         T 
F | T | F |     T    |       F     |         F        |                 T             |         T 
F | F | T |     T    |       F     |         F        |                 T             |         T 
F | F | F |     F    |       T     |         F        |                 F             |         F 

Based on the last two columns, I would say yes.

夜司空 2024-10-19 23:50:54

是的,是一样的。使用德摩根规则:

(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.

爱给你人给你 2024-10-19 23:50:54

你也可以说:

(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

少跟Wǒ拽 2024-10-19 23:50:54

是的,这两个表达式是等价的。 (我刚刚编写了几个函数来测试所有八种可能性。)

Yes, the two expressions are equivalent. (I just wrote a couple of functions to test all eight possibilities.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文