MyInt::MyInt(int num)
{
maxSize = 5;
currentSize = 0;
numArray = new int[maxSize];
int temp;
if(num >= 0)
{
for(int i = 0; num > 0; i++)
{
temp = num % 10;
num = num / 10;
numArray[i] = temp;
currentSize++;
if(currentSize == maxSize)
{
Grow();
}
}
}
else
{
numArray[0] = 0;
}
}
MyInt::MyInt(char* n)
{
maxSize = 5;
currentSize = 0;
numArray = new int[maxSize];
for(int i = 0; i < strlen(n); i++)
{
if(strlen(n) == 0)
{
numArray[i] = 0;
}
else if(isdigit(n[i]))
{
switch(n[i])
{
case 48: numArray[i] = 0;
break;
case 49: numArray[i] = 1;
break;
case 50: numArray[i] = 2;
break;
case 51: numArray[i] = 3;
break;
case 52: numArray[i] = 4;
break;
case 53: numArray[i] = 5;
break;
case 54: numArray[i] = 6;
break;
case 55: numArray[i] = 7;
break;
case 56: numArray[i] = 8;
break;
case 57: numArray[i] = 9;
break;
}
currentSize++;
if(currentSize == maxSize)
{
Grow();
}
}
else
{
numArray[i] = 0;
}
}
}
void MyInt::Shrink()
{
maxSize = maxSize - 5; // Determine a new size.
int* array = new int[maxSize]; // Allocate a new array.
for (int i = 0; i < currentSize; i++) // Copy each entry into
array[i] = numArray[i]; // the new array.
delete [] numArray; // Remove the old array
numArray = array; // Point old name to new array.
cout << "New allocated size is " << maxSize << "\n";
}
void MyInt::Grow()
{
maxSize = currentSize + 5; // Determine a new size.
int* array = new int[maxSize]; // Allocate a new array.
for (int i = 0; i < currentSize; i++) // Copy each entry into
array[i] = numArray[i]; // the new array.
delete [] numArray; // Remove the old array
numArray = array; // Point old name to new array.
cout << "New allocated size is " << maxSize << "\n";
}
MyInt::~MyInt()
// This destructor function for class MyInt
// deallocates the MyInt's pointer to its entry list.
{
delete [] numArray;
}
MyInt::MyInt(const MyInt& i)
// copy constructor. Initialize object as a copy of D
{
maxSize = i.maxSize;
currentSize = i.currentSize;
// allocate new array
numArray = new int[maxSize];
// copy the list over from i
for (int j = 0; j < currentSize; j++)
numArray[j] = i.numArray[j];
}
MyInt& MyInt::operator= (const MyInt& i)
// assignment operator
{
if (this != &i) // only make the copy if the original is not this same object
{
//delete the existing array
delete [] numArray;
// now copy
maxSize = i.maxSize;
currentSize = i.currentSize;
numArray = new int[maxSize];
for (int j = 0; j < currentSize; j++)
numArray[j] = i.numArray[j];
}
return *this; // return this object
}
/*FRIEND FUNCTIONS*/
/*OPERATOR OVERLOADING*/
ostream& operator<<(ostream& os, const MyInt& i)
{
for(int j = i.currentSize; j >= 0; j--)
os << i.numArray[j];
return os;
}
/*
istream& operator>>(istream& is, MyInt& t)
{
}
*/
MyInt operator+ (const MyInt& x, const MyInt& y)
{
int remainder;
int temp;
int temp1;
int temp2;
int* p;
if((x.currentSize == y.currentSize) && (x.currentSize == x.maxSize))
{
x.Grow();
y.Grow();
}
else if(x.currentSize < y.currentSize)
{
temp = x.currentSize;
temp2 = y.currentSize - x.currentSize;
}
else
{
temp = y.currentSize;
temp2 = x.currentSize - y.currentSize;
}
if(x.maxSize > y.maxSize)
MyInt array[x.maxSize];
else
MyInt array[y.maxSize];
for(int i = 0; i <= temp; i++)
{
if(remainder > 0)
{
x.numArray[i] = x.numArray[i] + remainder;
}
array.numArray[i] = x.numArray[i] + y.numArray[i];
if(array[i] >= 10)
{
array[i] = array[i] % 10;
remainder = array[i] / 10;
}
}
for(temp1 = temp + 1; temp1 < (temp + temp2); temp1++)
{
if(x.currentSize < y.currentSize)
{
array[temp1] = y.numArray[temp1];
}
else
{
array[temp1] = x.numArray[temp1];
}
}
return array;
}
MyInt operator* (const MyInt& x, const MyInt& y)
{
int temp;
int temp1;
int temp2;
if(x.maxSize > y.maxSize)
{
do
{
if(((x.currentSize + y.currentSize) - 1) > x.maxSize)
x.Grow();
}while(((x.currentSize + y.currentSize) - 1) > x.maxSize);
temp1 = x.maxSize;
}
else
{
do
{
if(((x.currentSize + y.currentSize) - 1) > y.maxSize)
y.Grow();
}while(((x.currentSize + y.currentSize) - 1) > y.maxSize);
temp1 = y.maxSize;
}
int array[temp1];
int array1[temp1];
for(int k = 0; k < temp1; k++)
{
array1[k] = 0;
}
if(x.currentSize < y.currentSize)
temp = x.currentSize;
else
temp = y.currentSize;
for(int i = 0; i <= x.currentSize; i++)
{
for(int j = 0; j <= y.currentSize; j++)
{
array[i] = x.numArray[i] * (y.numArray[j] * (10^j));
array1[i] = array1[i] + array[i];
}
}
return array1;
}
/*-----------------------------OPERATOR OVERLOADING PART 2------------------------------------------------------*/
bool operator==(const MyInt& x, const MyInt& y)
{
int counter;
if(x.currentSize < y.currentSize)
return false;
else if(x.currentSize > y.currentSize)
return false;
else if(x.currentSize == y.currentSize)
{
for(int i = x.currentSize; i >= 0; i--)
{
if(x.numArray[i] < y.numArray[i])
return false;
else if(x.numArray[i] > y.numArray[i])
return false;
else if(x.numArray[i] == y.numArray[i])
{
counter++;
}
}
}
if(counter == x.currentSize)
return true;
}
bool operator!=(const MyInt& x, const MyInt& y)
{
return (!(x == y));
}
bool operator<(const MyInt& x, const MyInt& y)
{
int counter;
if(x.currentSize < y.currentSize)
return true;
else if(x.currentSize > y.currentSize)
return false;
else if(x.currentSize == y.currentSize)
{
for(int i = x.currentSize; i >= 0; i--)
{
if(x.numArray[i] < y.numArray[i])
return true;
else if(x.numArray[i] > y.numArray[i])
return false;
else if(x.numArray[i] == y.numArray[i])
{
counter++;
}
}
}
if(counter == x.currentSize)
return false;
}
bool operator>(const MyInt& x, const MyInt& y)
{
return(!(x <= y));
}
bool operator>=(const MyInt& x, const MyInt& y)
{
return(!(x < y));
}
bool operator<=(const MyInt& x, const MyInt& y)
{
int counter;
if(x.currentSize < y.currentSize)
return true;
else if(x.currentSize > y.currentSize)
return false;
else if(x.currentSize == y.currentSize)
{
for(int i = x.currentSize; i >= 0; i--)
{
if(x.numArray[i] < y.numArray[i])
return true;
else if(x.numArray[i] > y.numArray[i])
return false;
else if(x.numArray[i] == y.numArray[i])
{
counter++;
}
}
}
if(counter == x.currentSize)
return true;
}