Advertisement
panupatchong

Untitled

Mar 14th, 2024
773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 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 "Pins not set. Cannot perform gate logic."
  21.        
  22.         self.xor_gate.pinA = self.pinA
  23.         self.xor_gate.pinB = self.pinB
  24.         self.and_gate.pinA = self.pinA
  25.         self.and_gate.pinB = self.pinB
  26.  
  27.         ## OR, if you implemented set_pins ##
  28.         self.xor_gate.set_pins(self.pinA, self.pinB)
  29.         self.and_gate.set_pins(self.pinA, self.pinB)
  30.         #####################################
  31.  
  32.         gate_sum = self.xor_gate.getOutput()
  33.         gate_carry = self.and_gate.getOutput()
  34.  
  35.         return gate_sum, gate_carry
  36.  
  37.  
  38. class BinaryGate(LogicGate):
  39.     pass
  40.  
  41. class AndGate(BinaryGate):
  42.     pass
  43.  
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement