【发布时间】:2017-04-20 10:54:58
【问题描述】:
我目前正在使用一个给我的 bigint 类。我已经成功地创建了加减乘法运算,但是我似乎无法破解除法运算符。
我并不热衷于获得商的余数,我只对小数点前的数字感兴趣。我还添加了一些检查。第一个是如果第二个数字为零,那么它将返回零。第二个检查是如果第二个数字大于第一个数字,我返回零,因为我对低于零的数字不感兴趣。
下面是我的代码以及到目前为止我为这个大的 int 运算符所做的工作。
Bigint operator/ (const Bigint& n1, const Bigint& n2) {
Bigint final;
Bigint quotient;
int count = 0;
Bigint result = n1;
Bigint check;
for(int i = 0; i < DIGITS; ++i) {
if(n2.digits[i] == 0){
quotient = 0;
}
else if (n2.digits[i] > n1.digits[i]){
quotient = 0;
}
else {
while (result.digits[0] > 0){
for(int i = 0; i < DIGITS; ++i){
result.digits[i] -= n2.digits[i];
if(result.digits[i] < 0){
result.digits[i] += 10;
result.digits[i+1] = -1;
}
}
count++;
}
for(int j = 1; j < DIGITS; j++){
final.digits[j] = count % 10;
count = count / 10;
}
return final;
}
}
return final;
}
无论我在我的程序中输入什么,它总是返回一个零,我已经在这几个小时了,我一辈子都无法破解它。非常感谢任何帮助。
期望结果的一些例子是:
987654321 / 123456789 = 8
123425 / 545 = 226
干杯
【问题讨论】:
-
如果您在调试器中逐行执行代码并查看所有变量及其值,似乎没有什么问题?计算中的所有步骤都正确吗?
-
好的,那么 // 占位符代码的哪一部分:仅当 n2 为 1 时才正确 你认为我们应该忽略吗?
-
@n.m.抱歉忽略该评论,我以为我已将其删除。
-
“如果第二个数字大于第一个,我返回零,因为我对零以下的数字不感兴趣”让人怀疑你是否知道除法是如何工作的。
-
@websafepalletone 你真的相信 0.75 小于零吗?
标签: c++ biginteger division