编程介的小学生 2019-09-14 02:47 采纳率: 20.5%
浏览 144

逻辑的问题,Boolean Logic

Description

Propositions are logical formulas consisting of proposition symbols and connecting operators. They are recursively defined by the following rules:
All proposition symbols (in this problem, lower-case alphabetic characters, e.g., a and z) are propositions.
If P is a proposition, (!P) is a proposition, and P is a direct subformula of it.
If P and Q are propositions, (P&Q), (P|Q), (P-->Q), and (P<->Q) are propositions, and P and Q are direct subformulas of them.
Nothing else is a proposition.

The operations !, &, |, -->, and <-> denote logical negation, conjunction, disjunction, implication, and equivalence, respectively. A proposition P is a subformula of a proposition R if P=R or P is a direct subformula of a proposition Q and Q is a subformula of R.
Let P be a proposition and assign boolean values (i.e., 0 or 1) to all proposition symbols that occur in P. This induces a boolean value to all subformulas of P according to the standard semantics of the logical operators:
negation conjunction disjunction implication equivalence
!0=1 0&0=0 0|0=0 0-->0=1 0<->0=1
!1=0
0&1=0 0|1=1 0-->1=1 0<->1=0
1&0=0 1|0=1 1-->0=0 1<->0=0
1&1=1 1|1=1 1-->1=1 1<->1=1

This way, a value for P can be calculated. This value depends on the choice of the assignment of boolean values to the proposition symbols. If P contains n different proposition symbols, there are 2n different assignments. To evaluate all possible assignments we may use truth tables.

A truth table contains one line per assignment (i.e., 2n lines in total). Every line contains the values of all subformulas under the chosen assignment. The value of a subformula is aligned with the proposition symbol, if the subformula is a proposition symbol, and with the center of the operator otherwise.
Input

The input contains several test cases, each on a separate line. Every test case denotes a proposition and may contain arbitrary amounts of spaces in between. The input file terminates immediately after the newline symbol following the last test case.
Output

For each test case generate a truth table for the denoted proposition. Start the truth table by repeating the input line. Evaluate the proposition (and its subformulas) for all assignments to its variables, and output one line for each assignment. The line must have the same length as the corresponding input line and must consist only of spaces and the characters 0 and 1. Output an empty line after each test case.

Let s1,...,sn be the proposition symbols in the denoted proposition sorted in alphabetic order. Then, all assignments of 0 to s1 must precede the assignments of 1 to s1. Within each of these blocks of assignments, all assignments of 0 to s2 must precede the assignments of 1 to s2, and so on.
Sample Input

((b --> a) <-> ((! a) --> (! b)))
((y & a) - ->(c |c))
Sample Output

((b --> a) <-> ((! a) --> (! b)))
0 1 0 1 1 0 1 1 0

1 0 0 1 1 0 0 0 1

0 1 1 1 0 1 1 1 0

1 1 1 1 0 1 1 0 1

((y & a) - ->(c |c))
0 0 0 1 0 00

1 0 0 1 0 00

0 0 0 1 1 11

1 0 0 1 1 11

0 0 1 1 0 00

1 1 1 0 0 00

0 0 1 1 1 11

1 1 1 1 1 11

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
    • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
    • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
    • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
    • ¥20 腾讯企业邮箱邮件可以恢复么
    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?