views:

194

answers:

3

Hey everyone,

So, for a very silly project in C++, we are making our own long integer class, called VLI (Very Long Int). The way it works (they backboned it, blame them for stupidity) is this:

  1. User inputs up to 50 digits, which are input as string.
  2. String is stored in pre-made Sequence class, which stores the string in an array, in reverse order.

That means, when "1234" is input, it gets stored as [4|3|2|1].

So, my question is this: How can I go about doing division using only these arrays of chars?

If the input answer is over 32 digits, I can't use ints to check for stuff, and they basically saying using long ints here is cheating.

Any input is welcome, and I can give more clarification if need be, thanks everyone.

+3  A: 

Implement the long division algorithm you learned in grade school.

Start by implementing subtraction. Create a function which can string-subtract any number from the input. Then you should be able to detect whether the result is negative. Modify this function to allow the number to be string-shifted before you subtract…

Potatoswatter
This will work well as long as the divisor is less than `(1<<32)/10`
AShelly
Also, use big groups of digits for increased efficiency - instead of one extra digit for iteration, use 5 or 10 or something. Be careful that the remainder with the added digits don't break `Int.MaxValue`.
ANeves
But see, I can't. What if it's a 41 digit number divided by a 40 digit number?That's why I'm on this site; I don't know how to handle it if 32 < input.size() <= 50.
Befall
@befall: Do what Potatoswatter said. It's quite possible to do the arithmetic directly on strings. It isn't efficient, but efficient arithmetic with arbitrary precision is difficult to get right, and if you're having problems with this concept you'd best get it working and then perhaps aim higher.
David Thornley
+1  A: 

Get your school math book out, you did manual division some years ago in school I suppose. It is exactly the same principle :)

codymanix
+1  A: 

Potatoswatter is correct. I wrote a Pascal program in the past that worked on arbitrary length numbers as strings, and it could calculate the square root as well.

Here is a reminder of technique for long division: Long Division to Decimal Places

Greg Chabala