挪威森林湖畔的水手 2021-09-27 22:08 采纳率: 75%
浏览 50
已结题

代码运行不了,请问问题出在哪?

#输入下面的测试语句:
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))


  • 写回答

1条回答 默认 最新

  • CSDN专家-HGJ 2021-09-27 22:40
    关注

    这样改一下代码:

    class Proposition:
        def __init__(self, name):
            self.name=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(Proposition):
        def __init__(self, formula):
            self.formula=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(Proposition):
        def __init__(self, formula_a, formula_b):
            self.formula_a=formula_a
            self.formula_b=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(Proposition):
        def __init__(self, formula_a, formula_b):
            self.formula_a=formula_a
            self.formula_b=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(Proposition):
        def __init__(self, formula_a, formula_b):
            self.formula_a = formula_a
            self.formula_b = 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(Proposition):
        def __init__(self, formula_a, formula_b):
            self.formula_a = formula_a
            self.formula_b = 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(Proposition):
        def __init__(self, name):
            self.name=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))
    
    
    print(And(Proposition('P'), Proposition('Q')))
    print(Not(Proposition('R')))
    
    
    

    运行结果:
    F:\2021\qa\ot2>t3
    (P /\ Q)
    ~R

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月5日
  • 已采纳回答 9月27日
  • 创建了问题 9月27日

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程