首先创建一个类,利用python自身的优势对表达式进行解析
# &为合取,v为析取,~为非,>为条件联结词,*为双条件联结词
class Variable:
def __init__(self, value=0):
self.value = value
# 返回两个对象的合取
def __and__(self, other):# 对 & 运算符的定义
return Variable(1) if (self.value==1 and other.value==1) else Variable(0)
def __or__(self, other):# 对 | 运算符的定义
return Variable(0) if self.value==other.value==0 else Variable(1)
# 对象的条件连接
def __gt__(self, other):# 对 > 运算符的定义
return Variable(0) if (self.value==1 and other.value==0) else Variable(1)
# 对象的取反
def __invert__(self):# 对 ~ 运算符的定义
return Variable(1) if self.value==0 else Variable(0)
# 对象的双条件连接
def __mul__(self, other):# 对 * 运算符的定义
return Variable(1) if self.value==other.value else Variable(0)
def __str__(self):# 对打印显示的重载
return str(