<<python数据结构与算法分析>>第一章:用类实现逻辑门电路

# 逻辑门电路的类   练习类的继承
'''logicgate 逻辑门
    binarygate 两个输入
        andgate与门  orgate或门
        与非门  或非门
        异或门  同或门
    unarygate 一个输入
        notgate 非门'''
class LogicGate:

    def __init__(self,n):
        self.label = n
        self.output = None

    def getLael(self):
        return self.label

    def getOutput(self):
        self.output = self.performGateLogic()  #该方法并未定义,在创建子类的时候定义
        return self.output



class BinaryGate(LogicGate):

    def __init__(self,n):
        super().__init__(n)

        self.pinA = None
        self.pinB = None

    def getpinA(self):
        if self.pinA : # 配合连接器时新增
            return self.pinA.getFrom().getOutput()
        return int(input("Enter pin A for gate" + self.getLael()+"-->"))

    def getpinB(self):
        if self.pinB : # 配合连接器时新增
            return self.pinB.getFrom().getOutput()
        return int(input("Enter pin B for gate" + self.getLael() + "-->"))

    def setNextPin(self,source):  # 该方法用于连结器连结两个逻辑门的时候
        if self.pinA == None:
            self.pinA = source
        else:
            if self.pinB == None:
                self.pinB = source
            else:
                raise  RuntimeError('ERROR:No empry pin')

class AndGate(BinaryGate):
    def __init__(self,n):
        super().__init__(n)

    def performGateLogic(self):
        '''重写基类的方法,实现逻辑门的输出'''
        a = self.getpinA()
        b = self.getpinB()
        if a == 1 and b == 1:
            return 1
        return 0

class NotAndGate(BinaryGate):
    '''与非门'''
    def __init__(self,n):
        super().__init__(n)

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


class OrGate(BinaryGate):
    def __init__(self,n):
        super().__init__(n)

    def performGateLogic(self):
        a = self.getpinA()
        b = self.getpinB()
        if a == 1 or b == 1:
            return 1
        return 0

class NotOrGate(BinaryGate):
    '''或非门'''
    def __init__(self,n):
        super().__init__(n)

    def performGateLogic(self):
        a = self.getpinA()
        b = self.getpinB()
        if a == 1 or b == 1:
            return 0
        return 1

class XORgate(BinaryGate):
    '''异或门'''
    def __init__(self,n):
        super().__init__(n)

    def performGateLogic(self):
        a = self.getpinA()
        b = self.getpinB()
        if a != b:
            return 1
        return 0

class XNORgate(BinaryGate):
    '''同或门'''
    def __init__(self,n):
        super().__init__(n)

    def performGateLogic(self):
        a = self.getpinA()
        b = self.getpinB()
        if a == b:
            return 1
        return 0


class UnaryGate(LogicGate):

    def __init__(self, n):
        super().__init__(n)

        self.pin = None

    def getPin(self):
        if self.pin:
            return self.pin.getFrom().getOutput()
        return int(input("Enter pin  for gate" + self.getLael() + "-->"))

    def setNextPin(self, source):  # 该方法用于连结器连结两个逻辑门的时候
        if self.pin == None:
            self.pin = source
        else:
            raise RuntimeError('ERROR:No empry pin')


class NotGate(UnaryGate):

    def __init__(self, n):
        super().__init__(n)

    def performGateLogic(self):
        x = self.getPin()
        if x == 1:
            return 0
        return 1







class Connector:
    '''连接器类,用于连接两个逻辑门'''
    def __init__(self, fgate, tgate):
        self.fromGate = fgate
        self.toGate = tgate
        tgate.setNextPin(self)  # 此处有疑问 此处的self为Connector类的实例

    def getFrom(self):
        return self.fromGate

    def getTo(self):
        return self.toGate
















if __name__ == '__main__':
    # g1 = AndGate('g1')
    # print(g1.getOutput())
    # n1 = NotGate('n1')
    # print(n1.getOutput())

    # o1 = OrGate('o1')
    # print(o1.getOutput())

    # g1 = AndGate('g1')
    # g2 = AndGate('g2')
    # g3 = OrGate('g3')
    # g4 = NotGate('g4')
    # c1 = Connector(g1,g3)
    # c2 = Connector(g2,g3)
    # c3 = Connector(g3,g4)
    # print(g4.getOutput())
    g5 = XORgate('g5')
    print(g5.getOutput())





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值