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

4 Procedure 4.1 Provided Code Download the files for this lab: the bigint.h head

ID: 3683854 • Letter: 4

Question

4 Procedure 4.1 Provided Code Download the files for this lab: the bigint.h header and some test programs. mkdir/lab-bigint cd /lab-bigint wget http://bits.usc.edu/files/cs103/lab-bigint.tar tar xvf lab-bigint.tar 4.2 [3 pts.] constructor, to_string0 Implement the constructor and print function for the Biglnt class. . We have provided bigint.h that is mostly complete: o add the necessary private data member to the definition. Create bigint.cpp and fill in the actual definitions for the constructor and the . to string() function * Run make test0 and./test0, which should print out 103. 4.3 [5 pts.l addo Implement the add() function. Before starting to code, write up pseudocode for how the addition should work. How many times should it loop? How will you represent the "carried" number? Does it use push_back(), which appends to the end of a vector? * Note that inside of void Biglnt::add(Biglnt b), you are allowed to access the private variables of b. This is because your code is part of the definition of Biglnt. Privacy prevents other classes from seeing inside, but not other objects of the same class * In order to get your numbers to line up, you might like to add some extra zeroes to one vector or the other. Note that inside of void Biglnt:add(Biglnt b), you are allowed to change the data members of b. This is safe because C++ uses pass-by-value, even with objects: you are only changing a copy of · Run·/test1, which should print 55 and 68. Create and run a test of your own design that checks whether your code is correct in other cases (carrying, different lengths, etc). Look at the other sample programs provided. ·

Explanation / Answer

#include <iostream>

#include <vector>

#include <string>

#include <fstream>

using namespace std;

class BigInt

{

     public:

     BigInt();

     BigInt(int);

     BigInt(string &);

     BigInt& operator+=(const BigInt&);

     BigInt& operator++();

     BigInt operator++(int);

     int& operator[](int index) ;

     BigInt& operator*=(const BigInt&);

     friend ostream& operator<<(ostream&, const BigInt&);

     friend istream& operator>>(istream&, BigInt&);

     BigInt operator+(const BigInt&) ;

     BigInt operator*(const BigInt&);

     void times_10(int n);

     void times_single_digit(int n);

     int size() const;

     string to_string();

     ~BigInt();

     private:

     /* data */

     vector<int> num;

};

BigInt :: BigInt()

     {

          num.push_back(0);

     }

BigInt :: BigInt(int n)

     {

          while(n!=0)

              {

                   num.push_back(n%10);

                   n/=10;

              }

     }

BigInt :: BigInt(string &str)

     {

          /*LOOP*/

          for(int i=str.size()-1 ; i>=0 ; i--)

              {

                   num.push_back(str[i] - '0');

              }

     }

BigInt& BigInt :: operator+=(const BigInt& num2)

     {

          // this->num = num + n;

          // return *this;

          vector<int> sum;

          vector<int> num1 = num;

          int size1 = num1.size();

          int size2 = num2.size();

          int size = (size1>size2)?size1:size2;

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

              {

                   if(i>=size1)

                        {

                             sum.push_back(num2[i]);

                        }

                   else if(i>=size2)

                        {

                             sum.push_back(num1[i]);

                        }

                   else

                        {

                             sum.push_back(num1[i]+num2[i]);

                        }

              }

for(int i=1;i<sum.size();i++)

     {

          sum[i] = sum[i]+sum[i-1]/10;

          sum[i-1]%=10;

     }

if(sum[sum.size()-1]>9)

     {

          sum.push_back(sum[sum.size()-1]/10);

          sum[sum.size()-2]%=10;

     }

     num = sum;

     return *this;

}

BigInt& BigInt :: operator++()

     {

          vector<int> unit(1,1);

          num += unit;

          return *this;

     }

BigInt BigInt :: operator++(int)

     {

          BigInt old = *this;

          ++(*this);

          return old;

     }

int& BigInt :: operator[](int index)

     {

          return *(num.begin()+index);

     }

BigInt& BigInt :: operator*=(const BigInt& num2)

     {

          BigInt prod;

          for(int i=0;i<num2.size();i++)

              {

                   BigInt temp = *this;

                   temp.times_single_digit(num2[i]);

                   temp.times_10(i);

                   prod+=temp;

              }

          *this = prod;

          return *this;

     }

ostream& operator<<(ostream& out, const BigInt& n)

     {

          for(int i=n.size()-1 ; i>=0 ; i--)

              {

                   out << n[i];

              }

          return out;

     }

istream& operator>>(istream& in, BigInt& n)

     {

          for(int i=n.size()-1 ; i>=0 ; i--)

              {

                   in >> n[i];

              }

          return in;

     }

BigInt BigInt :: operator+(const BigInt& num2)

     {

          vector<int> sum;

          vector<int> num1 = num;

int size1 = num1.size();

int size2 = num2.size();

int size = (size1>size2)?size1:size2;

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

{

if(i>=size1)

{

sum.push_back(num2[i]);

}

else if(i>=size2)

{

sum.push_back(num1[i]);

}

else

{

sum.push_back(num1[i]+num2[i]);

}

}

for(int i=1;i<sum.size();i++)

{

sum[i] = sum[i]+sum[i-1]/10;

sum[i-1]%=10;

}

if(sum[sum.size()-1]>9)

{

sum.push_back(sum[sum.size()-1]/10);

sum[sum.size()-2]%=10;

}

string str;

for(int i=sum.size()-1;i>=0;i--)

str.push_back(sum[i]+'0');

return BigInt(sum);

}

BigInt BigInt :: operator*( const BigInt& num2)

{

BigInt temp = *this;

temp*= num2;

return temp;

}

void BigInt :: times_10(int n)

{

vector<int> p_10(n,0);

for(int i=0; i<num.size() ; i++)

{

p_10.push_back(num[i]);

}

this->num = p_10;

}

void BigInt :: times_single_digit(int n)

{

num[0]*=n;

for(int i=1;i<num.size();i++)

{

num[i] = num[i]*n + num[i-1]/10;

num[i-1]%=10;

}

if(num[num.size()-1]/10)

{

num.push_back(num[num.size()-1]/10);

num[num.size()-2]%=10;

}

}

int BigInt :: size() const

{

return num.size();

}

string BigInt :: to_string()

{

string str;

for(int i=num.size()-1 ; i>=0 ; i--)

{

str.push_back(num[i]+'0');

}

return str;

}

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