编写正确的逻辑表达式

Write logical expression which is true

本文关键字:表达式      更新时间:2023-10-16

当最多两个数字 A、B、C 为非正数时,编写为真的逻辑表达式。你能回答这个问题吗?

老师说答案是!(A =< 0 && B =< 0 && C <= 0)A > 0 || B > 0 || C > 0.

我还是不明白她的回答,你能帮我吗?

现在,问题指出 A、B、C 中最多有 2 个是非正的。因此,这意味着包含 3 个数字并返回 true 的条件将是 1.

!(A =< 0 && B =< 0 && C <= 0)

在这里,我们知道 A、B 或 C 中的任何一个都大于 0。因此,它将返回 true。 阿拉伯数字。

A > 0 || B > 0 || C > 0

在这里,同样的理由成立,其中至少有一个会返回为真。

你的老师应用了德摩根定律,它说:

not (A or B) = not A and not B; 

not (A and B) = not A or not B

查看 https://en.wikipedia.org/wiki/De_Morgan%27s_laws 的完整解释

工作代码。

#include <iostream>
using namespace std;
//Function for logic
bool myFun(int a,int b,int c)
{
bool conditionFirst=!(a <= 0 && b <= 0 && c <= 0);
bool condintionSecond=(a > 0 || b > 0 || c > 0);
if(conditionFirst==true && condintionSecond==true)
return true;
else
return false;
}
int main() {
int a,b,c;
cin>>a>>b>>c;
if(myFun(a,b,c))
cout<<"True";
else
cout<<"False";
}