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

write c++ code and use linked list in class sample Main program: #include \"BigI

ID: 3773568 • Letter: W

Question

write c++ code and use linked list in class

sample Main program:

#include "BigInteger.h"

#include <string>

#include <iostream> 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; }

Develop a class to represent a big integer number according to the following specifications: A big integer is represented as a linked list of digits where each digit is an integer between 0 and 9. For example if 472385 is a big integer then its linked list representation will be as illustrated in Figure 1. Note that the least significant digit of the big integer is the first node of the linked list.

Explanation / Answer

class bigInteger
{
private:
int sign;                     
linkedListType<int> digits;

public:
bigInteger();                         
bigInteger(const bigInteger& other);
bigInteger(const string& number);     
const bigInteger& operator= (const bigInteger& other);
bigInteger& operator+ (bigInteger& other);
};


bigInteger& bigInteger::operator+ ( bigInteger& other )
{
bigInteger resultBI;
bigInteger forSwap;
bool explicitSign = 0;
stack<int> resultStack;
int result;
int carry = 0;
linkedListIterator<int> p = digits.begin();   
linkedListIterator<int> q = other.digits.begin();


if ( this->digits.length() >= other.digits.length() )
{
    while ( q != NULL )
    {
        result = ( *p + *q + carry ) % 10;
        carry = ( *p + *q + carry ) / 10;
        ++p;                              
        ++q;
        resultStack.push(result);        
    }
    while ( p != NULL )
    {
        result = ( *p + carry ) % 10;
        carry = ( *p + carry ) / 10;
        ++p;
        resultStack.push(result);
    }
}

if ( this->digits.length() < other.digits.length())
{
    while ( p != NULL )
    {
        result = ( *p + *q + carry ) % 10;
        carry = ( *p + *q + carry ) / 10;
        ++p;
        ++q;
        resultStack.push(result);
    }
    while ( q != NULL )
    {
        result = ( *q + carry ) % 10;
        carry = ( *q + carry ) / 10;
        ++q;
        resultStack.push(result);
    }
}

if ( carry != 0 )          
{
    resultStack.push(carry);
}


while ( !resultStack.empty())        
{
    resultBI.digits.insert ( resultStack.top() );
    resultStack.pop();
}

if ( explicitSign == 1 )
    resultBI.sign = 1;

return resultBI;

}

int main()
{

bigInteger n1;                
bigInteger n2("987654321");   
bigInteger n3("123456789");   
cout << "n1 = ";
cout << n1 << endl;           
cout << "n2 = ";
cout << n2 << endl;        
cout << "n3 = ";
cout << n3 << endl;         
cout << "n2 + n3 = ";
cout << n2 + n3 << endl;
return 0;
}