#输入下面的测试语句:
print(And(Proposition('P'), Proposition('Q')))
print(Not(Proposition('R')))
#我的目的是输出P/\Q,¬R,((P <-> Q) -> R),(((P <-> Q) -> R) \/ F)等结果,但是下面的代码运行不了,也不知道哪里出错。
class Proposition:
def __init__(self, name):
def __eq__(self, other):
if not isinstance(other, Proposition):
return False
return self.name == other.name
def __str__(self):
return self.name
def __hash__(self):
return hash(str(self))
class Not:
def __init__(self, formula):
def __eq__(self, other):
if not isinstance(other, Not):
return False
return self.formula == other.formula
def __str__(self):
return '~' + str(self.formula)
def __hash__(self):
return hash(str(self))
class And:
def __init__(self, formula_a, formula_b):
def __eq__(self, other):
if not isinstance(other, And):
return False
return self.formula_a == other.formula_a and \
self.formula_b == other.formula_b
def __str__(self):
return '(%s /\\ %s)' % (self.formula_a, self.formula_b)
def __hash__(self):
return hash(str(self))
class Or:
def __init__(self, formula_a, formula_b):
def __eq__(self, other):
if not isinstance(other, Or):
return False
return self.formula_a == other.formula_a and \
self.formula_b == other.formula_b
def __str__(self):
return '(%s \\/ %s)' % (self.formula_a, self.formula_b)
def __hash__(self):
return hash(str(self))
class Implies:
def __init__(self, formula_a, formula_b):
def __eq__(self, other):
if not isinstance(other, Implies):
return False
return self.formula_a == other.formula_a and \
self.formula_b == other.formula_b
def __str__(self):
return '(%s -> %s)' % (self.formula_a, self.formula_b)
def __hash__(self):
return hash(str(self))
class Equiv:
def __init__(self, formula_a, formula_b):
def __eq__(self, other):
if not isinstance(other, Equiv):
return False
return self.formula_a == other.formula_a and \
self.formula_b == other.formula_b
def __str__(self):
return '(%s <-> %s)' % (self.formula_a, self.formula_b)
def __hash__(self):
return hash(str(self))
class BoolConstant:
def __init__(self, name):
def __eq__(self, other):
if not isinstance(other, BoolConstant):
return False
return self.name == other.name
def __str__(self):
return self.name
def __hash__(self):
return hash(str(self))