Advertisement
panupatchong

Untitled

Mar 14th, 2024
761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. class LogicGate:
  2.     def __init__(self, name):
  3.         self.name = name
  4.         self.pinA = None
  5.         self.pinB = None
  6.  
  7.     def set_pins(self, pinA, pinB):
  8.         self.pinA = pinA
  9.         self.pinB = pinB
  10.  
  11.  
  12. class HalfAdder(LogicGate):
  13.     def __init__(self, name):
  14.         super().__init__(name)
  15.         self.xor_gate = XorGate("xor1")
  16.         self.and_gate = AndGate("and1")
  17.  
  18.     def getOutput(self):
  19.         if self.pinA is None or self.pinB is None:
  20.             return None
  21.         self.xor_gate.pinA = self.pinA
  22.         self.xor_gate.pinB = self.pinB
  23.         self.and_gate.pinA = self.pinA
  24.         self.and_gate.pinB = self.pinB
  25.  
  26.         ## OR, if you implemented set_pins
  27.         self.xor_gate.set_pins(self.pinA, self.pinB)
  28.         self.and_gate.set_pins(self.pinA, self.pinB)
  29.         ##
  30.  
  31.         sum = self.xor_gate.getOutput()
  32.         carry = self.and_gate.getOutput()
  33.  
  34.         return sum, carry
  35.  
  36.  
  37. class BinaryGate(LogicGate):
  38.     pass
  39.  
  40. class AndGate(BinaryGate):
  41.     pass
  42.  
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement