Question: Implement a fully working overloaded division and modulo operators of a LargeInteger class representing numbers as a DoublyLinkedList class. Assume you have a proper DoublyLinkedList

Implement a fully working overloaded division and modulo operators of a LargeInteger class representing numbers as a DoublyLinkedList class. Assume you have a proper DoublyLinkedList API available. Use the code of the LargeInt below. Current implementation of the division and modulo fails. Assume you have all other operators properly implemented, don't look at my current formatting as it might be bad due to me just pasting the code to fit the character limit:
class LargeInt {
private:
DoublyLinkedList digits;
public:
// Constructor
LargeInt(const std::string& str ="0"){
for (char c : str){
if (std::isdigit(c)){
digits.insertBack(c -'0');
}
}
}
LargeInt operator/(const LargeInt& a, const LargeInt& b){
if (b == LargeInt("0")){
throw std::runtime_error("Division by zero");
}
if (a < b){
return LargeInt("0");
}
LargeInt quotient;
LargeInt remainder;
LargeInt dividend = a;
int num_digits = a.digits.size()- b.digits.size()+1;
for (int i =0; i < num_digits; ++i){
remainder.digits.insertBack(*dividend.digits.begin());
dividend.digits.deleteItem(*dividend.digits.begin());
int digit =0;
while (remainder >= b){
remainder = remainder - b;
++digit;
}
quotient.digits.insertBack(digit);
}
while (!quotient.digits.isEmpty() && *quotient.digits.begin()==0){
quotient.digits.deleteItem(*quotient.digits.begin());
}
if (quotient.digits.isEmpty()){
quotient.digits.insertBack(0);
}
return quotient;
}
// Modulo operator
LargeInt operator%(const LargeInt& a, const LargeInt& b){
if (b == LargeInt("0")){
throw std::runtime_error("Division by zero");
}
LargeInt remainder = a;
while (remainder >= b){
LargeInt temp = b;
int count =0;
while (remainder >= temp){
temp.digits.insertBack(0);
++count;
}
temp.digits.deleteItem(temp.digits.size()-1);
--count;
remainder = remainder - temp;
}
return remainder;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!