Custom “Very Long Int” Division Issue

2019-09-11 07:47发布

问题:

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.

回答1:

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…



回答2:

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



回答3:

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