BigInteger 家庭作业需要帮助

发布于 2024-10-17 15:10:08 字数 2155 浏览 1 评论 0原文

有人告诉我必须编写一个 BigInteger 类,我知道有一个类,但我必须编写自己的类。我将采用整数或字符串并将它们转换为数组来存储它们。从那里我将允许对数字进行加法、减法和乘法。我让它获取整数和字符串并制作数组,效果很好。我对其余的有问题。

对于添加,我尝试制作一些东西来检查数字类型数组的大小,然后设置较小和较大的值。从那里开始,我让它循环,直到它到达较小的末尾,并且当它循环时,它将数组的该部分的数字作为两个数字并将它们相加。现在这是可以的,直到它们大于 10,在这种情况下我需要结转一个数字。我想我在某个时候也有这样的工作。

请记住,我的 BigInt 有两件事:数字数组和符号整数(1 或 -1)。

因此,在这种情况下,我在添加正确且符号正确时遇到问题。与减法相同。

至于乘法,我完全迷失了,甚至没有尝试过。下面是我尝试制作的一些代码:(添加函数),请帮助我。

public BigInt add(BigInt val){

        int[] bigger;
        int[] smaller;
        int[] dStore;
        int carryOver = 0;
        int tempSign = 1;

        if(val.getSize() >= this.getSize()){
            bigger = val.getData();
            smaller = this.getData();

            dStore = new int[val.getSize()+2];

            if(val.getSign() == 1){
                tempSign = 1;
            }else{
                tempSign = -1;
            }

        }else{

            bigger = this.getData();
            smaller = val.getData();

            dStore = new int[this.getSize()+2];

            if(this.getSign() == 1){
                tempSign = 1;
            }else{
                tempSign = -1;
            }
        }


        for(int i=0;i<smaller.length;i++){
            if((bigger[i] < 0 && smaller[i] < 0) || (bigger[i] >= 0 && smaller[i] >= 0)){
                dStore[i] = Math.abs(bigger[i]) + Math.abs(smaller[i]) + carryOver;
            }else if((bigger[i] <= 0 || smaller[i] <= 0) && (bigger[i] > 0 || smaller[i] > 0)){
                dStore[i] = bigger[i] + smaller[i];
                dStore[i] = Math.abs(dStore[i]);
            }
            if(dStore[i] >= 10){
                dStore[i] = dStore[i] - 10;
                if(i == smaller.length - 1){
                    dStore[i+1] = 1;
                }
                carryOver = 1;
            }else{
                carryOver = 0;
            }
        }

        for(int i = smaller.length;i<bigger.length;i++){
            dStore[i] = bigger[i];
        }

        BigInt rVal = new BigInt(dStore);
        rVal.setSign(tempSign);

        return rVal;

I have been told I have to write a BigInteger class, I know there is one, but I have to write my own. I am to take either ints or a string and turn them into arrays to store them. From there I am to then allow adding, subtract, and multiplying of the numbers. I have it taking the ints and the string and making the arrays that was fine. I am having issues with the rest.

For the add, I have tried to make something that checks the size of the type arrays of numbers, and then sets which is smaller and bigger. From there I have it looping till it gets to the end of the smaller one, and as it loops it takes the digit at that part of the array for the two numbers and adds them. Now this is ok till they are greater then 10, in which case I need to carryover a number. I think I had that working at a point too.

Keep in mind the two things my BigInt has is the array of the number and an int for the sign, 1 or -1.

So in this case I am having issues with it adding right and the sign being right. Same with subtracting.

As for multiplying, I am completely lost on that, and haven't even tried. Below is some of the code I have tried making: ( the add function), PLEASE HELP ME.

public BigInt add(BigInt val){

        int[] bigger;
        int[] smaller;
        int[] dStore;
        int carryOver = 0;
        int tempSign = 1;

        if(val.getSize() >= this.getSize()){
            bigger = val.getData();
            smaller = this.getData();

            dStore = new int[val.getSize()+2];

            if(val.getSign() == 1){
                tempSign = 1;
            }else{
                tempSign = -1;
            }

        }else{

            bigger = this.getData();
            smaller = val.getData();

            dStore = new int[this.getSize()+2];

            if(this.getSign() == 1){
                tempSign = 1;
            }else{
                tempSign = -1;
            }
        }


        for(int i=0;i<smaller.length;i++){
            if((bigger[i] < 0 && smaller[i] < 0) || (bigger[i] >= 0 && smaller[i] >= 0)){
                dStore[i] = Math.abs(bigger[i]) + Math.abs(smaller[i]) + carryOver;
            }else if((bigger[i] <= 0 || smaller[i] <= 0) && (bigger[i] > 0 || smaller[i] > 0)){
                dStore[i] = bigger[i] + smaller[i];
                dStore[i] = Math.abs(dStore[i]);
            }
            if(dStore[i] >= 10){
                dStore[i] = dStore[i] - 10;
                if(i == smaller.length - 1){
                    dStore[i+1] = 1;
                }
                carryOver = 1;
            }else{
                carryOver = 0;
            }
        }

        for(int i = smaller.length;i<bigger.length;i++){
            dStore[i] = bigger[i];
        }

        BigInt rVal = new BigInt(dStore);
        rVal.setSign(tempSign);

        return rVal;

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

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

发布评论

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

评论(4

莫言歌 2024-10-24 15:10:08

如果您知道如何添加手动相乘大数,在 Java 中实现这些算法并不困难。

if you know how to add and multiply big numbers by hand, implementing those algorithms in Java won't be difficult.

月下伊人醉 2024-10-24 15:10:08

如果它们的符号不同,您需要实际减去数字(如果合适的话还可以借用)。另外,您的进位函数似乎无法超出较小数字的长度(所进位的“1”被覆盖)。

为了进一步了解符号,您有几种不同的情况(假设这些情况下这是正数,而 val 是负数):

  1. 如果它有更多数字,那么您需要从中减去 val,结果将为正数
  2. 如果 val 有更多的位数,那么你需要从 val 中减去它,结果将为负
  3. 如果它们有相同的位数,你必须扫描找出哪个更大(从最高有效位开始)。

当然,如果两者都是正数,那么您只需按正常方式相加,如果两者都是负数,则相加,然后将结果设置为负数。

If their signs differ, you'll need to actually subtract the digits (and borrow if appropriate). Also, it looks like your carry function doesn't work to carry past the length of the smaller number (the carried "1" gets overwritten).

To go further into signs, you have a few different cases (assume that this is positive and val is negative for these cases):

  1. If this has more digits, then you'll want to subtract val from this, and the result will be positive
  2. If val has more digits, then you'll want to subtract this from val, and the result will be negative
  3. If they have the same number of digits, you'll have to scan to find which is larger (start at the most significant digit).

Of course if both are positive then you just add as normal, and if both are negative you add, then set the result to be negative.

想你只要分分秒秒 2024-10-24 15:10:08

现在我们知道数字是反向存储的......
我认为如果数字具有相同的符号,您的代码就可以工作。我尝试了以下测试用例:

  • 相同的长度,非常基本的测试。
  • 长度相同,中间有残留。
  • 长度相同,最后结转。
  • 长度相同,中间和末端结转
  • 第一个数字较长,中间和末端结转
  • 第二个数字较长,中间和末端结转
  • 均为负数,第一个数字较长,中间和末端结转最后

一切都很顺利。
但是,当一个为正,一个为负时,就不能正常工作了。
这并不奇怪,因为 -1 + 7 实际上更像是减法而不是加法。您应该将其视为 7-1,如果您检查这种情况并调用减法,对您来说会容易得多。
同样,-1 - 1 应被视为加法,即使它看起来像减法。

Now that we know the numbers are stored in reverse...
I think your code works if the numbers both have the same sign. I tried the following test cases:

  • Same length, really basic test.
  • Same length, carryover in the middle.
  • Same length, carryover at the end.
  • Same length, carryover in the middle and at the end
  • First number is longer, carryover in the middle and at the end
  • Second number is longer, carryover in the middle and at the end
  • Both negative, first number is longer, carryover in the middle and at the end

This all worked out just fine.
However, when one is positive and one is negative, it doesn't work properly.
This isn't too surprising, because -1 + 7 is actually more like subtraction than addition. You should think of it as 7-1, and it'll be much easier for you if you check for this case and instead call subtraction.
Likewise, -1 - 1 should be considered addition, even though it looks like subtraction.

陌生 2024-10-24 15:10:08

实际上,几年前我已经用汇编编写了一个大数字库;如果有帮助的话,我可以在这里添加乘法代码。我给您的建议是不要尝试自己编写函数。已经有一些已知的方法可以对大数进行加、减、乘、除、powmod、xgcd 等运算。我记得我正在阅读 Bruce Schneier 的《应用密码学》一书和 Randall Hyde 的《装配艺术》。两者都有所需的算法来做到这一点(也在伪代码中)。我强烈建议您看一下,尤其是第二个,因为它是在线免费资源。

I've actually written a big numbers library in assembly some years ago; i can add the multiplication code here if that helps. My advice to you is not try to write the functions on your own. There are already known ways to add, substract, multiply, divide, powmod, xgcd and more with bignumbers. I remember that i was reading Bruce Schneier's Applied Cryptography book to do that and The Art of Assembly by Randall Hyde. Both have the needed algorithms to do that (in pseudocode also). I would highly advice that you take a look, especially to the second one that it's an online free resource.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文