简体   繁体   English

将字符串转换为大整数?

[英]Convert string to a large integer?

I have an assignment (i think a pretty common one) where the goal is to develop a LargeInteger class that can do calculations with.. very large integers. 我有一个任务(我认为这很常见),目的是开发一个LargeInteger类,该类可以使用..非常大的整数进行计算。

I am obviously not allowed to use the Java.math.bigeinteger class at all. 我显然根本不允许使用Java.math.bigeinteger类。

Right off the top I am stuck. 就在顶部,我被卡住了。 I need to take 2 Strings from the user (the long digits) and then I will be using these strings to perform the various calculation methods (add, divide, multiply etc.) 我需要从用户那里获取2个字符串(长数字),然后我将使用这些字符串来执行各种计算方法(加,除,乘等)。

Can anyone explain to me the theory behind how this is supposed to work? 谁能向我解释这应该如何工作的理论呢? After I take the string from the user (since it is too large to store in int) am I supposed to break it up maybe into 10 digit blocks of long numbers (I think 10 is the max long maybe 9?) 在我从用户那里获取了字符串之后(由于它太大而无法存储在int中),我是否应该将其分解成10位数字的长整数块(我认为10就是最大长整数,也许是9位?)

any help is appreciated. 任何帮助表示赞赏。

First off, think about what a convenient data structure to store the number would be. 首先,考虑一下存储数字的便捷数据结构。 Think about how you would store an N digit number into an int[] array. 考虑一下如何将N位数字存储到int[]数组中。

Now let's take addition for example. 现在以加法为例。 How would you go about adding two N digit numbers? 您将如何添加两个N位数?

Using our grade-school addition, first we look at the least significant digit (in standard notation, this would be the right-most digit) of both numbers. 使用我们的小学加法,首先我们看两个数字最低有效位 (按标准符号,这将是最右边的位)。 Then add them up. 然后将它们加起来。

So if the right-most digits were 7 and 8 , we would obtain 15 . 因此,如果最右边的数字是78 ,我们将获得15 Take the right-most digit of this result ( 5 ) and that's the least significant digit of the answer. 取此结果的最右边数字( 5 ),这是答案的最低有效数字。 The 1 is carried over to the next calculation. 1将结转到下一个计算。 So now we look at the 2nd least significant digit and add those together along with the carry (if there is no carry, it is 0 ). 因此,现在我们看第二个最低有效数字,并将它们与进位加在一起(如果没有进位,则为0 )。 And repeat until there are no digits left to add. 并重复直到没有剩余数字可添加为止。

The basic idea is to translate how you add, multiply, etc by hand into code when the numbers are stored in some data structure. 基本思想是将数字存储在某些数据结构中时,将如何手动将加,乘等转换为代码。

I'll give you a few pointers as to what I might do with a similar task, but let you figure out the details. 我将为您提供一些指导,说明我可能会执行类似的任务,但请让您了解详细信息。

  1. Look at how addition is done from simple electronic adder circuits. 看一下如何通过简单的电子加法器电路完成加法。 Specifically, they use small blocks of addition combined together. 具体来说,他们使用小的加法块组合在一起。 These principals will help. 这些校长会有所帮助。 Specifically, you can add the blocks, just remember to carry over from one block to the next. 具体来说,您可以添加块,只需记住从一个块继续到下一个块即可。
  2. Your idea of breaking it up into smaller blogs is an excellent one. 您将其分解为较小的博客的想法是一个很好的想法。 Just remember to to the correct conversions. 只要记住要正确的转换即可。 I suspect 9 digits is just about right, for the purpose of carry overs, etc. 我怀疑9位数是正确的,用于结转等目的。

These tasks will help you with addition and subtraction. 这些任务将帮助您进行加法和减法。 Multiplication and Division are a bit trickier, but again, a few tips. 乘法和除法有点棘手,但还是有一些技巧。

  1. Multiplication is the easier of the tasks, just remember to multiply each block of one number with the other, and carry the zeros. 乘法是较容易的任务,只需记住将一个数的每个块与另一个数相乘,然后携带零。
  2. Integer division could basically be approached like long division, only using whole blocks at a time. 整数除法基本上可以像长除法一样进行,一次只能使用整个块。

I've never actually build such a class, so hopefully there will be something in here you can use. 我从未真正构建过这样的类,因此希望这里可以使用一些东西。

Look at the source code for MPI 1.8.6 by Michael Bromberger (a C library). 查看Michael Bromberger的MPI 1.8.6的源代码(C库)。 It uses a simple data structure for bignums and simple algorithms. 它为bignum和简单的算法使用简单的数据结构。 It's C, not Java, but straightforward. 它是C,不是Java,而是简单易懂的代码。

Its division performs poorly (and results in slow conversion of very large bignums to tex), but you can follow the code. 它的除法性能很差(并且导致非常大的bignums到tex的缓慢转换),但是您可以遵循代码。

There is a function mpi_read_radix to read a number in an arbitrary radix (up to base 36, where the letter Z is 35) with an optional leading +/- sign, and produce a bignum. 有一个函数mpi_read_radix可以读取带有可选的前导+/-号的任意基数(最高为36,其中Z为35)的数字,并产生一个bignum。

I recently chose that code for a programming language interpreter because although it is not the fastest performer out there, nor the most complete, it is very hackable. 我最近为编程语言解释器选择了该代码,因为尽管它不是执行速度最快,也不是最完整的代码,但是它很容易被黑客入侵。 I've been able to rewrite the square root myself to a faster version, fix some coding bugs affecting a port to 64 bit digits, and add some missing operations that I needed. 我已经能够自己将平方根重写为一个更快的版本,将影响端口的一些编码错误修复为64位数字,并添加了我需要的一些缺少的操作。 Plus the licensing is BSD compatible. 另外,许可与BSD兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM