Hey i have an addition operator overload but its not working. I used GDB to go through it and can''t figure out why its not working. Maybe someone can see why.
Code:
//addition operator overload
BigInt operator +(const BigInt& a, const BigInt& b)
{
//loop variable
int i;
//used in negative case
int borrow = 0;
//used in positive case
int carry = 0;
//basically the result of the added numbers
BigInt c;
//variables a and b will be set to so they can
//be edited
BigInt editA = a;
BigInt editB = b;
//temp pointer
int *temp;
//adds integers together when the same sign
if(a.positive == b.positive)
{
//makes c.dig the larger number
if (a.numDigits <= b.numDigits)
{
c.numDigits = b.numDigits;
}
else
{
c.numDigits = a.numDigits;
}
//makes room for the new array
c.digits = new int[c.numDigits];
//sets c.pos to the sign of the integers
c.positive = a.positive;
//initalizes c.digits to zero
for(i=0; i<c.numDigits; i++)
{
c.digits[c.numDigits-i] = 0;
}
//loop to add the two integers together
for(i=0; i<c.numDigits; i++)
{
//add carry to c.dig
c.digits[c.numDigits-i] += carry;
if(i<a.numDigits)
{
c.digits[c.numDigits-i] += a.digits[a.numDigits-i];
}
if(i < b.numDigits)
{
c.digits[c.numDigits-i] += b.digits[b.numDigits-i];
}
if(c.digits[c.numDigits-i] > 9)
{
c.digits[c.numDigits-i] -= 10;
carry = 1;
}
else
{
carry = 0;
}
}
if(carry == 1)
{
//remember where old c array is with temp ptr
temp = c.digits;
//make numDig one index bigger
c.numDigits ++;
// make new array
c.digits = new int [c.numDigits];
//loop through old c into new c
for(i = 0; i<c.numDigits; i++)
{
c.digits[c.numDigits-i] = temp[c.numDigits-i];
}
//put carry into last space
c.digits[c.numDigits -1] = carry;
//delete old c array
delete [] temp;
}
}
//case when the two interges have different signs
if(editA.positive != editB.positive)
{
//set both signs == positive
editA.positive = editB.positive = true;
//when a=b c.ND = 1, && c.digit[0] = 0
if(editA == editB)
{
c.numDigits = 1;
c.digits[0] = 0;
return c;
}
//set c to be larger magnitude
if(editA>editB)
{
c = editA;
}
else
{
c = editB;
}
//loop subtracting smaller integer from c
for(i=0; i<c.numDigits; i++)
{
//add borrow to the digit
c.digits[c.numDigits-i] -= borrow;
//subtracts the larger num from the smaller num
if(editA > editB)
{
c.digits[c.numDigits-i] -= editB.digits[i];
}
else
{
c.digits[c.numDigits-i] -= editA.digits[i];
}
//when subtracting, if the digit is less than zero
//add 10 to digit and borrow, else don't borrow
if(c.digits[i] < 0)
{
c.digits[c.numDigits-i] += 10;
borrow = 1;
}
else
{
borrow = 0;
}
}
//set positives back to original state
if(editA > editB)
{
c.positive = a.positive;
}
else
{
c.positive = b.positive;
}
//if first index is !=0
if(c.digits[c.numDigits -1] != 0)
{
return c;
}
//make smaller array if needed
//loops to find where 0's stop
i=0;
while(c.digits[c.numDigits -1 -i] == 0)
{
//subtracts one from ND for each 0
i++;
}
//numdigts new correct value
c.numDigits -= i;
//make new array by using a temp ptr
temp = c.digits;
c.digits = new int[c.numDigits];
//copy digits into new array
for(i=0; i<c.numDigits; i++)
{
c.digits[c.numDigits-i] = temp[c.numDigits-i];
}
//delete old array
delete [] temp;
}
return c;