鱼C论坛

 找回密码
 立即注册
查看: 2354|回复: 6

关于用python模仿逻辑电路的一个小例子

[复制链接]
发表于 2018-3-2 21:39:30 | 显示全部楼层 |阅读模式

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

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

x
  1. class LogicGate:

  2.     def __init__(self,n):
  3.         self.name = n
  4.         self.output = None

  5.     def getName(self):
  6.         return self.name

  7.     def getOutput(self):
  8.         self.output = self.performGateLogic()
  9.         return self.output


  10. class BinaryGate(LogicGate):

  11.     def __init__(self,n):
  12.         LogicGate.__init__(self,n)

  13.         self.pinA = None
  14.         self.pinB = None

  15.     def getPinA(self):
  16.         if self.pinA == None:
  17.             return int(input("Enter Pin A input for gate "+self.getName()+"-->"))
  18.         else:
  19.             return self.pinA.getFrom().getOutput()

  20.     def getPinB(self):
  21.         if self.pinB == None:
  22.             return int(input("Enter Pin B input for gate "+self.getName()+"-->"))
  23.         else:
  24.             return self.pinB.getFrom().getOutput()

  25.     [color=DarkSlateBlue]def setNextPin(self,source):[/color]
  26.         if self.pinA == None:
  27.             self.pinA = source
  28.         else:
  29.             if self.pinB == None:
  30.                 self.pinB = source
  31.             else:
  32.                 print("Cannot Connect: NO EMPTY PINS on this gate")


  33. class AndGate(BinaryGate):

  34.     def __init__(self,n):
  35.         BinaryGate.__init__(self,n)

  36.     def performGateLogic(self):

  37.         a = self.getPinA()
  38.         b = self.getPinB()
  39.         if a==1 and b==1:
  40.             return 1
  41.         else:
  42.             return 0

  43. class OrGate(BinaryGate):

  44.     def __init__(self,n):
  45.         BinaryGate.__init__(self,n)

  46.     def performGateLogic(self):

  47.         a = self.getPinA()
  48.         b = self.getPinB()
  49.         if a ==1 or b==1:
  50.             return 1
  51.         else:
  52.             return 0

  53. class UnaryGate(LogicGate):

  54.     def __init__(self,n):
  55.         LogicGate.__init__(self,n)

  56.         self.pin = None

  57.     def getPin(self):
  58.         if self.pin == None:
  59.             return int(input("Enter Pin input for gate "+self.getName()+"-->"))
  60.         else:
  61.             return self.pin.getFrom().getOutput()

  62.     def setNextPin(self,source):
  63.         if self.pin == None:
  64.             self.pin = source
  65.         else:
  66.             print("Cannot Connect: NO EMPTY PINS on this gate")


  67. class NotGate(UnaryGate):

  68.     def __init__(self,n):
  69.         UnaryGate.__init__(self,n)

  70.     def performGateLogic(self):
  71.         if self.getPin():
  72.             return 0
  73.         else:
  74.             return 1


  75. class Connector:

  76.     def __init__(self, fgate, tgate):
  77.         self.fromgate = fgate
  78.         self.togate = tgate

  79.         [color=Olive]tgate.setNextPin(self)[/color]

  80.     def getFrom(self):
  81.         return self.fromgate

  82.     def getTo(self):
  83.         return self.togate


  84. def main():
  85.    g1 = AndGate("G1")
  86.    g2 = AndGate("G2")
  87.    g3 = OrGate("G3")
  88.    g4 = NotGate("G4")
  89.    c1 = Connector(g1,g3)
  90.    c2 = Connector(g2,g3)
  91.    c3 = Connector(g3,g4)
  92.    print(g4.getOutput())

  93. main()
复制代码


这是个魔方逻辑门的程序,我是在搞不懂 g3传入tgate.setNextPin()之后到 BinaryGate(LogicGate)再去执行def setNextPin(self,source)中source 到底是什么意思?该怎么去理解。求大神指点
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-3-2 22:33:14 | 显示全部楼层
tgate.setNextPin(self)是把gate传入 setNextPin函数中么?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-3 10:33:15 | 显示全部楼层
g3传入后,作为tgate,在Connector类使用tgate.setNextPin(self),
由于g3是或门,OrGate类,父类二进制门类BinaryGate才定义了setNextPin(self,source)
本身的这个函数是父类继承过来的而已
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-3 13:03:01 | 显示全部楼层
塔利班 发表于 2018-3-3 10:33
g3传入后,作为tgate,在Connector类使用tgate.setNextPin(self),
由于g3是或门,OrGate类,父类二进制门 ...


谢谢回答 但是我还是有些不理解,比如
g1 = AndGate('G1')
g3 = AndGate('G3')
c1 = Connector(g1,g3)
g1,g3 传入进 Connector,
然后g3.setNextPin 会把 self.fromgate 和 self.togate 都传入进 setNextPin ,
输出 self.fromgae ,self.togate

souce就是 g1,g3
是这样子理解吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-3 15:52:21 | 显示全部楼层
snakeshe 发表于 2018-3-3 13:03
谢谢回答 但是我还是有些不理解,比如
g1 = AndGate('G1')
g3 = AndGate('G3')

说实话,我都很不想看了= =,关键引用的继承太乱,初始化也来回引用,我教你个方法
去改写程序,将输出结果变成加法计算,来看看是不是这样
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-3 18:10:09 | 显示全部楼层
塔利班 发表于 2018-3-3 15:52
说实话,我都很不想看了= =,关键引用的继承太乱,初始化也来回引用,我教你个方法
去改写程序,将输出 ...

好的  我试试看 谢谢你
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-3 21:50:29 | 显示全部楼层
snakeshe 发表于 2018-3-3 13:03
谢谢回答 但是我还是有些不理解,比如
g1 = AndGate('G1')
g3 = AndGate('G3')

这里的 source ,就是指向这 connector(g1,g3)这个类,之后把这个类赋给 self.pin,所以才可以调用 getFrom()的方法
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-21 15:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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