多条件IF语句的奇怪行为

The*_*ryX 3 c++ boolean-logic

我注意到,在组合!=&&运算符时,我偶然发现了if函数的奇怪行为.举例来说,我有两个std::string对我要检查,如果他们不是对象"ffff""0000"然后继续一些计算.

代码如下所示:

std::string sentData("ffff")
std::string sentAddr("060C")
if (sentData == "ffff") 
    cout << "A 1. Same" << endl;
else 
    cout << "A 1. Different" << endl;
if (sentAddr == "0000") 
    cout << "A 2. Same" << endl;
else 
    cout << "A 2. Different" << endl;
if ((sentData == "ffff") && (sentAddr == "0000")) 
    cout << "A &&. Same" << endl;
else 
    cout << "A &&. Different" << endl;

if (sentData != "ffff") 
    cout << "B 1. Different" << endl;
else 
    cout << "B 1. Same" << endl;
if (sentAddr != "0000") 
    cout << "B 2. Different" << endl;
else 
    cout << "B 2. Same" << endl;
if ((sentData != "ffff") && (sentAddr != "0000")) 
    cout << "B &&. Different" << endl;
else 
    cout << "B &&. Same" << endl;
Run Code Online (Sandbox Code Playgroud)

理论上,我认为它应该产生相同的结果......但事实并非如此.最后,如果函数将&&作为||(我不知道为什么),所以我进入最后的if语句,即使只有一个答案是正确的.下面有一些答案.

按原样工作:

sentData: ffff  sentAddr: 0000
A 1. Same
A 2. Same
A &&. Same
B 1. Same
B 2. Same
B &&. Same

sentData: ffdf  sentAddr: 060e
A 1. Different
A 2. Different
A &&. Different
B 1. Different
B 2. Different
B &&. Different
Run Code Online (Sandbox Code Playgroud)

工作错误:

sentData: ffff  sentAddr: 060c
A 1. Same
A 2. Different
A &&. Different
B 1. Same
B 2. Different
B &&. Same

sentData: fb8c  sentAddr: 0000
A 1. Different
A 2. Same
A &&. Different
B 1. Different
B 2. Same
B &&. Same
Run Code Online (Sandbox Code Playgroud)

所以,如果我用sentData: fb8c sentAddr: 0000if ((sentData != "ffff") && (sentAddr != "0000"))我得到的if (TRUE && FALSE),但显然它还是进入了if声明.

问:有谁知道为什么?

我问这个是因为我想避免像这样编写丑陋的代码:

if (!((sentData == "ffff") && (sentAddr == "0000"))) {
    // do stuff
}
Run Code Online (Sandbox Code Playgroud)

要么

if ((sentData == "ffff") && (sentAddr == "0000")) {
    // nothing to do
} else {
    // do stuff
}
Run Code Online (Sandbox Code Playgroud)

Cin*_*lue 9

你认为这!a && !b是不正确的!(a && b).

这是因为在布尔运算上分配或取消分配否定需要将布尔运算转换为其对偶以及否定单个内部项,反之亦然.

这被称为德摩根定律,是布尔代数中的基本定律.

因此双重:

(a && b)
Run Code Online (Sandbox Code Playgroud)

实际上是:

(!a || !b)
Run Code Online (Sandbox Code Playgroud)

和双重:

(!a && !b)
Run Code Online (Sandbox Code Playgroud)

实际上是:

(a || b)
Run Code Online (Sandbox Code Playgroud)

这是工作代码:

#include <iostream>
#include <string>

int main()
{
    using namespace std;

    std::string sentData("ffff");
    std::string sentAddr("060C");

    auto a = (sentData == "ffff");  // true
    auto b = (sentAddr == "0000");  // false

    if (a)              cout << "A 1. Same" << endl;
    else                cout << "A 1. Different" << endl;

    if (b)              cout << "A 2. Same" << endl;
    else                cout << "A 2. Different" << endl;

    if (a && b)         cout << "A &&. Same" << endl;
    else                cout << "A &&. Different" << endl;

    if (!a)             cout << "B 1. Different" << endl;
    else                cout << "B 1. Same" << endl;

    if (!b)             cout << "B 2. Different" << endl;
    else                cout << "B 2. Same" << endl;

    if ((!a || !b))     cout << "B &&. Different" << endl;
    else                cout << "B &&. Same" << endl;
}
Run Code Online (Sandbox Code Playgroud)

在Coliru上看到它.