大整数的C ++整数

2023-09-11 04:27:17 作者:清华北大得不到的学生

我知道这个问题可能已经问在这个论坛很多次,在网上为好。有人问我创建的C ++大整数的实现,但有一个限制,我的构造函数中的一个应该采取一个int作为参数...所以我猜测将会有一个以上的非默认的构造函数.. 。所以我的问题是,什么是做到这一点?最简单的方法

I know this question has probably been asked in this forum many times and in the web as well. I am asked to create an implementation of a big integer in c++, however there is a constraint that one of my constructor should take an int as an argument... so I am guessing there will be more than one non-default constructor... so my question is, what would be the easiest way to do this??

推荐答案

现在的问题的话,似乎是我怎么把一个整数转换位的名单?换句话说,什么是一个整数的基2重presentation?

The question, then, seems to be "how do I turn an integer into a list of bits"? Put another way, what's the base-2 representation of an integer?

由于这应该是家庭作业,让我谈谈解决此问题,在碱-10思维;适当的修改应该是显而易见的一些想法。

As this is supposed to be homework, let me talk around the problem by thinking in base-10; the appropriate changes should be obvious with some thought.

如果有一个基10号,这是pretty的容易找出最右边的数字是:这只是在由10例如除以剩余如果n = 1234,那么它的最右边的数字为n%10 = 4获得下一个最右边的数字,我们除以10(获得123),并重复上述过程。所以:

Given a base 10 number, it's pretty easy to figure out what the rightmost digit is: It's just the remainder when dividing by 10. E.g. if n=1234, then it's rightmost digit is n%10 = 4. To get the next rightmost digit, we divide by 10 (getting 123), and repeat the process. So:

1234/10=123; 1234%10 = 4
123/10=12  ; 123%10 = 3
12/10=1    ; 12%10 = 2
1/10=0     ; 1%10 = 1

所以,现在我们已经得到了答案[4,3,2,1]。如果我们扭转它们,我们有编号的基极 - 10个数字:[1,2,3,4]

So now we've gotten the answers [4,3,2,1]. If we reverse them, we have the base-10 digits of our number: [1, 2, 3, 4].