Addition Operator Overload

This is a discussion on Addition Operator Overload within the C++ Programming forums, part of the General Programming Boards category; Hey i have an addition operator overload but its not working. I used GDB to go through it and can''t ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    109

    Addition Operator Overload

    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;

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Your array for the result isn't big enough in all cases

    Try doing 6 + 6
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,269
    That's an extremely long piece of code for a bigint operator +. There has got to be a way of simplifying that, probably by combining the same-sign and differing-sign cases. Although I see you're not storing yours in the most compact representation possible, which has contributed to making the code longer as well.

    If you want a fairly efficient big number class, in terms of speed and memory usage, take a look at bigint.h from here:
    http://homepages.ihug.co.nz/~aurora7...ul_Classes.htm
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Floating Point Addition
    By ggraz in forum C Programming
    Replies: 10
    Last Post: 10-12-2008, 12:29 PM
  2. Replies: 7
    Last Post: 06-01-2008, 07:47 AM
  3. Repeated Addition
    By rocksteady in forum C Programming
    Replies: 15
    Last Post: 12-11-2007, 06:05 AM
  4. need some help with binary addition.
    By InvariantLoop in forum C++ Programming
    Replies: 21
    Last Post: 01-27-2005, 05:52 AM
  5. Problems with an addition operator
    By jamjar in forum C++ Programming
    Replies: 12
    Last Post: 03-28-2003, 12:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21