布尔逻辑和真值表
创始人
2024-12-23 21:00:25
0

下面是一个使用Python编写的布尔逻辑和真值表的代码示例:

# 定义布尔逻辑运算函数
def AND(x, y):
    return x and y

def OR(x, y):
    return x or y

def NOT(x):
    return not x

def XOR(x, y):
    return x != y

# 定义真值表生成函数
def truth_table(func):
    inputs = [(True, True), (True, False), (False, True), (False, False)]
    outputs = [func(x, y) for x, y in inputs]

    print("x  y  |  Output")
    print("----------------")
    for i in range(len(inputs)):
        x, y = inputs[i]
        output = outputs[i]
        print(f"{x}  {y}  |  {output}")

# 测试布尔逻辑函数
print("AND Truth Table:")
truth_table(AND)

print("\nOR Truth Table:")
truth_table(OR)

print("\nNOT Truth Table:")
inputs = [True, False]
outputs = [NOT(x) for x in inputs]
for i in range(len(inputs)):
    x = inputs[i]
    output = outputs[i]
    print(f"{x}  |  {output}")

print("\nXOR Truth Table:")
truth_table(XOR)

运行上述代码,将会输出以下结果:

AND Truth Table:
x  y  |  Output
----------------
True  True  |  True
True  False  |  False
False  True  |  False
False  False  |  False

OR Truth Table:
x  y  |  Output
----------------
True  True  |  True
True  False  |  True
False  True  |  True
False  False  |  False

NOT Truth Table:
True  |  False
False  |  True

XOR Truth Table:
x  y  |  Output
----------------
True  True  |  False
True  False  |  True
False  True  |  True
False  False  |  False

这个代码示例中,我们定义了四个布尔逻辑函数:AND、OR、NOT和XOR。然后,我们编写了一个名为truth_table的函数,该函数接受一个布尔逻辑函数作为参数,并生成相应的真值表。我们使用print语句输出真值表的结果。

最后,我们使用这些函数分别生成了AND、OR、NOT和XOR运算的真值表,并进行了打印输出。

相关内容

热门资讯

透视中牌率(wepOKE)透明... 透视中牌率(wepOKE)透明挂辅助器(aapoker透明挂)总是真的有挂(详细透视力荐教程)1、不...
wepower有外挂!德州线上... wepower有外挂!德州线上扑克辅助,德州ai辅助神器wpk,规律教程(有挂攻略)您好,wepow...
透视教学(wepOke)透明挂... 透视教学(wepOke)透明挂辅助插件(wepoke是真的有挂)其实有挂(详细透视揭秘教程);wep...
wepoke有挂!aapoke... wepoke有挂!aapoker钻石代理有挂的,德州ai辅助,技巧教程(有挂教程);是一款可以让一直...
透视了解(wEpoke)透视辅... 1、透视了解(wEpoke)透视辅助工具(WePoKe透明挂)真是存在有挂(详细透视解说技巧)(UU...
aa扑克辅助!aapoker发... 自定义aa扑克辅助系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不管你是...
透视辅助(wEpoke)透明挂... 此外,数据分析德州(wepoke计算辅助)辅助神器app还具备辅助透视行为开挂功能,通过对客户wep...
aapoker辅助!wpk德州... aapoker辅助!wpk德州ai辅助神器,aa扑克软件,我来教教你(有挂辅助挂);人气非常高,ai...
透视美元局(wePOke)透视... 透视美元局(wePOke)透视辅助挂(wepoke的确有挂)从前真的是有挂(详细透视细节方法);科技...
微扑克ai机器人!wpk俱乐部... 微扑克ai机器人!wpk俱乐部会员管理软件,wepokeai代打逻辑,微扑克教程(有挂方法)是一款可...