gpt4 book ai didi

python - 半加器和全加器逻辑?

转载 作者:行者123 更新时间:2023-11-28 22:40:03 25 4
gpt4 key购买 nike

<分区>

我无法通过半加器和全加器测试,我想知道我在 FullAdder() 中的逻辑有什么问题和 HalfAdder()方法?由于某种原因,我的测试似乎都没有通过......

代码如下:

    # All wires that enter/exit the circuit need to be visible outside of the 
# function: they are to be created in the enclosing scope and passed
# as parameters.
# However, wires that connect the AND gates to the OR gates are
# _internal wires_, visible only inside the circuit: they need to be created
xy = self.Wire('xy')
xz = self.Wire('xz')
yz = self.Wire('yz')
xy_or_xz = self.Wire('xy_or_xz')

self.AndGate(x, y, xy)
self.AndGate(x, z, xz)
self.Andgate(x, z, yz)

# OR(xy,xz,yz) = OR( OR(xy,xz), yz)
self.OrGate(xy, xz, xy_or_xz)
self.OrGate(xy_or_xz, yz, o)

return 'ok'

def TwoSwitches(self, x, y, o):
""" An example: light controlled by 2 switches (Rosen, 12.3, Example 3,
- Figure 6, p. 825)

F(x, y) = xy + !x.!y
"""
# Wires x, y, and o are passed as parameters.
# Only the internal wires need to be created:
xy = self.Wire('xy')
not_x = self.Wire('not_x')
not_y = self.Wire('not_y')
notx_noty = self.Wire('notx_noty')

self.AndGate(x,y,xy)
self.Inverter(x, not_x)
self.Inverter(y, not_y)
self.AndGate(not_x, not_y, notx_noty)
self.OrGate(xy, notx_noty, o)

return 'ok'

# This function needs to be defined: parameter, body definition
def HalfAdder(self, x, y, s, c):
notx_and_y=self.wire('notx_and_y')
x_and_noty=self.wire('x_and_noty')
cwire=self.wire('cwire')
allwire=self.wire(allwire)
self.OrGate(self.AndGate(not x,y,notx_and_y),self.AndGate(x,not y,x_and_noty),allwire)
cwire=self.AndGate(x,y,cwire)
c=cwire
s=allwire
pass

def FullAdder(self,x,y,c_i,s, c_out):
#TODO
pass

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com