仅具有特定方法的计算器。正常和递归
Calculator with specific methods only. Normal and recursive
所以 atm 我坚持使用我的计算器。只允许使用以下方法:
int succ(int x){
return ++x;
}
int neg(int x){
return -x;
}
我已经得到的是+、-。 *。 Iterativ 也是递归的(所以如果需要我也可以使用它们)。
现在我坚持使用 divide 方法,因为我不知道如何处理逗号及其背后的逻辑。想象一下处理 succ() 和 neg() 的样子,这是一个减法迭代和递归的例子:
int sub(int x, int y){
if (y > 0){
y = neg(y);
x = add(x, y);
return x;
}
else if (y < 0){
y = neg(y);
x = add(x, y);
return x;
}
else if (y == 0) {
return x;
}
}
int sub_recc(int x, int y){
if (y < 0){
y = neg(y);
x = add_recc(x, y);
return x;
} else if (y > 0){
x = sub_recc(x, y - 1);
x = x - 1;
return x;
}else if( y == 0) {
return x;
}
}
如果你会减法和加法,那么你就能处理整数除法。在伪代码中它只是:
division y/x is:
First handle signs because we will only divide positive integers
set sign = 0
if y > 0 then y = neg(y), sign = 1 - sign
if x > 0 then y = neg(y), sign = 1 - sign
ok, if sign is 0 nothing to do, if sign is 1, we will negate the result
Now the quotient is just the number of times you can substract the divisor:
set quotient = 0
while y > x do
y = y - x
quotient = quotient + 1
Ok we have the absolute value of the quotient, now for the sign:
if sign == 1, then quotient = neg(quotient)
C++语言的正确翻译以及递归部分留作习题...
递归提示 y/x == 1 + (y-x)/x while y>x
以上是整数部分。整数既好又简单,因为它提供了精确的操作。 base 中的浮点表示总是接近于 mantissa * baseexp 其中 mantissa 是具有最大位数的整数或一个介于 0 和 1 之间的数字(表示正常表示)。您可以从一种表示形式传递到另一种表示形式,但通过尾数的位数更改指数部分:2.5 是 25 10-1 (int mantissa) of .25 101 (0 <= 尾数 < 1).
所以如果你想操作以 10 为底的浮点数你应该:
- 将整数转换为浮点数(尾数+指数)表示法
- 对于加法和减法,结果指数是先验指数中的较大者。两个尾数都应缩放到该指数和 added/substracted。然后必须调整最后的指数,因为该操作可能添加了一个额外的数字 (7 + 9 = 16) 或导致最高阶的数字消失 (101 - 98 - 3)
- 对于乘积,您添加指数并乘以尾数,然后 归一化(调整指数)结果
- 对于除法,您将尾数按最大位数缩放,使用整数除法算法进行除法,然后再次归一化。例如,精度为 6 位的 1/3 是通过以下方式获得的:
1/3 = (1 * 106 /3) * 10-6 = (1000000/3) * 10- 6
它给出 333333 * 10-6 所以 .333333 的标准化形式
好吧,这会是很多沸腾的代码,但没什么难的。
日志故事简短:只记得你是如何用纸和铅笔学会的...
所以 atm 我坚持使用我的计算器。只允许使用以下方法:
int succ(int x){
return ++x;
}
int neg(int x){
return -x;
}
我已经得到的是+、-。 *。 Iterativ 也是递归的(所以如果需要我也可以使用它们)。 现在我坚持使用 divide 方法,因为我不知道如何处理逗号及其背后的逻辑。想象一下处理 succ() 和 neg() 的样子,这是一个减法迭代和递归的例子:
int sub(int x, int y){
if (y > 0){
y = neg(y);
x = add(x, y);
return x;
}
else if (y < 0){
y = neg(y);
x = add(x, y);
return x;
}
else if (y == 0) {
return x;
}
}
int sub_recc(int x, int y){
if (y < 0){
y = neg(y);
x = add_recc(x, y);
return x;
} else if (y > 0){
x = sub_recc(x, y - 1);
x = x - 1;
return x;
}else if( y == 0) {
return x;
}
}
如果你会减法和加法,那么你就能处理整数除法。在伪代码中它只是:
division y/x is:
First handle signs because we will only divide positive integers
set sign = 0
if y > 0 then y = neg(y), sign = 1 - sign
if x > 0 then y = neg(y), sign = 1 - sign
ok, if sign is 0 nothing to do, if sign is 1, we will negate the result
Now the quotient is just the number of times you can substract the divisor:
set quotient = 0
while y > x do
y = y - x
quotient = quotient + 1
Ok we have the absolute value of the quotient, now for the sign:
if sign == 1, then quotient = neg(quotient)
C++语言的正确翻译以及递归部分留作习题...
递归提示 y/x == 1 + (y-x)/x while y>x
以上是整数部分。整数既好又简单,因为它提供了精确的操作。 base 中的浮点表示总是接近于 mantissa * baseexp 其中 mantissa 是具有最大位数的整数或一个介于 0 和 1 之间的数字(表示正常表示)。您可以从一种表示形式传递到另一种表示形式,但通过尾数的位数更改指数部分:2.5 是 25 10-1 (int mantissa) of .25 101 (0 <= 尾数 < 1).
所以如果你想操作以 10 为底的浮点数你应该:
- 将整数转换为浮点数(尾数+指数)表示法
- 对于加法和减法,结果指数是先验指数中的较大者。两个尾数都应缩放到该指数和 added/substracted。然后必须调整最后的指数,因为该操作可能添加了一个额外的数字 (7 + 9 = 16) 或导致最高阶的数字消失 (101 - 98 - 3)
- 对于乘积,您添加指数并乘以尾数,然后 归一化(调整指数)结果
- 对于除法,您将尾数按最大位数缩放,使用整数除法算法进行除法,然后再次归一化。例如,精度为 6 位的 1/3 是通过以下方式获得的: 1/3 = (1 * 106 /3) * 10-6 = (1000000/3) * 10- 6
它给出 333333 * 10-6 所以 .333333 的标准化形式
好吧,这会是很多沸腾的代码,但没什么难的。
日志故事简短:只记得你是如何用纸和铅笔学会的...