I have a project where i need to create a C++ class that will use very long integers. The software should handle additions of integers of up to 50 digits. I already finished creating a class for sequence that we have to use too store the numbers.

But my question is how would I go about creating a class that would support integer operations on integers with an arbitrarily large number of digits. Very confused student, any help would be great.

Recommended Answers

All 2 Replies

There are a couple of ways you can do this.

First you need to create a BigInteger class. It basically adds and subtracts arbitrary long digits.

One way to do this is to use a string to hold the number, internally.
Then create an add function, that adds one BigInteger to another.
The add function is what you need to create. Apply the same logic step by step as you would if you're adding in paper. Give it a try.

Another way to do this is to separate the digits. For example, get
the input as a string in the BigInteger class constructor. Then internally
have 2 __int64 variable, holding the high and the low byte of the number passed in. Then work from there.

I assume for now, option 1 is easier.

I wish it was easy as holding numbers in string then adding them logically. Although from what you said I understand my project a little better; In regards to the fact that the numbers must be FIRST entered into a pre-defined sequence class we have to use... which is basically holding it like a string value. From there I could then use the same logic as adding on paper.

Which leads me to another question :)

the pre-defined sequence class we have to use only uses ints, bools, and unsigned ints... which would not work with a very large integer.
ex:

bool insert( const EType& X, unsigned I );

Project guidelines call for an I/O format that is highly constrained...so the program must read very large integers expressions from cin. My question is: how would I read in a very large integer number by number so I could insert it into a sequence for logical adding in a "BigInteger class".


EX: a file below with two numbers

5346424356456745756765 1678904353845348538094

how could I read in each numbers separately ([5][3][4][6]...) for the first and second number and put it into a different "sequence" using cin.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.