Custom “Very Long Int” Division Issue

邮差的信 提交于 2019-12-13 03:39:13

问题


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



来源:https://stackoverflow.com/questions/2525172/custom-very-long-int-division-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!