Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with (+) operator and rest function only A big integer is represente

ID: 3773465 • Letter: I

Question

I need help with (+) operator and rest function only

A big integer is represented as a linked list of digits where each digit is an integer between 0 and 9 . For example if 583824ia a big integer then its linked list representation will be as illustrated in Figure 1.

Provide the following functions:

* A constructor with string parameter that initializes a big integer object to the digits of the string parameter.You should use default parameter of enpty string.

* A copy constructor , a destructor and an overloaded assignment operator.

* An overloaded stream insertion operator (<<) to display a big integer.

* An overloaded (greater than , > ) operator to check if this big integer is greater than another big integer.

* An overloaded + operator   to add two big integers. Notice that you have to add digit by digit and to take care ot the carry when two digits add to more than 9. For example adding 6 and 7 is 13 but 13 cannot be stored as a single digit so it should be stored as 3 and you carry 1 to the next digit and so on.

* A rest function that takes one string parameter, The function should delete all the digits of the current integer and create new digits based on the parameter.


class LinkedListIterator
{
public:
   LinkedListIterator(); // default constructor
   LinkedListIterator(nodeType *ptr);
   int operator*();
   LinkedListIterator operator++();
   bool operator== (const LinkedListIterator & right) const;
   bool operator!= (const LinkedListIterator & right) const;
private:
   nodeType *current;

};


LinkedListIterator::LinkedListIterator()
{
   current = NULL;
}

LinkedListIterator::LinkedListIterator(nodeType *ptr)
{
   current = ptr;
}
int LinkedListIterator::operator*()
{
   return current->info;
}

LinkedListIterator LinkedListIterator::operator ++()
{
   current = current->link;
   return *this;
}
bool LinkedListIterator::operator!= (const LinkedListIterator & right) const
{
   return (current != right.current);
}
bool LinkedListIterator::operator== (const LinkedListIterator & right) const
{
   return (current == right.current);

}

"BigInteger.h"


struct nodeType
{
    char info; // the information in a node
   nodeType *link; // points to the next node
};

class BigInteger
{
   friend ostream& operator<<(ostream & osObject, const BigInteger& obj);

void copyList(const BigInteger & otherlist);
public:
      BigInteger( const string & str = "");
   BigInteger(const BigInteger & otherlist); // copy constructor
   const BigInteger& operator=(const BigInteger& ); // overload the assignment operator
   void destroyList();

    int length() const;

    void insertfirst(const char& newitem);

   ~BigInteger(); //destructor

protected:
   int count; // to count how many elements are there in a node
   nodeType *first; // points to the first node
   nodeType *last; // points to the last node

};

BigInteger::BigInteger(const string & n)
{

   first=NULL;
   last=NULL;
   count=0;

int len = n.length();


   for(int i=0;i <len;i++)

{
       if(isdigit(n[i]))
           insertFirst(n[i]);
   }
   
}

void BigInteger::destroyList()
{
   nodeType *temp; // pointer to deallocate memory
   while (first != NULL)
   {
       temp = first;
       first = first->link;
       delete temp;
   }
   last = NULL;
   count = 0;
}
BigInteger::~BigInteger()
{
   destroyList(); // call function destroylist()
}
void BigInteger::copyList(const BigInteger& otherList)
{
   nodeType *newnode; // create a new node
   nodeType *temp; // traverse the list ptr

   if (first != NULL) // if list is not empty make it empty
       destroyList();
   if (otherList.first == NULL) // otherlist is empty
   {
       first = NULL;
       last = NULL;
       count = 0;
   }

   else
   {
       temp= otherList.first; // current points to the list to be copied
       count = otherList.count;
       // copy the first node
       first = new nodeType;
       first->info = temp->info;
       first->link = NULL;

       last = first;

       temp= temp->link;

       //copy the remaining list
       while (temp!= NULL)
       {
           newnode = new nodeType;
           newnode->info = temp->info;
           newnode->link = NULL;
           last->link = newnode;
           last = newnode;

           temp= temp->link;
       } // while
   } //else

}
BigInteger::BigInteger(const BigInteger& otherlist)
{
   first = NULL;
   copyList(otherlist);
}
const BigInteger& BigInteger::operator=(const BigInteger& otherlist)
{
   if (this != &otherlist)
   {
       copyList(otherlist);
   }
   return *this;
}

ostream & operator<<(ostream & osObject, const BigInteger& Object)
{
   nodeType * tmp;
   tmp=Object.first;

   while(tmp != NULL)
   {
       osObject        tmp=tmp->link;
   }
   return osObject;
}

int BigInteger::length() const
{
   return count;
}

void BigInteger::insertfirst(const char & newitem)
{
   nodeType *newnode;
   newnode = new nodeType;
   newnode->info = newitem;
   newnode->link = first;
   first = newnode;
   count++;
   if (last == NULL)
       last = newnode; // if list is empty new node the last node is the new node

}

Main program :

#include "BigInteger.h"
#include
#include

using namespace std;

int main()
{
    int n;
    BigInteger f1("99999"), f2("888"), tmp;

    tmp = f2;

    cout << "f1 = " << f1 << endl;
    cout << "f2 = " << f2 << endl;
    cout << "tmp = " << tmp << endl;

    // the next statement will work only if you do the
    // bonus question (i.e. overload the + operator)
    cout << f1 << " + " << f2 << " = " << f1+f2 << endl;

    tmp.reset("123456789");
    cout << "tmp after reset = " << tmp << endl;
    tmp.reset("0");
    cout << "tmp after reset = " << tmp << endl;
    tmp.reset("");
    cout << "tmp after reset = " << tmp << endl;

    return 0;
}

The program will produce the output shown below

8 8 2 4 Figure1

Explanation / Answer

Here operator + overloading .... adding current object value to new object value ... and making this new object as current object and return.

const BigInteger& BigInteger::operator+(const BigInteger& otherlist)
{

//CHECKING DIFFERENT LIST OR NOT
if (this != &otherlist)
{

//CREATING NEW OBJECT WITH ADDING BOTH VALUES
BigInteger newtmp = new BigInteger(this.info + otherlist.info);
this = newtmp;
}
return *this;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote