Click here to Skip to main content
15,911,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So far I've got an add, subtract and print function as well as a constructor that initializes to zero the array. For some reason the operations(+ and -) made alot of sense to me(i think) so I kind of got ahead of my self and am not too sure how to initialize a big integer, could I get some help with a function such as void assign(const bigint & A) or something like that? Also if there is something already wrong with my code please tell me. Thanks


C#
const int size=30;   //just to make things easier, will change to something big later
class bigint
{
    int digits[size];


public:
    // initializes to zero
    bigint()
    {
        for (int i = 0; i < size; i++)
            digits[i] = 0;

    }

    // prints a big int
    void print()
    {
        bigint B;
        for (int i = size - 1; i >= 0; i--)
        {
            int dig = B.digits[i];
            if (dig != 0)
                std::cout << dig;
        }
    }

    // subtracts a bigint(B) from another(A)
    void subtract(bigint & A, bigint & B)
    {
        for (int i = 0, borrow = 0; i < size; i++)
        {
            if (borrow = ((A.digits[i] -= B.digits[i] + borrow) < 0))
            {
                A.digits[i] += 10;
            }
        }
    }

    // adds a bigint(A) to another(B)
    void add(bigint & A, bigint & B)
    {
        for (int i = 0, carry = 0; i < size; i++)
        {
            if (carry = ((A.digits[i] += B.digits[i] + carry) < 9))
            {
                A.digits[i] -= 10;
            }
        }
    }
};
Posted
Comments
Richard MacCutchan 6-Feb-15 4:15am    
Your array should be of bytes (8 bits, unsigned char) rather than int (32 bits, signed) type. You can then ensure each element contains only the values 0-9, which makes the calculations simpler. Also your print function creates a new bigint rather than printing the current object.

1 solution

Yes, there are a couple of things wrong with your class.

(a) The print function prints the value of a newly created variable B, which will be zero all the time. Just use your own member digits instead! Also: Your print functions contains a bug: It just prints the non-zero digits!

(b) The add and subtract member functions don't touch your member digits at all, which doesn't make sense. Instead you might want to provide functions like
C++
void add (const bigint& A);
void subtract (const bigint& A);

which add/subtract A to/from the object the operation is applied to.

(c) Working in decimal digits will not get you the best performance and is a waste of memory. Why don't you work in base 2 and hence use the built-in arithmetic operations. That will increase the performance of your functions by one or two orders of magnitude. Just for the purpose of printing a number it needs to be converted back to decimal.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900