鱼C论坛

 找回密码
 立即注册
查看: 1495|回复: 2

python 的电路难题

[复制链接]
发表于 2023-9-8 09:41:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
class logicalgate:
    def __init__(self,label):#这是最通用的类logicgate
        self.label = label
        self.output = None
    def getlabel(self):#逻辑门要有标签,要有能获得标签的方法,也要能获得输出值的方法,要有输出值
        return self.label
    def getoutput(self):
        self.output = self.performgatelogic()#目前还不用知道该函数的用法,因为每种逻辑门的算法都不一样
        return self.output


class Binarygate(logicalgate):#继承父类的方法
    def __init__(self, label):#逻辑门要有标签,logicgate的子类Binarygate也不列外,所以这里要调用其父类的创造标签的方法,即super().__init__(labe),super()为其父类,否则就不能创造标签。
        super().__init__(label)#调用super().__init__(labe)后,就默认有self.label = label,self.output = None,父类的方法(函数)也有继承
        self.pinA = None #Binargate有两个输入接口,分别是self.pinA 和self.pinB,,默认为None,即没接入状态一个输出接口
        self.pinB = None
    def getpinA(self): #要得到此逻辑门的输出值
        if self.pinA == None:
            return int(input("Enter pinA -->" + self.label ))
        else:
            return self.pinA.getFrom().getoutput()


   
    def getpinB(self):
        if self.pinB == None:
            return int(input("Enter pinB -->" + self.label ))
        else:
            return self.pinB.getFrom().output()

class Unarygate(logicalgate): #只有一个输入值,一个输出值,是logicalgate的子类
    def __init__(self, label):
        super().__init__(label)

        self.pin = None

    def getpin(self):
        return int(input("Enter pin -->" + self.label ))
         

class AndGate(Binarygate): #要有两个输入值,并且只有都为一时才返回一
    def __init__(self, label):#其调用它的父类,他的父类有调用它的父类,所以共继承有两个输入值,一个输出值,一个标签,方法也有继承
        super().__init__(label)

    def performgatelogic(self):
        a = self.getpinA()
        b = self.getpinB()
        if a== 1 and b==1:
            return 1
        else:
            return 0

    def setNextPin(self, source): #source 这里指的是conector的是列对象
        if self.pinA == None:
            self.pinA = source
        else:
            if self.pinB == None:
                self.pinB = source
            else:
                raise RuntimeError("你是啥插拔")




            

class conector:  #用于连接两个逻辑门
   
    def __init__(self, fgate, tgate):
        self.fgate = fgate
        self.tgate = tgate

        tgate.setNextPin(self)


    def getFrom(self):
        return self.fgate

    def getTo(self):
        return self.tgate
   
      


        
e = AndGate("rr")
d = AndGate("rrrrr")
a = conector(e, d)
print(d.getoutput()) #从最后一个逻辑门开始运行,逆向思维


为什么print(d.getoutput()),反而会打印出现Enter pinA -->rr,为什么是e的标签rr而不是d的标签rrrr
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-9-8 09:41:55 | 显示全部楼层
在代码中,我们创建了两个AndGate对象e和d,并使用conector对象a将它们连接起来。然后我们打印d的输出值。

在AndGate类中,performgatelogic方法定义了逻辑门的行为,根据输入值计算输出值。在getpinA和getpinB方法中,如果pinA或pinB为空,则会提示用户输入对应的pin值。这是因为我们可以将一个逻辑门连接到另一个逻辑门的输出接口,以实现电路的级联。

在conector类中,我们将tgate(目标逻辑门)的输入接口设置为当前conector对象。这样,当tgate的输入接口需要输入值时,它将从fgate(源逻辑门)获取输出值。这种连接方式实现了电路的级联。

在打印d的输出值时,会调用d的getoutput方法。在getoutput方法中,首先调用performgatelogic方法计算输出值。但在计算输出值之前,会调用getpinA和getpinB方法获取输入值。由于d是最后一个逻辑门,它没有其他逻辑门连接到它的输入接口,因此getpinA和getpinB方法会提示用户输入值。

所以,当你打印d的输出值时,会先提示输入pinA的值,因为在getpinA方法中使用了e的标签"rr"。这是由于在conector类的构造函数中,我们将tgate的输入接口设置为当前conector对象,即d的输入接口。因此,当d的输入接口需要输入值时,它将从e获取输出值,而e的标签是"rr"。

所以,打印d的输出值时会出现Enter pinA -->rr的提示。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-9 07:55:45 | 显示全部楼层
你的代码中出现了一个小错误。在 getpinA 和 getpinB 方法中,你使用了 self.label,而它是逻辑门的标签,而不是输入提示的标签。这就解释了为什么它显示了 "Enter pinA -->rr" 而不是 "Enter pinA -->rrrrr"。

要解决这个问题,你需要在提示用户输入时,使用适当的标签。在 getpinA 和 getpinB 方法中,将 self.label 替换为 "pinA" 或 "pinB"。

以下是你需要做的更改:
def getpinA(self):
    if self.pinA == None:
        return int(input("Enter pinA -->" + "pinA" ))
    else:
        return self.pinA.getFrom().getoutput()

def getpinB(self):
    if self.pinB == None:
        return int(input("Enter pinB -->" + "pinB" ))
    else:
        return self.pinB.getFrom().output()
这样,当你调用 d.getoutput() 时,它应该正常工作了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 13:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表