Lesson 52: Logic from words
此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
From words to logic
- Exam questions describe a system in words. You write a logic expression, then a truth table.
- Find the inputs, then the words that join them: and, or, not.
- Here three switches are the sensors and an LED is the alarm.
Words for this lesson
| Word | Meaning |
|---|---|
| input | a sensor value, 0 or 1 |
| logic expression | inputs joined with AND, OR, NOT |
| truth table | the output for every row of inputs |
English
Words to logic
| In the question | Logic | C++ |
|---|---|---|
| it is dark (L = 0) | NOT L | !L |
| and | AND | && |
| motion or sound or both | M OR S | M || S |
- Three inputs give 2 × 2 × 2 = 8 rows.
English
In the exam
- 0478/23/M/J/25 Q7(b): the alarm sounds if it is dark and motion or sound or both are detected. (i) Mark scheme: A = NOT L AND (M OR S) (longer correct forms also get the marks). (ii) The truth table: A = 1 only in rows 001, 010 and 011; four marks for eight correct outputs.
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
A security system has three inputs. A switch that is on gives 1. L = 1 means light (0 means dark), M = 1 means motion is detected, S = 1 means sound is detected. The alarm on pin 13 sounds if it is 黑暗 and motion 或 sound 或 both are detected. The program reads L, M and S for you. Write the logic expression in C++ and switch the alarm on or off. Check tries all 8 rows of the truth table.
点击“运行”查看此处输出。
难度 4/5 · 今日首次完成可获 280 金币及少量额外奖励
Let the Arduino write the truth table. Finish the function alarm(L, M, S) so it returns the alarm output. The loops try every row and print it like 0 1 1 -> 1.
点击“运行”查看此处输出。