Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static InfiniteInt add(InfiniteInt inf1, InfiniteInt inf2)
- {
- InfiniteInt solution = new InfiniteInt();
- int carry = 0;
- int digit;
- int count = 1;
- String intConcatenate = "";
- DLLNode<Integer> cursor1 = inf1.tail;
- DLLNode<Integer> cursor2 = inf2.tail;
- DLLNode<Integer> cursor3 = solution.tail;
- int n1 = cursor1.data;
- int n2 = cursor2.data;
- //case1 - infInts are same size
- if(inf1.size() == inf2.size())
- {
- while(cursor1 != null)
- {
- while(count <= 3)
- {
- //if the sum of digits/carry is less than 10, digit is sum of 2 numbers
- if(((n1%10)+(n2%10) + carry) < 10)
- {
- digit = ((n1%10)+(n2%10) + carry);
- }
- else //we know the sum of the 2 and carry is greater than 9
- {
- digit = ((n1%10)+(n2%10) + carry) % 10;
- carry = ((n1%10)+(n2%10) + carry) / 10;
- }
- intConcatenate = digit + intConcatenate; //Concatenate a string of ints to be parsed into the node as an int
- count++;
- n1 = n1 / 10; //discard the last digits
- n2 = n2 / 10;
- }
- cursor3.data = Integer.parseInt(intConcatenate);
- cursor1 = cursor1.prev;
- cursor2 = cursor2.prev;
- cursor3 = cursor3.prev;
- count = 0; //reset count
- }
- }
- //case2 - inf1 is larger
- //case3 - inf1 is smaller
- return solution;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement