编写 Python 程序,求命题公式┐(Q∧R)→(┐Q∨┐R)及 P→(Q→R)的真值表,使之能在pycharm运行
时间: 2024-03-05 20:48:17 浏览: 27
以下是Python代码,可以求出命题公式┐(Q∧R)→(┐Q∨┐R)及 P→(Q→R)的真值表:
```python
# 求命题公式┐(Q∧R)→(┐Q∨┐R)的真值表
print('┐(Q∧R)→(┐Q∨┐R)')
print('P\tQ\tR\tQ∧R\t┐Q\t┐R\t┐Q∨┐R\t┐(Q∧R)\t┐(Q∧R)→(┐Q∨┐R)')
for P in [True, False]:
for Q in [True, False]:
for R in [True, False]:
Q_and_R = Q and R
not_Q = not Q
not_R = not R
not_Q_or_not_R = not_Q or not_R
not_Q_and_R = not Q_and_R
result = not_Q_and_R <= not_Q_or_not_R
print('{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}'.format(
P, Q, R, Q_and_R, not_Q, not_R, not_Q_or_not_R, not_Q_and_R, result))
# 求命题公式 P→(Q→R) 的真值表
print('\nP→(Q→R)')
print('P\tQ\tR\tQ→R\tP→(Q→R)')
for P in [True, False]:
for Q in [True, False]:
for R in [True, False]:
Q_implies_R = not Q or R
result = P <= Q_implies_R
print('{}\t{}\t{}\t{}\t{}'.format(P, Q, R, Q_implies_R, result))
```
你可以复制这段代码,粘贴到PyCharm中,运行后即可得到这两个命题公式的真值表。