Math 为计算机AIDJ编写除法程序
我正在为一台叫做AIDJ的虚拟计算机编写一个乘法程序 它支持任意数量的变量,并且能够读取和执行表单的任何操作序列(程序)Math 为计算机AIDJ编写除法程序,math,Math,我正在为一台叫做AIDJ的虚拟计算机编写一个乘法程序 它支持任意数量的变量,并且能够读取和执行表单的任何操作序列(程序) mk: <operation> 以c>=2的程序相乘为例: 00: x(1) = c 01: x(2) = d 02: x(2) = x(2) + 1 03: x(1) = x(1) - 1 04: if x(1) != 0 goto 02 00: x(1) = d 01: x(1) = d - 1 02: x(2) = c 03: x(3) = c 04:
mk: <operation>
以c>=2的程序相乘为例:
00: x(1) = c
01: x(2) = d
02: x(2) = x(2) + 1
03: x(1) = x(1) - 1
04: if x(1) != 0 goto 02
00: x(1) = d
01: x(1) = d - 1
02: x(2) = c
03: x(3) = c
04: x(2) = x(2) + 1
05: x(3) = x(3) - 1
06: if x(3) =! 0 goto 04
07: x(1) = d - 1
08: if x(3) =! 0 goto 03
| Command | x(1) | x(2) | x(3) |
--------------------------------------------------
| 00: x(1) = d | 3 | - | - |
| 01: x(1) = d - 1 | 2 | - | - |
| 02: x(2) = c | 2 | 3 | - |
| 03: x(3) = c | 2 | 3 | 3 |
| 04: x(2) = c + 1 | 2 | 4 | 3 |
| 05: x(3) = d - 1 | 2 | 4 | 2 |
| 06: if x(3) =! 0 goto 04 | 2 | 4 | 2 |
| 04: x(2) = c + 1 | 2 | 5 | 2 |
| 05: x(3) = d - 1 | 2 | 5 | 1 |
| 06: if x(3) =! 0 goto 04 | 2 | 5 | 1 |
| 04: x(2) = c + 1 | 2 | 6 | 1 |
| 05: x(3) = d - 1 | 2 | 6 | 0 |
| 06: if x(3) =! 0 goto 04 | 2 | 6 | 0 |
| 07: x(1) = d - 1 | 1 | 6 | 0 |
| 08: if x(3) =! 0 goto 03 | 1 | 6 | 0 |
| 03: x(3) = c | 1 | 6 | 3 |
| 04: x(2) = c + 1 | 1 | 7 | 3 |
| 05: x(3) = d - 1 | 1 | 7 | 2 |
| 06: if x(3) =! 0 goto 04 | 1 | 7 | 2 |
| 04: x(2) = c + 1 | 1 | 8 | 2 |
| 05: x(3) = d - 1 | 1 | 9 | 1 |
| 06: if x(3) =! 0 goto 04 | 1 | 9 | 1 |
| 04: x(2) = c + 1 | 1 | 9 | 1 |
| 05: x(3) = d - 1 | 1 | 9 | 0 |
| 06: if x(3) =! 0 goto 04 | 1 | 9 | 0 |
| 07: x(1) = d - 1 | 0 | 9 | 0 |
| 08: if x(3) =! 0 goto 03 | 0 | 9 | 0 |
我确保c×d等于c+c+…+c(d总和),这样我就可以使用程序ADD,并且我们确保ADD执行d-1次。当计算停止时,x(2)等于c×d
如果c=3和d=3,则会产生以下值表:
00: x(1) = c
01: x(2) = d
02: x(2) = x(2) + 1
03: x(1) = x(1) - 1
04: if x(1) != 0 goto 02
00: x(1) = d
01: x(1) = d - 1
02: x(2) = c
03: x(3) = c
04: x(2) = x(2) + 1
05: x(3) = x(3) - 1
06: if x(3) =! 0 goto 04
07: x(1) = d - 1
08: if x(3) =! 0 goto 03
| Command | x(1) | x(2) | x(3) |
--------------------------------------------------
| 00: x(1) = d | 3 | - | - |
| 01: x(1) = d - 1 | 2 | - | - |
| 02: x(2) = c | 2 | 3 | - |
| 03: x(3) = c | 2 | 3 | 3 |
| 04: x(2) = c + 1 | 2 | 4 | 3 |
| 05: x(3) = d - 1 | 2 | 4 | 2 |
| 06: if x(3) =! 0 goto 04 | 2 | 4 | 2 |
| 04: x(2) = c + 1 | 2 | 5 | 2 |
| 05: x(3) = d - 1 | 2 | 5 | 1 |
| 06: if x(3) =! 0 goto 04 | 2 | 5 | 1 |
| 04: x(2) = c + 1 | 2 | 6 | 1 |
| 05: x(3) = d - 1 | 2 | 6 | 0 |
| 06: if x(3) =! 0 goto 04 | 2 | 6 | 0 |
| 07: x(1) = d - 1 | 1 | 6 | 0 |
| 08: if x(3) =! 0 goto 03 | 1 | 6 | 0 |
| 03: x(3) = c | 1 | 6 | 3 |
| 04: x(2) = c + 1 | 1 | 7 | 3 |
| 05: x(3) = d - 1 | 1 | 7 | 2 |
| 06: if x(3) =! 0 goto 04 | 1 | 7 | 2 |
| 04: x(2) = c + 1 | 1 | 8 | 2 |
| 05: x(3) = d - 1 | 1 | 9 | 1 |
| 06: if x(3) =! 0 goto 04 | 1 | 9 | 1 |
| 04: x(2) = c + 1 | 1 | 9 | 1 |
| 05: x(3) = d - 1 | 1 | 9 | 0 |
| 06: if x(3) =! 0 goto 04 | 1 | 9 | 0 |
| 07: x(1) = d - 1 | 0 | 9 | 0 |
| 08: if x(3) =! 0 goto 03 | 0 | 9 | 0 |
我如何修改上面的程序,使其以除法而不是乘法?乘法运算是在循环中添加的,对吗?除法就是在循环中进行减法,直到剩余的除数小于除数。在伪代码中:
# We're dividing a/b, getting the quotient c
c = 0
while a >= b
{
a = a - b
c = c+1
}
# End of loop
# The value of a is the remainder at this point
那就这样吧。首先,在VM上实现减法(在循环中递减)。然后做剩下的
由于条件限制为与零比较,因此通过减法(循环中的减量)实现a>=b的逻辑。不要害怕引入额外的变量。架构可能会受到限制,但对使用的内存没有明确的限制,是吗
甚至可以将循环条件(即循环中的减量)与循环体(也可以是循环中的减量)组合在一起。在高级语言中,这不是他们的工作方式,甚至连真正的CPU都不是这样做的——所有现代CPU都将整数比较作为一个基本元素。类似于“递减,如果在除数剩余值达到零之前达到零,那么退出循环,我们就完成了”。然而,在现实生活中,强烈建议不要使用这种微优化(对于不存在的体系结构,也不要少)
此外,如果这是家庭作业,请添加标签“家庭作业”。闻起来很像家庭作业
编辑:逻辑是这样的。我们引入三个变量:
- x(1)是要递减的股息的运行副本
- x(2)最初是商零,要递增
- x(3)是除数的运行循环变量
- d是股息
- c是除数
- 我们可以给变量分配常数(比如零),对吗
x(1) = d
x(2) = 0
x(3) = c
Main_Loop:
x(1) = x(1) - 1 # decrement the running copy of the dividend
x(3) = x(3) - 1 # decrement the running copy of the divisor
if x(3) != 0 goto Divisor_still_nonzero
# If we're here, this means we've reached the end of the divisor.
# Increment the quotient, reset the running divisor to the initial value
x(2) = x(2) + 1
x(3) = c
Divisor_still_nonzero:
# Now let's check if we've reached the zero on the dividend
if x(1) != 0 then goto Main_Loop
# If x(1) is zero then we end up here.
# Means we've exhausted the dividend.
# Now the value of x(2) is the quotient.
# The remainder is c minus x(3),
# but retrieving it was not a requirement.
在现实生活中,你也应该在一开始就检查c和d是零还是负。如果d为零,代码将中断-在第一次迭代时它将变成负数,并且只有在变量发生溢出时才会终止。如果c为零,则整个除法是非法的
还需要额外处理负片
另外,请记住,一些教授已知会观察堆栈溢出。多类型应用是在循环中添加的,对吗?除法就是在循环中进行减法,直到剩余的除数小于除数。在伪代码中:
# We're dividing a/b, getting the quotient c
c = 0
while a >= b
{
a = a - b
c = c+1
}
# End of loop
# The value of a is the remainder at this point
那就这样吧。首先,在VM上实现减法(在循环中递减)。然后做剩下的
由于条件限制为与零比较,因此通过减法(循环中的减量)实现a>=b的逻辑。不要害怕引入额外的变量。架构可能会受到限制,但对使用的内存没有明确的限制,是吗
甚至可以将循环条件(即循环中的减量)与循环体(也可以是循环中的减量)组合在一起。在高级语言中,这不是他们的工作方式,甚至连真正的CPU都不是这样做的——所有现代CPU都将整数比较作为一个基本元素。类似于“递减,如果在除数剩余值达到零之前达到零,那么退出循环,我们就完成了”。然而,在现实生活中,强烈建议不要使用这种微优化(对于不存在的体系结构,也不要少)
此外,如果这是家庭作业,请添加标签“家庭作业”。闻起来很像家庭作业
编辑:逻辑是这样的。我们引入三个变量:
- x(1)是要递减的股息的运行副本
- x(2)最初是商零,要递增
- x(3)是除数的运行循环变量
- d是股息
- c是除数
- 我们可以给变量分配常数(比如零),对吗
x(1) = d
x(2) = 0
x(3) = c
Main_Loop:
x(1) = x(1) - 1 # decrement the running copy of the dividend
x(3) = x(3) - 1 # decrement the running copy of the divisor
if x(3) != 0 goto Divisor_still_nonzero
# If we're here, this means we've reached the end of the divisor.
# Increment the quotient, reset the running divisor to the initial value
x(2) = x(2) + 1
x(3) = c
Divisor_still_nonzero:
# Now let's check if we've reached the zero on the dividend
if x(1) != 0 then goto Main_Loop
# If x(1) is zero then we end up here.
# Means we've exhausted the dividend.
# Now the value of x(2) is the quotient.
# The remainder is c minus x(3),
# but retrieving it was not a requirement.
在现实生活中,你也应该在一开始就检查c和d是零还是负。如果d为零,代码将中断-在第一次迭代时它将变成负数,并且只有在变量发生溢出时才会终止。如果c为零,则整个除法是非法的
还需要额外处理负片
另外,请记住,一些教授已知会观察堆栈溢出。我可以通过简单地反转乘法程序中的所有加减符号来实现同样的效果吗?不,当然不能。乘法主循环具有完全不同的逻辑。我仍然不能100%确定如何通过减法(循环中的减量)实现a>=b的逻辑。你能给我举个例子吗。顺便说一句,这不是家庭作业——我正在为考试做一些额外的复习,我有一种感觉,尽管我们在讲座中只讨论了乘法程序,但这个问题可能会出现。我能通过简单地反转乘法程序中的所有加减符号来实现同样的效果吗?不,当然不能。乘法主循环具有完全不同的逻辑。我仍然不能100%确定如何通过减法(循环中的减量)实现a>=b的逻辑。你能