Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. public static InfiniteInt add(InfiniteInt inf1, InfiniteInt inf2)
  2.     {
  3.         InfiniteInt solution = new InfiniteInt();
  4.        
  5.         int carry = 0;
  6.         int digit;
  7.         int count = 1;
  8.         String intConcatenate = "";
  9.        
  10.         DLLNode<Integer> cursor1 = inf1.tail;
  11.         DLLNode<Integer> cursor2 = inf2.tail;
  12.         DLLNode<Integer> cursor3 = solution.tail;
  13.        
  14.         int n1 = cursor1.data;
  15.         int n2 = cursor2.data;
  16.        
  17.         //case1 - infInts are same size
  18.        
  19.         if(inf1.size() == inf2.size())
  20.         {
  21.             while(cursor1 != null)
  22.             {
  23.                
  24.                 while(count <= 3)
  25.                 {
  26.                     //if the sum of digits/carry is less than 10, digit is sum of 2 numbers
  27.                     if(((n1%10)+(n2%10) + carry) < 10)
  28.                     {
  29.                         digit = ((n1%10)+(n2%10) + carry);
  30.                     }
  31.                     else //we know the sum of the 2 and carry is greater than 9
  32.                     {
  33.                         digit = ((n1%10)+(n2%10) + carry) % 10;
  34.                         carry = ((n1%10)+(n2%10) + carry) / 10;
  35.                     }
  36.                    
  37.                     intConcatenate = digit + intConcatenate; //Concatenate a string of ints to be parsed into the node as an int
  38.                     count++;
  39.                     n1 = n1 / 10; //discard the last digits
  40.                     n2 = n2 / 10;
  41.                    
  42.                 }
  43.                 cursor3.data = Integer.parseInt(intConcatenate);
  44.                 cursor1 = cursor1.prev;
  45.                 cursor2 = cursor2.prev;
  46.                 cursor3 = cursor3.prev;
  47.                 count = 0; //reset count
  48.             }
  49.         }
  50.         //case2 - inf1 is larger
  51.        
  52.         //case3 - inf1 is smaller
  53.         return solution;
  54.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement