以下是一个使用Python实现布尔逻辑的例子:
def and_gate(a, b):
return a and b
def or_gate(a, b):
return a or b
def not_gate(a):
return not a
def xor_gate(a, b):
return (a and not b) or (not a and b)
# 真值表函数
def truth_table(func):
print("A\tB\tOutput")
print("----------------")
for a in [False, True]:
for b in [False, True]:
output = func(a, b)
print(f"{a}\t{b}\t{output}")
# 使用真值表函数来输出and_gate的真值表
truth_table(and_gate)
# 使用真值表函数来输出or_gate的真值表
truth_table(or_gate)
# 使用真值表函数来输出not_gate的真值表
truth_table(not_gate)
# 使用真值表函数来输出xor_gate的真值表
truth_table(xor_gate)
运行以上代码,将会输出各个布尔函数的真值表。
希望这个例子能够帮助您理解布尔逻辑和真值表函数的使用。