Python 初学笔记:逻辑门练习

正在学习Problem Solving with Algorithms and Data Structures using Python(点击打开链接),有一个关于逻辑门实现的小例子,个人感觉很有意思。


class LogicGate:
    def __init__(self, n):
        self.name = n
        self.output = None
    def getName(self):
        return self.name
    def getOutput(self):
        self.output = self.performGateLogic() #可将performGateLogic看作接口
        return self.output

   
class BinaryGate(LogicGate):
    def __init__(self, n):
        LogicGate.__init__(self, n)
        self.pinA = None
        self.pinB = None
    def getPinA(self):
        if self.pinA == None:
            return int(input("Enter Pin A input for gate " + self.getName() + "-->"))
        else:
            return self.pinA.getFrom().getOutput()
    def getPinB(self):
        if self.pinB == None:
            return int(input("Enter Pin B input for gate " + self.getName() + "-->"))
        else:
            return self.pinB.getFrom().getOutput()
    def setNextPin(self, source):
        if self.pinA == None:
            self.pinA = source
        else:
            if self.pinB == None:
                self.pinB = source
            else:
                print("Cannot Connect: No Empty Pins on this gate")
               
class AndGate(BinaryGate):
    def __init__(self, n):
        BinaryGate.__init__(self,n)
    def performGateLogic(self):
        a = self.getPinA()
        b = self.getPinB()
        if a == 1 and b == 1:
            return 1
        else:
            return 0

class OrGate(BinaryGate):
    def __init__(self, n):
        BinaryGate.__init__(self, n)
    def performGateLogic(self):
        a = self.getPinA()
        b = self.getPinB()
        if a == 1 or b == 1:
            return 1
        else:
            return 0
        
class UnaryGate(LogicGate):
    def __init__(self, n):
        LogicGate.__init__(self, n)
        self.pin = None
    def getPin(self):
        if self.pin == None:
            return int(input("Enter Pin input for gate " + self.getName() + "-->"))
        else:
            return self.pin.getFrom().getOutput()
    def setNextPin(self, source):
        if self.pin == None:
            self.pin = source
        else:
            print("Cannot Connect: No Empty Pins on this gate")

class NotGate(UnaryGate):
    def __init__(self, n):
        UnaryGate.__init__(self, n)
    def performGateLogic(self):
        if self.getPin():
            return 0
        else:
            return 1

class Connector:
    def __init__(self, fgate, tgate):
        self.fromgate = fgate
        self.togate = tgate
        tgate.setNextPin(self)
    def getFrom(self):
        return self.fromgate
    def getTo(self):
        return self.togate

class NorGate(BinaryGate):
    def __init__(self, n):
        BinaryGate.__init__(self, n)
    def performGateLogic(self):
        if self.getPinA():
            return 0
        elif self.getPinB():
            return 0
        else:
            return 1

class NandGate(AndGate):
    def __init__(self, n):
        AndGate.__init__(self, n)
    def performGateLogic(self):
        if not self.getPinA():
            return 1
        elif not self.getPinB():
            return 1
        else:
            return 0
        #return not AndGate.performGateLogic(self)

def main():
    
    g1 = AndGate("G1")
    g2 = AndGate("G2")
    g3 = OrGate("G3")
    g4 = NorGate("G4")
    g5 = NandGate("G5")
    g6 = NotGate("G6")
    c4 = Connector(g5, g1)
    c5 = Connector(g4, g1)
    c1 = Connector(g1, g3)
    c2 = Connector(g2, g3)
    c3 = Connector(g3, g6)
    print(g6.getOutput())


main()


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值