|
20鱼币
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): # 继承父类的方法
# 逻辑门要有标签,logicgate的子类Binarygate也不列外,所以这里要调用其父类的创造标签的方法,即super().__init__(labe),super()为其父类,否则就不能创造标签。
def __init__(self, label):
# 调用super().__init__(labe)后,就默认有self.label = label,self.output = None,父类的方法(函数)也有继承
super().__init__(label)
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:
# 返回变为connecor的接口的pinA ,取得其上一个逻辑门,再让上一个逻辑门执行getoutput
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()
def getinput(self):
return self.getpinA()
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) # 把tgate的输入接口变为conector本身
def getFrom(self):
return self.fgate
def getTo(self):
return self.tgate
e = AndGate("rr")
d = AndGate("rrrrr")
k= AndGate("uu")
a = conector(e, d) # 这里有参与运算,当a = conector(e, d)是,。。。。。
b = conector(k,e )
print(d.getoutput())# 从最后一个逻辑门开始运行,逆向思维
如代码所示,这是段逆向的电路,方向是k-->e--->d 可以直接print(d.getoutput())# 从最后一个逻辑门开始运行,逆向思维
想要将其变为正向电路,即输入k.getoutput(),可得出最后答案
|
|