Choosing the gate for a rule: AND, OR or NOT
Job 3 asks: how do the conditions combine? If all of them are needed, the gate is AND, with the formula Y = A·B. If at least one is enough, the gate is OR, with Y = A + B. If the result is the opposite of the condition, the gate is NOT, with Y = ¬A. Our three decisions fit in turn. The trip needs a signed slip and a free seat: AND. The library fine is waived if the book was on time or the card is gold: OR. The night lamp is on when it is not daylight: NOT. Some rules need two gates. A garden sprinkler runs when the soil is dry and it is not raining; with A for dry soil and B for rain, the formula is Y = A·¬B. After the table comes the test. A function in PHP or VB.NET must give the same answers as the table, row by row. Write the function, run it on every row, and compare.
| The rule says | Gate | Formula |
|---|---|---|
| all the conditions are needed | AND | Y = A·B |
| at least one condition is enough | OR | Y = A + B |
| the result is the opposite of the condition | NOT | Y = ¬A |
| one condition and the opposite of another | AND with NOT | Y = A·¬B |
1function tripOk(bool $slip, bool $seat): bool { 2 return $slip && $seat; 3} 4function feeWaived(bool $onTime, bool $gold): bool { 5 return $onTime || $gold; 6} 7function nightLight(bool $daylight): bool { 8 return !$daylight; 9} 10 11var_dump(tripOk(true, false)); 12var_dump(feeWaived(false, true)); 13var_dump(nightLight(true)); 14var_dump(nightLight(false));
bool(false) bool(true) bool(false) bool(true)
1Function Sprinkler(dry As Boolean, raining As Boolean) As Boolean 2 Return dry And Not raining 3End Function 4 5Console.WriteLine(Sprinkler(True, False) & " " & Sprinkler(True, True) & " " & Sprinkler(False, False) & " " & Sprinkler(False, True))
True False False False
| A | B | A∧¬B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
A greenhouse fan should run when it is hot or the door is open. Write the inputs, the output, the gate, the formula and the truth table. Then write the PHP function.