I am currently using a bigint class that was given to me. I have managed to create the adding subtracting and multiplication operations successfully, however I cannot seem to crack the division operator.
I am not fussed on getting the remainder of a quotient I am only interested in the number prior to the decimal. I also have added some checks. The first being if the second number is zero then it will return zero. The second check is if is if the second number is greater than the first I return zero as I am not interested in numbers below zero.
Below is my code and what I have done so far for this big int operator.
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;
}
No matter what I enter into my program it always returns a zero, I've been at this for several hours and can't for the life of me crack it. Any help is greatly appreciated.
Some examples of desired results are:
987654321 / 123456789 = 8
123425 / 545 = 226
Cheers