Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.76 KB | None | 0 0
  1. //Will Maxwell
  2. //wjmaxwel
  3. //estimated time: 2 hours
  4. //actual time: 3 hours
  5.  
  6. //package program4; //package for netbeans
  7. import java.util.Scanner;
  8. import java.lang.Integer;
  9. import java.lang.Character;
  10.  
  11. public class Program4
  12. {
  13.     public static void main(String[] args)
  14.     {
  15.         Scanner keyboard = new Scanner(System.in);
  16.         char[] arrayOne, arrayTwo, reverseArrayOne, reverseArrayTwo, arrayOneWithZeroes, arrayTwoWithZeroes, sum;
  17.         String stringOne, stringTwo;
  18.         int arrayLengthOne, arrayLengthTwo, maxLength;
  19.  
  20.         System.out.print("First number: ");
  21.         stringOne = keyboard.nextLine();
  22.         System.out.print("Second number: ");
  23.         stringTwo = keyboard.nextLine();
  24.  
  25.         arrayOne = stringOne.toCharArray();
  26.         arrayTwo = stringTwo.toCharArray();
  27.  
  28.         arrayLengthOne = stringOne.length();
  29.         arrayLengthTwo = stringTwo.length();
  30.         maxLength = max(arrayLengthOne, arrayLengthTwo);
  31.  
  32.         arrayOneWithZeroes = zeroes(arrayOne, arrayLengthOne);
  33.         arrayTwoWithZeroes = zeroes(arrayTwo, arrayLengthTwo);
  34.  
  35.         reverseArrayOne = reverse(arrayOneWithZeroes, arrayLengthOne);
  36.         reverseArrayTwo = reverse(arrayTwoWithZeroes, arrayLengthTwo);
  37.  
  38.         sum = add(arrayOneWithZeroes, arrayTwoWithZeroes, arrayLengthOne, arrayLengthTwo);
  39.         sum = unzero(sum, maxLength);
  40.         sum = reverse(sum, sum.length);
  41.         System.out.print("Sum: ");
  42.         System.out.println(sum);
  43.  
  44.  
  45.  
  46.     }
  47.  
  48.     public static char[] reverse(char[] array, int arrayLength) //reverse the array so addition algorithm is easier
  49.     {
  50.         int left = 0;
  51.         int right = arrayLength - 1;
  52.  
  53.         while(left < right)
  54.         {
  55.             char temp = array[left];
  56.             array[left] = array[right];
  57.             array[right] = temp;
  58.             left++;
  59.             right--;
  60.         }
  61.        
  62.         return array;
  63.  
  64.     }
  65.  
  66.     public static char[] add(char[] arrayOne, char[] arrayTwo, int lengthOne, int lengthTwo)
  67.     {
  68.         char[] sum = new char[20];
  69.         int firstNum, secondNum, smallSum;
  70.         boolean carry = false;
  71.  
  72.         for(int i = 0; i < 20; i++)
  73.         {
  74.             firstNum = (Integer.valueOf(arrayOne[i]) - 48); //48 is the offset of the ASCII value
  75.             secondNum = (Integer.valueOf(arrayTwo[i]) - 48);
  76.             smallSum = firstNum + secondNum;
  77.             if(carry == true)
  78.             {
  79.                 smallSum++;
  80.             }
  81.             carry = false;
  82.             if(smallSum >= 10)
  83.             {
  84.                 smallSum = smallSum%10;
  85.                 carry = true;
  86.             }
  87.             sum[i] = Character.forDigit(smallSum, 10);
  88.         }
  89.  
  90.         return sum;
  91.     }
  92.  
  93.     public static char[] zeroes(char[] array, int length) //adds zeroes to the empty spots in the array to make addition easier.
  94.     {
  95.         char[] arrayWithZeroes = new char[20];
  96.         for(int i = 0; i < 20; i++)
  97.         {
  98.             if(i >= length)
  99.             {
  100.                 arrayWithZeroes[i] = '0';
  101.             }
  102.             else
  103.             {
  104.                 arrayWithZeroes[i] = array[i];
  105.             }
  106.         }
  107.  
  108.         return arrayWithZeroes;
  109.     }
  110.  
  111.     public static char[] unzero(char[] array, int length) //takes off the padded zeroes
  112.     {
  113.         char[] unzero = new char[20];
  114.         for(int i = 0; i < 20; i++)
  115.         {
  116.             if(i < length)
  117.             {
  118.                 unzero[i] = array[i];
  119.             }
  120.         }
  121.         if(array[length] != '0')
  122.         {
  123.             unzero[length] = array[length];
  124.         }
  125.         return unzero;
  126.     }
  127.  
  128.     public static int max(int a, int b)
  129.     {
  130.         if(a > b)
  131.         {
  132.             return a;
  133.         }
  134.         else
  135.         {
  136.             return b;
  137.         }
  138.     }
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement