BigInteger Homework Help Needed
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])开发者_StackOverflow + 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;
if you know how to add and multiply big numbers by hand, implementing those algorithms in Java won't be difficult.
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):
- If this has more digits, then you'll want to subtract val from this, and the result will be positive
- If val has more digits, then you'll want to subtract this from val, and the result will be negative
- 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.
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.
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.
精彩评论