Created
August 30, 2020 00:28
-
-
Save nytpu/048ae4f88a9ff0f137e1b440707d20dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LogicGate: | |
def __init__(self,n): | |
self.name = n | |
self.output = None | |
def getLabel(self): | |
return self.name | |
def getOutput(self): | |
self.output = self.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.getLabel()+"-->")) | |
else: | |
return self.pinA.getFrom().getOutput() | |
def getPinB(self): | |
if self.pinB == None: | |
return int(input("Enter Pin B input for gate "+self.getLabel()+"-->")) | |
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 NorGate(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 0 | |
else: return 1 | |
class NandGate(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 0 | |
else: return 1 | |
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.getLabel()+"-->")) | |
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 | |
def main(): | |
a1 = AndGate("G1") | |
a2 = AndGate("G2") | |
no1 = NorGate("G3") | |
c1 = Connector(a1, no1) | |
c2 = Connector(a2, no1) | |
na1 = NandGate("G4") | |
na2 = NandGate("G5") | |
a3 = AndGate("G6") | |
c3 = Connector(na1, a3) | |
c4 = Connector(na2, a3) | |
print(no1.getOutput()) | |
print(a3.getOutput()) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment