针对类间的函数调用,跟踪函数间跳转的整个过程,实现
两个与门 给 一个或门,,而后给一个非门。
import inspect
class LogicGate:
def __init__(self, n):
global a
a += 1
print(a, self.__class__.__name__ + '.' + inspect.stack()[0][3] + '()')
self.label = n
self.output = None
def getLabel(self):
global a
a += 1
print(a, self.__class__.__name__ + '.' + inspect.stack()[0][3] + '()')
return self.label
def getOutput(self):
global a
a += 1
print(a, self.__class__.__name__ + '.' + inspect.stack()[0][3] + '()')
self.output = self.performGateLogic()
return self.output
class BinaryGate(LogicGate):
def __init__(self, n):
super().__init__(n)
self.pinA = None
self.pinB = None
global a
a += 1
print(a, self.__class__.__name__ + '.' + inspect.stack()[0][3] + '()')
def getPinB(self):
global a
a += 1
print(a, self.__class__.__name__ + '.' + inspect.stack()[0][3] + '()')
if self.pinB == None:
return int(input("Enter Pin B input for gate " + self.getLabel() + "-->"))
else:
nb = self.pinB.getFrom()
return nb.getOutput()
def getPinA(self):
global a
a += 1
print(a, self.__class__.__name__ + '.' + inspect.stack()[0][3] + '()')
if self.pinA == None:
return int(input("Enter Pin A input for gate " + self.getLabel() + "-->"))
# return 0
else:
nb = self.pinA.getFrom()
return nb.getOutput()
def setNextPin(self, source):
global a
a += 1
print(a, self.__class__.__name__ + '.' + inspect.stack()[0][3] + '()')
if self.pinA == None:
self.pinA = source
else:
if self.pinB == None:
self.pinB = source
else:
raise RuntimeError("Error: NO EMPTY PINS")
class UnaryGate(LogicGate):
def __init__(self, n):
super().__init__(n)
self.pin = None
global a
a += 1
print(a, self.__class__.__name__ + '.' + inspect.stack()[0][3] + '()')
def getPin(self):
global a
a += 1
print(a, self.__class__.__name__ + '.' + inspect.stack()[0][3] + '()')
if self.pin == None:
return int(input("Enter Pin input for gate " + self.getLabel() + "-->"))
else:
w = self.pin.getFrom()
return w.getOutput()
def setNextPin(self, source):
global a
a += 1
print(a, self.__class__.__name__ + '.' + inspect.stack()[0][3] + '()')
if self.pin == None:
self.pin = source
else:
raise RuntimeError("Error: NO nono")
class AndGate(BinaryGate):
def __init__(self, n):
super().__init__(n)
global a
a += 1
print(a, self.__class__.__name__ + '.' + inspect.stack()[0][3] + '()')
def performGateLogic(self):
global a
a += 1
print(a, self.__class__.__name__ + '.' + inspect.stack()[0][3] + '()')
t = self.getPinA()
b = self.getPinB()
if t == 1 and b == 1:
return 1
else:
return 0
class OrGate(BinaryGate):
def __init__(self, n):
super().__init__(n)
global a
a += 1
print(a, self.__class__.__name__ + '.' + inspect.stack()[0][3] + '()')
def performGateLogic(self):
global a
a += 1
print(a, self.__class__.__name__ + '.' + inspect.stack()[0][3] + '()')
t = self.getPinA()
b = self.getPinB()
if t == 0 and b == 0:
return 0
else:
return 1
class NotGate(UnaryGate):
def __init__(self, n):
super().__init__(n)
global a
a += 1
print(a, self.__class__.__name__ + '.' + inspect.stack()[0][3] + '()')
def performGateLogic(self):
t = self.getPin()
global a
a += 1
print(a, self.__class__.__name__ + '.' + inspect.stack()[0][3] + '()')
return t ^ 1
class Connector:
def __init__(self, fgate, tgate):
self.fromgate = fgate
self.togate = tgate
global a
a += 1
print(a, self.__class__.__name__ + '.' + inspect.stack()[0][3] + '()')
tgate.setNextPin(self)
def getFrom(self):
global a
a += 1
print(a, self.__class__.__name__ + '.' + inspect.stack()[0][3] + '()')
return self.fromgate
def getTo(self):
global a
a += 1
print(a, self.__class__.__name__ + "." + inspect.stack()[0][3] + '()')
return self.togate
a = 0
g1 = AndGate("G1")
g2 = AndGate("G2")
g3 = OrGate("G3")
g4 = NotGate("G4")
c1 = Connector(g1, g3)
c2 = Connector(g2, g3)
c3 = Connector(g3, g4)
x = g4.getOutput()
print(a, x)
网友评论