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

#ifndef BIGINTEGER_H #define BIGINTEGER_H #include using namespace::std; const i

ID: 642476 • Letter: #

Question

#ifndef BIGINTEGER_H

#define BIGINTEGER_H

#include

using namespace::std;

const int NUMDIGITS = 50;

// A non-negative integer with NUMDIGITS

// Leading zeores are stored in the number

// if + or * result in overflow, i.e. and answer with more that NUMDIGIST, the overflow flag is set

class BigInteger

{

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

public:

   BigInteger(); // return NULL

   BigInteger(const BigInteger &); // copy constructor

   BigInteger( int );

   BigInteger( const char *);

   ~BigInteger();

   void setToZero();

   bool operator==(const BigInteger &) const;

   bool operator<(const BigInteger &) const;

   bool operator<=(const BigInteger &) const;

   bool operator>(const BigInteger &) const;

   bool operator>=(const BigInteger &) const;

   bool operator!=(const BigInteger &) const;

   const BigInteger & operator=(const BigInteger &) ;

   BigInteger operator+(const BigInteger &) const;

   BigInteger operator*(const BigInteger &) const;

   bool hasOverFlowed(void) const;

   static int getNumberOfDigits(void)

   {

       return NUMDIGITS;

   }

private:

   void convertIntToBigInteger(int); // place the int parameter into the BigInteger format for the invoking instance

   int digits[NUMDIGITS]; // high order digit stored in cell 0, low order digit stored in cell NUMDIGITS-1

   bool overflow;

};

#endif

#include "BigInteger.h"

#include<iostream>

using namespace std;

ostream & operator <<( ostream & out, const BigInteger & r )

{

int digitsStart = 0;

// STUDENTS

// write a for or while loop to place the subscript of the first high order digit that is not 0 in digitsStart

  

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

{

   if(r.digits[i] !=0)

   {

       digitsStart= i ;

       break;

   }

     

     

   }

  

//out<< r.digits[i];

if ( digitsStart == NUMDIGITS ) // the number is all 0's

{

   out << '0';

   return out;

}

for(int i = digitsStart; i < NUMDIGITS; i++) // this code prevents leading 0's from being printed

   out << r.digits[i];

return out;

}

BigInteger::BigInteger()

{

   setToZero();

}

BigInteger::BigInteger(const BigInteger & r)

{

   this->overflow = r.overflow;

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

       this->digits[i] = r.digits[i];

}

BigInteger::BigInteger( int r)

{

   this->convertIntToBigInteger(r);

}

BigInteger::BigInteger( const char * s)

{

   int len = strlen(s);

   setToZero();

   if ( len > NUMDIGITS )

   {

       overflow = true;

       return;

   }

   overflow = false;

   // STUDENTS

   // write a for or while loop to place the correct number in the digits array from the C++ string

   // work right to left and move the digits one by one WILL NEED TO CONVERT A CHAR DIGIT TO A NUMBER DIGIT

   for(int i=0; i< strlen(s); i++)

   return;

}

BigInteger::~BigInteger()

{

   return;

}

void BigInteger::setToZero()

{

   overflow = false;

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

       digits[i] = 0;

}

bool BigInteger::operator==(const BigInteger &r) const

{

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

       if ( digits[i] != r.digits[i] ) return false;

   return true;

}

bool BigInteger::operator<(const BigInteger & r) const

{

   // STUDENTS

   // Write a for or a while loop to compare digits left to right

   // if a smaller digit is found in the invoking instance return true

   // if a larger digit is found in the invoking instance return false

   //for(int i=0; i<NUMDIGITS; i++)

if(NUMDIGITS > r.NUMDIGITS){

return false;}

else if(NUMDIGITS < r.NUMDIGITS){

return true;}

  

for(int i=0; i<NUMDIGITS; i++){

   if(digits[i] >= r.digits[i]){

   return false;}

   }

      

   // for loop has been completed here ==> all digits match, so return false

   return true; // all digits match, both BigIntegers are equal

       }

}

bool BigInteger::operator<=(const BigInteger & r) const

{

   // STUDENTS

   // code needed

if(NUMDIGITS > r.NUMDIGITS){

return false;}

else if(NUMDIGITS < r.NUMDIGITS){

return true;}

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

   if(digits[i] >r.digits[i]){

   return false;}

}

return true;

  

}

bool BigInteger::operator>(const BigInteger & r) const

