import comparator, bitwise_operator
def divide(dividend, divisor, quotient_remainder):
operand = []
divisor_length = len(divisor)
try:
for value in range(divisor_length):
operand.append(dividend.pop(0))
divisor_length -= 1
except IndexError:
for value in range(divisor_length):
operand.insert(0, 0)
quotient = []
remainder = []
while len(dividend) != 0:
if comparator.compare(list(operand),
list(divisor)) in [[1, 1], [1, 0]]:
quotient.append(1)
operand = bitwise_operator.operate(
list(operand),
list(divisor),
[0, 1, 1, 0, 1, 0, 0, 1],
[0, 1, 1, 1, 0, 0, 0, 1],
[0, 0])
operand.append(dividend.pop(0))
elif comparator.compare(list(operand), list(divisor)) == [0, 1]:
quotient.append(0)
operand.append(dividend.pop(0))
remainder = operand
while comparator.compare(list(remainder),
list(divisor)) in [[1, 1], [1, 0]]:
remainder = bitwise_operator.operate(list(remainder),
list(divisor),
[0, 1, 1, 0, 1, 0, 0, 1],
[0, 1, 1, 1, 0, 0, 0, 1],
[0, 0])
quotient = bitwise_operator.operate(list(quotient),
[1],
[0, 1, 1, 0, 1, 0, 0, 1],
[0, 0, 0, 1, 0, 1, 1, 1],
[0, 0])
if quotient == []:
quotient = [0]
if quotient_remainder == [1, 0]:
return(quotient)
elif quotient_remainder == [0, 1]:
return(remainder)
def quotient(dividend, divisor):
return(divide(dividend, divisor, [1, 0]))
def modulo(dividend, divisor):
return(divide(dividend, divisor, [0, 1]))
模块“按位运算符”在运算中按位进行,在这种情况下,它执行减法运算。
如果A小于,大于或等于B,模块“比较器”将返回01,10或11。
我一直试图制作一个二元分裂程序,每次尝试相同的方法。这个是我最成功的,因为它显示了正确的模数,但商是关闭的。据我所知,它正确遵循二元长除法。
有人知道它为什么不起作用???真的很令人沮丧。我看着它并修正了错误,但现在它应该如何工作。
不幸的是,我不能给你导入的模块。但有人可以分析我的代码,看看我做错了什么,即错误的二元分割方法?