c++ 中的大整数

发布于 2024-08-13 01:41:25 字数 128 浏览 3 评论 0原文

我知道这个问题可能已经在这个论坛和网络上被问过很多次了。我被要求在 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??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

執念 2024-08-20 01:41:25

那么,问题似乎是“如何将整数转换为位列表”?换句话说,整数的以 2 为底的表示形式是什么?

由于这应该是家庭作业,所以让我通过以 10 为基数的思考来讨论这个问题;经过一番思考,适当的改变应该是显而易见的。

给定一个以 10 为基数的数字,很容易算出最右边的数字是什么:它只是除以 10 时的余数。例如,如果 n=1234,那么它的最右边的数字是 n%10 = 4。要获取下一个最右边的数字,我们除以 10(得到 123),然后重复该过程。所以:

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]。

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?

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.

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

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].

指尖凝香 2024-08-20 01:41:25

C++ BigInt 类
C++ 大整数库
例如编写 big int :

typedef struct {
    int high, low;
} BiggerInt;

BiggerInt add( const BiggerInt *lhs, const BiggerInt *rhs ) {
    BiggerInt ret;

    /* Ideally, you'd want a better way to check for overflow conditions */
    if ( rhs->high < INT_MAX - lhs->high ) {
        /* With a variable-length (a real) BigInt, you'd allocate some more room here */
    }

    ret.high = lhs->high + rhs->high;

    if ( rhs->low < INT_MAX - lhs->low ) {
        /* No overflow */
        ret.low = lhs->low + rhs->low;
    }
    else {
        /* Overflow */
        ret.high += 1;
        ret.low = lhs->low - ( INT_MAX - rhs->low ); /* Right? */
    }

    return ret;
}

C++ BigInt class
C++ Big Integer Library
to write big int for example :

typedef struct {
    int high, low;
} BiggerInt;

BiggerInt add( const BiggerInt *lhs, const BiggerInt *rhs ) {
    BiggerInt ret;

    /* Ideally, you'd want a better way to check for overflow conditions */
    if ( rhs->high < INT_MAX - lhs->high ) {
        /* With a variable-length (a real) BigInt, you'd allocate some more room here */
    }

    ret.high = lhs->high + rhs->high;

    if ( rhs->low < INT_MAX - lhs->low ) {
        /* No overflow */
        ret.low = lhs->low + rhs->low;
    }
    else {
        /* Overflow */
        ret.high += 1;
        ret.low = lhs->low - ( INT_MAX - rhs->low ); /* Right? */
    }

    return ret;
}
腻橙味 2024-08-20 01:41:25

为什么要重新发明轮子?使用GNU MP 库

[编辑] 闻起来像家庭作业。因此,当您有一个 BigBit 类时,请执行以下操作:

  1. 清除所有位
  2. 编写一个循环,遍历构造函数的 int 参数上的所有位
  3. 对于 < 中的每个位code>int 参数为 != 0,设置 BigBit 向量中的位。

Why reinvent the wheel? Use the GNU MP library.

[EDIT] Smells like homework. So when you have a BigBit class, then do this:

  1. Clear all bits
  2. Write a loop which goes over all bits on the int argument of the constructor
  3. For each bit in the int argument which is != 0, set the bit in the BigBit vector.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文