Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 扭转逻辑表达式_C++_If Statement_Logic - Fatal编程技术网

C++ 扭转逻辑表达式

C++ 扭转逻辑表达式,c++,if-statement,logic,C++,If Statement,Logic,我有以下代码: bool s = true; for (...; ...; ...) { // code that defines A, B, C, D // and w, x, y, z if (!(A < w) && s == true) { s = false; } if (!(B < x) && s == true) { s = false; }

我有以下代码:

bool s = true;

for (...; ...; ...) {
    // code that defines A, B, C, D 
    // and w, x, y, z

    if (!(A < w) && s == true) {
        s = false;
    }

    if (!(B < x) && s == true) {
        s = false;
    }

    if (!(C < y) && s == true) {
        s = false;
    }

    if (!(D < z) && s == true) {
        s = false;
    }
}
但是,由于上面的代码正在工作,因此无法正常工作。我知道在逻辑上有什么想法是错误的,但我不知道在哪里。安比昂看到我可能明显的错误了吗

编辑:添加了三个以上的if statemets。因为它们被注释掉了,所以错过了它们。

说,您还应该将
&
更改为
|
!(A
A>=x
相同,因此您的函数根本没有反转逻辑。您需要使用
A

我可能不会费心检查循环中
s
的当前状态。要么你在翻动它,要么你不在翻动它。除非有什么理由继续循环,否则我可能会在翻动
s

找到答案后打断
。我的问题的正确代码是:

bool s = false;

for (...; ...; ...) {
    // code that defines A, B, C, D 
    // and w, x, y, z

    if (!(A >= w || s == false)) {
        s = true;
    }

    if (!(B >= x || s == false)) {
        s = true;
    }

    if (!(C >= y || s == false)) {
        s = true;
    }

    if (!(D >= z || s == false)) {
        s = true;
    }
}

谢谢@EJP的提示

设置
s
的循环体部分在逻辑上等同于:

if(A >= w || B >= x || C >= y || D >= z)
    s = false;
s &= some_function(A, B, C, D, w, x, y, z);
s |= some_other_function(A, B, C, D, w, x, y, z);
将条件抽象为以下内容:

if(A >= w || B >= x || C >= y || D >= z)
    s = false;
s &= some_function(A, B, C, D, w, x, y, z);
s |= some_other_function(A, B, C, D, w, x, y, z);
您要将其更改为:

if(A >= w || B >= x || C >= y || D >= z)
    s = false;
s &= some_function(A, B, C, D, w, x, y, z);
s |= some_other_function(A, B, C, D, w, x, y, z);
在第一种情况下,
s
在循环后为true,如果
some_函数
在循环的每次迭代中返回false。在第二个true中,
s
在循环后为true,如果
some_other_函数
在循环的任何迭代中返回true

some_other_函数
只能在
some_函数
在任何迭代中返回true时返回true。但是
some_other_函数
只能访问当前迭代中的值。因此,有效的
some\u other\u函数
不可能存在


这是假设在这两种情况下,
s
在循环后必须具有相同的值。否则,您可以在与
s

相关的所有位置轻松地交换
true
false
,然后它将进入第一个循环,如果总是在第一个循环中。请注意,
A和&B
的否定不是
!A&&!B
,但是
!A | | |!B) 
@thriqon但他也可以将s设置为true,因为它将始终输入if。更改条件(As==false
始终为true,因此
!(A>=w | | s==false)
始终为false且
s=true