{

   // STUDENTS

   // code needed

if(NUMDIGITS < r.NUMDIGITS){

  

return false;}

else if(NUMDIGITS > r.NUMDIGITS){

return true;}

for(int i =0; i < NUMDIGITS; i++){

if((digits[i] <=r.digits[i]){

   return false;}

}

return true;

}

bool BigInteger::operator>=(const BigInteger & r) const

{

   // STUDENTS

   // code needed

if(NUMDIGITS < r.NUMDIGITS){

return false;}

else if(NUMDIGITS > r.NUMDIGITS){

return true;}

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

   if(digits[i] < r.digits[i]){

   return false;}

}

return true;

  

//   return !(*this < r);

// return true;

}

bool BigInteger::operator!=(const BigInteger & r) const

{

   return !( *this == r );

}

const BigInteger & BigInteger::operator=(const BigInteger & r)

{

   if ( this == & r ) return *this;

   overflow = r.overflow;

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

       digits[i] = r.digits[i];

   return *this;

}

BigInteger BigInteger::operator+(const BigInteger & r) const

{

   BigInteger answer;

   int carry = 0;

   int temp =0;

   // STUDENTS

   // using a for or a while loop add the digits right to left using the carry

   // review your notes from class

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

   {

       temp = digits[i] + r.digits[i] + carry;

       answer.digits[i] = temp % 10;

       carry = temp / 10;

   }

   // loop exit, set the overflow flag

   answer.overflow = ( carry > 0 );

   return answer;

}

BigInteger BigInteger::operator*(const BigInteger & r) const

{

   BigInteger total(0);

   // STUDENTS

   // modify the code so that the small BigInteger is always used to control the for loop

   for(BigInteger i(0), one(1); i < *this; i = i + one ) // runs faster if *this < r   

   {

       total = total + r;

       if ( total.hasOverFlowed() ) return total; // stop when overflow

   }

   return total;

}

bool BigInteger::hasOverFlowed(void) const

{

   return this->overflow;

}

void BigInteger::convertIntToBigInteger(int r) // place the int parameter into the BigInteger format for the invoking instance

{

this->setToZero();

   if ( r <= 0 ) return;

   int placer = NUMDIGITS - 1; // rightmost digit, work right to left

   int value = r;

   while ( value > 0 && placer >= 0 )

   {

       // Students

       // use % operator to select the right most digit and place in the array

       // change the variable value by removing the right digit using / operator

// move to the left for placing digits in the array

       //

       //

           digits[placer] = value % 10;

           value = value/10;

           placer --;

  

       //

   //

       //  

       //   

       //   

   //   

   }

   return;

}

#include <iostream>

using namespace::std;

#include "BigInteger.h"

int main()

{

   {

       BigInteger a(123456);

       cout<<" a is " << a << endl;

       return 0;

   }

   // STUDENTS

   // test the code piece by piece by adding and removing comments

   // REMEMBER that you can use break points and the watch window to help debugging

   // Your final output should match the output at the end of the program

cout << "Number of digits in our numbers is " << BigInteger::getNumberOfDigits() << endl;

// test default constructor and << operator

BigInteger a;

cout << a << endl;

// test int argument constructor and << operator

BigInteger b(123456789), c(999);

// test <<

cout << b << ' ' << c << endl;

// test copy constructor

BigInteger d(b);

cout << "d is " << d << endl;

// test = operator

d = c;

cout << "d is now " << d << endl;

// test == and + operators

BigInteger e(123456788), f(667788), g(667788);

cout << b << ' ' << e << ' ' << " b == e is " << ( b == e ) << endl;

cout << f << ' ' << g << ' ' << " f == g is " << ( f == g ) << endl;

cout << e << " + " << f << " is " << ( e + f ) << endl;

// test *

BigInteger h(20), k(60);

cout << h << " * " << k << " is " << ( h * k ) << endl;

// Calculate powers of 2 until overflow

BigInteger s(1), value(0), stop(20), one(1), two(2),powerOf2(1), save(5);

while ( !powerOf2.hasOverFlowed())

{

   powerOf2 = two * powerOf2; // multiple by 2 each time

   cout << " 2 to the power of " << s << " is " << powerOf2 << endl;

   s = s + one;

}

// test C++ string argument constructor

BigInteger w("12345678901234567890");

cout << "w is " << w << endl;

BigInteger w1,w2,oneMillion("1000000");

w1 = w + w;

w2 = oneMillion * w;

cout << w1 << " " << w2 << endl;

return;

}

Explanation / Answer

BigInteger.h

BigInteger.cpp

DriverBigInteger.cpp