我正在尝试创建模拟布尔电路的代码,并且应该通过布尔电路基于布尔逻辑返回true或false(一或零)。我将在下面发布电路图。然而,我得到的输出是:
Ran 9 tests in 0.141s
OK
#!/usr/bin/python
import unittest
# An example: exercise 12.3.1
def circuit_3_1(x, y):
a1 = x + y
a2 = not y
# note: the end result should be returned as 0/1
# - bool() transforms any sum larger than 0 into True (False otherwise)
# - int() transforms any True into 1 (0 otherwise)
# int( bool ( n )) will therefore transforms any sum larger than 0 into 1 (0 otherwise)
return int( bool(a1 and a2))
def circuit_3_2(x, y):
a1 = not x
a2 = not y
a3 = (not a1) and (not a2)
return int( bool((a3)))
4
def circuit_3_4(x, y, z):
a1 = (not((not x) and y and z))
a2=((not x) or y or(not z))
return int( bool(a1 and a2))
class circuits_unit_tests( unittest.TestCase ):
def test_circuit_3_1(self):
self.assertEqual( circuit_3_1(0,0), 0)
self.assertEqual( circuit_3_1(0,1), 0)
self.assertEqual( circuit_3_1(1,0), 1)
self.assertEqual( circuit_3_1(1,1), 0)
def test_circuit_3_4_000(self):
self.assertEqual( circuit_3_4(0,0,0), 1)
def test_circuit_3_4_001(self):
self.assertEqual( circuit_3_4(0,0,1), 1)
def test_circuit_3_4_010(self):
self.assertEqual( circuit_3_4(0,1,0), 1)
def test_circuit_3_4_011(self):
self.assertEqual( circuit_3_4(0,1,1), 0)
def test_circuit_3_4_100(self):
self.assertEqual( circuit_3_4(1,0,0), 1)
def test_circuit_3_4_101(self):
self.assertEqual( circuit_3_4(1,0,1), 0)
def test_circuit_3_4_110(self):
self.assertEqual( circuit_3_4(1,1,0), 1)
def test_circuit_3_4_111(self):
self.assertEqual( circuit_3_4(1,1,1), 1)
def main():
unittest.main()
if __name__ == '__main__':
main()