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

Implementation: Develop .hpp and .cpp files to implement your ADT. The size of t

ID: 3783596 • Letter: I

Question

Implementation:

Develop .hpp and .cpp files to implement your ADT.

The size of the bigint must be specified by a global constant.

A default constructor to initialize a bigint to zero.

A constructor to initialize a bigint to an int value you provide [0, maxint]. Example: bigint(128).

A constructor to initialize a bigint to a char[] you provide. You can assume what is provided is a valid bigint. Example: bigint("299793").

Write a debug_print method that will be helpful for debugging your bigint. Use a method definition of void debug_print(std::ostream&) const; It simply prints out each element of your bigint array starting from the size of the bigint, minus 1, to zero. Printing a "|" between each value would also be pretting helpful.

Overload output operator<< as a friend, so that it takes a stream and bigint as input and writes the bigint to the stream. It should print at most 70 digits per line. No leading zeros should be printed.

Overload operator== to compare if two bigints are equal. It should return a bool - true if equal and false otherwise.

Testing:

Use the provided set of test cases to demostrate the correctness of each method you develop. You may add additional cases to these tests.

There is one for the three different constructors. They also use the operator<<() and operator==(). So these will be tested at the same time.

These will be a model for the unit tests for the rest of the project.

You will add unit tests for each part of the assignment.

The command make tests will build and run the unit tests.

You must use the class construct to implement your ADT.

The ADT bigint need only work for positive integers.

Use a global constant value for the maximum size of the bigint that is, a constant sized array.

using namespace std; is stricly forbiden. As are any global using statements.

Explanation / Answer

CODE:

#include <iostream>
#include <string>
#include<vector>
#include<cstring>
#include<algorithm>

// Please include this const statement in .hpp file and include that file here.
const int MAX_SIZE = 10;

// BigInt class.
class BigInt
{
private:
// value array contains the BigInt passed.
int value[MAX_SIZE];
// size declared for internal use.
int size;

   public:
// default constructor
BigInt();

// constructor which takes integer.
BigInt(int value);

// constructor which takes character array.
   BigInt( const char *value );

// overloaded << and == friend functions.
   friend std::ostream &operator<<( std::ostream &stm , const BigInt &bi );
friend bool operator==( const BigInt &rhs , const BigInt &lhs );
};

// initialize value array using default constructor
BigInt::BigInt():value(){

}

// initialize value array using integer input.
BigInt::BigInt(int x){

int i = 0;

// declare a vector digits.
std::vector<int> digits;

// store the input as a vector in the digits.
while (x)
{
digits.push_back(x % 10);
x /= 10;
i++;
}

// initialize size variable so that it contains number of digits in the input.
size = i;

// reverse the vector contents to get the correct value.
std::reverse(digits.begin(), digits.end());

// copy the contents to the value array.
std::copy(digits.begin(), digits.end(), value);
}

//initialize value array using char[] input.
BigInt::BigInt( const char *value )
{
// initialize size variable so that it contains number of digits in the input.
size = std::strlen(value);

// convert the character array to integer array and store the same.
for( int i = 0; i < size; ++i )
   {
       this->value[ i ] = *value - '0';
       ++value;
   }
}

// overloaded << operation
std::ostream &operator<<( std::ostream &stm , const BigInt &bi )
{
// return each index value to the output stream.
   for( int i = 0; i < bi.size; ++i )
   {
       stm << bi.value[ i ];
   }
   return( stm );
}

// overloaded == operator
bool operator==( const BigInt &rhs , const BigInt &lhs ) //compare
{
// check if the size of both the inputs are same, if not return false.
if(lhs.size != rhs.size)
return false;

// compare every index value.
for(int i=0;i<lhs.size;i++){
if (lhs.value[i] != rhs.value[i])
return false;
}

return true;
}

int main()
{
// first we input character "4567"
BigInt bi1( "4567" );
// output the value using overloaded << operator
   std::cout << bi1 << std::endl;

// second we input integer 4567
BigInt bi2 (4567);
// output the value using overloaded << operator
std::cout << bi2 << std::endl;

// compare bi1 and bi2.
// 1 - indicates EQUAL
// 0 - indicates UNEQUAL.
bool re = (bi1 == bi2);

// using printf here because << is overloaded.
printf(" Are bi1 and bi2 equal? %d ",re);

// Thirs we input integer 1234
BigInt bi3 (1234);
// output the value using overloaded << operator
std::cout << bi3 << std::endl;

// compare
re = (bi1 == bi3);

// using printf here because << is overloaded.
printf(" Are bi1 and bi3 equal ? %d ",re);
}

OUTPUT:

$ g++ bingint.cpp

$ ./a.out

4567
4567

Are bi1 and bi2 equal? 1

1234

Are bi1 and bi3 equal ? 0