In my homework I'm given
BigInteger::BigInteger(int val) {
and
BigInteger::BigInteger(string str) {
I'm supposed to implement two constructors to initialize a BigInteger object from an int value and a string storing an integer value. What I really need is just a step in the right direction for I just don't know how to start this.
It also mentions that this can be useful for it
void BigInteger::setDigit(int pos, char digit)
{
if (pos >= size)
{ // not enough space
int newSize = 1;
while (newSize <= pos)
newSize *= 2; // compute newSize as a power of 2 that is bigger than pos
char* temp = digits; // store link to current digits
digits = new char[newSize]; // allocate a new array
memset(digits, 0, newSize); // and fill zeros
if (temp != NULL) // if there are some current digits
memcpy(digits, temp, nDigits); // copy them
size = newSize;
}
digits[pos] = digit; // put the given digit at position pos
if (pos >= nDigits) // update the number of digits!
nDigits = pos + 1;
}
Here's the class definition
class BigInteger
{
private:
char* digits; // the array storing digits
int size; // the current size of digits array
int nDigits; // the current number of digits
void init(int size);
void copy(const BigInteger &num);
public:
BigInteger(); // Default constructor
~BigInteger(); // Default destructor
BigInteger(int val); // Initialize a BigInteger with an integer value
BigInteger(string str); // Initialize a BigInteger with a string storing a number
BigInteger(const BigInteger &num); // Copy constructor
BigInteger& operator=(const BigInteger &num); // Copy assignment operator
int numberOfDigits() const
{
return nDigits;
}
char getDigit(int pos) const; // get the digit at position pos
void setDigit(int pos, char digit); // set the digit at position pos
void print(bool debug = true);
};
The Ultimate goal of the homework is to be able to compute 999! (1*2*3*....999)
Like I said a step in the right direction would be appreciated.
Here is the whole code:
#include"BigInteger.h"
BigInteger::BigInteger()
{
digits = NULL; // Default constructor: storing nothing!
size = 0;
nDigits = 0;
}
BigInteger::~BigInteger()
{
if (digits != NULL)
delete[] digits;
digits = NULL;
size = 0;
nDigits = 0;
}
void BigInteger::init(int size)
{
// Task 1. Allocate memory for digits and fill them as zeros
digits = new char[size];
for (int i = 0; i < size; i++)
{
digits[i] = 0;
}
}
void BigInteger::copy(const BigInteger &num)
{
size = num.size; // copy digits array size
nDigits = num.nDigits; // copy number of digits
digits = new char[size]; // allocate a new digits array with the same size in num
memcpy(digits, num.digits, size); // copy digits array
}
BigInteger::BigInteger(const BigInteger &num)
{ // Copy constructor
copy(num);
}
BigInteger& BigInteger::operator=(const BigInteger &num)
{
if (this != &num)
{ // not assign to the same object
if (digits != NULL)
delete[] digits; // release current digits array
copy(num);
}
return *this;
}
BigInteger::BigInteger(int val)
{
// Task 2a. Construct a BigInteger from an int value
}
BigInteger::BigInteger(string str)
{
// Task 2b. Construct a BigInteger from a string.
}
char BigInteger::getDigit(int pos) const
{
if (pos >= nDigits)
return 0;
else
return digits[pos];
}
void BigInteger::setDigit(int pos, char digit)
{
if (pos >= size)
{ // not enough space
int newSize = 1;
while (newSize <= pos)
newSize *= 2; // compute newSize as a power of 2 that is bigger than pos
char* temp = digits; // store link to current digits
digits = new char[newSize]; // allocate a new array
memset(digits, 0, newSize); // and fill zeros
if (temp != NULL) // if there are some current digits
memcpy(digits, temp, nDigits); // copy them
size = newSize;
}
digits[pos] = digit; // put the given digit at position pos
if (pos >= nDigits) // update the number of digits!
nDigits = pos + 1;
}
BigInteger multiply(BigInteger &x, int y, int pos = 0)
{
int nx = x.numberOfDigits();
BigInteger z;
int carry = 0;
for (int i = 0; i < nx; i++)
{
carry += x.getDigit(i) * y;
z.setDigit(pos++, carry % 10);
carry /= 10;
}
while (carry > 0)
{
z.setDigit(pos++, carry % 10);
carry /= 10;
}
return z;
}
void BigInteger::print(bool debug)
{
if (digits == NULL)
cout << '0';
else
for (int i = nDigits; --i >= 0;)
cout << (int) digits[i];
if (debug)
printf(" [digits = %x, size = %d, nDigits = %d] ",
digits,
size,
nDigits);
}
ostream& operator<<(ostream& out, BigInteger num)
{
//Task 3. Overload operattor << to write a BigInteger object to screen
num.print();
return out;
}