这种逻辑关系有效吗?
(X or Y) and (XX and YY) = (X and XX and YY) or (Y and XX and YY)
直觉上,等价似乎是正确的,但它不适用于我的代码。我不知道首先是等式是错误的还是我的代码有问题。
这种逻辑关系有效吗?
(X or Y) and (XX and YY) = (X and XX and YY) or (Y and XX and YY)
直觉上,等价似乎是正确的,但它不适用于我的代码。我不知道首先是等式是错误的还是我的代码有问题。
是的,这两个方程是等价的。当用 OR 拆开括号时,你确实必须 AND Xwith(XX and YY)以及Ywith(XX and YY)再次和 OR 两者。所以结果是(X and XX and YY) or (Y and XX and YY)
如果这还不够令人信服,那么我们可以为每个生成真值表。使用片段功能进行交互:
const table = document.querySelector("table");
function one(X, Y, XX, YY) {
return (X || Y) && (XX && YY);
}
function two(X, Y, XX, YY) {
return (X && XX && YY) || (Y && XX && YY);
}
function test(X, Y, XX, YY) {
const addContent = addTo(table);
addContent(X);
addContent(Y);
addContent(XX);
addContent(YY);
addContent(one(X, Y, XX, YY));
addContent(two(X, Y, XX, YY));
}
const addTo = table => {
const row = table.insertRow();
return content => row.insertCell().textContent = content;
}
//generate all permutations of 4 booleans and test them with each boolean equation
for(let i = 0; i <= 15; i++) {
const X = Boolean(i & 8);
const Y = Boolean(i & 4);
const XX = Boolean(i & 2);
const YY = Boolean(i & 1);
test(X, Y, XX, YY);
}
body {
background-color: white;
}
table {
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
}
tr > td:nth-child(5) {
text-align: right;
}
td {
padding: 0 5px;
}
<table>
<tr>
<th>X</th>
<th>Y</th>
<th>XX</th>
<th>YY</th>
<th>(X or Y) <br/>and <br/>(XX and YY)</th>
<th>(X and XX and YY) <br/>or <br/>(Y and XX and YY)</th>
</tr>
</table>
所以,每个人的结果都是一样的。