我目前正在使用一個給我的bigint類。我成功地創建了加法減法和乘法操作,但我似乎無法破解除法運算符。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