首页 > 编程语言 > 详细

python类模拟电路实现

时间:2019-05-27 17:06:24      阅读:280      评论:0      收藏:0      [点我收藏+]

实现电路:

技术分享图片

实现方法:

class LogicGate(object):
    def __init__(self, n):
        self.name = n
        self.output = None

    def get_label(self):
        return self.name

    def get_output(self):
        self.output = self.perform_gate_logic()
        return self.output


class BinaryGate(LogicGate):
    def __init__(self, n):
        super().__init__(n)
        self.pinA = None
        self.pinB = None

    def get_pinA(self):
        if self.pinA == None:
            return int(input("输入PinA的值 " + self.get_label() + "-->"))
        else:
            return self.pinA.get_from().get_output()

    def get_pinB(self):
        if self.pinB == None:
            return int(input("输入PinB的值 " + self.get_label() + "-->"))
        else:
            return self.pinB.get_from().get_output()

    def set_next_pin(self, source):
        if self.pinA == None:
            self.pinA = source
        else:
            if self.pinB == None:
                self.pinB = source
            else:
                print("无法连接:这个逻辑门没有空引脚")


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

    def perform_gate_logic(self):
        a = self.get_pinA()
        b = self.get_pinB()
        return a & b


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

    def perform_gate_logic(self):
        a = self.get_pinA()
        b = self.get_pinB()
        return a | b


class UnaryGate(LogicGate):
    def __init__(self, n):
        super().__init__(n)
        self.pin = None

    def get_pin(self):
        if self.pin == None:
            return int(input("输入Pin的值 " + self.get_label() + "-->"))
        else:
            return self.pin.get_from().get_output()

    def set_next_pin(self, source):
        if self.pin == None:
            self.pin = source
        else:
            print("无法连接:这个逻辑门没有空引脚")


class NotGate(UnaryGate):
    def __init__(self, n):
        super().__init__(n)

    def perform_gate_logic(self):
        a = self.get_pin()
        return 0 if a else 1


class Connector(object):
    def __init__(self, fgate, tgate):
        self.from_gate = fgate
        self.to_gate = tgate
        tgate.set_next_pin(self)

    def get_from(self):
        return self.from_gate

    def get_to(self):
        return self.to_gate


def main():
    g1 = AndGate("U1")
    g2 = AndGate("U2")
    g3 = OrGate("U3")
    g4 = NotGate("U4")
    c1 = Connector(g1, g2)
    c2 = Connector(g2, g3)
    c3 = Connector(g3, g4)
    print(g4.get_output())

main()

 

python类模拟电路实现

原文:https://www.cnblogs.com/hwnzy/p/10931487.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!