Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Will Maxwell
- //wjmaxwel
- //estimated time: 2 hours
- //actual time: 3 hours
- //package program4; //package for netbeans
- import java.util.Scanner;
- import java.lang.Integer;
- import java.lang.Character;
- public class Program4
- {
- public static void main(String[] args)
- {
- Scanner keyboard = new Scanner(System.in);
- char[] arrayOne, arrayTwo, reverseArrayOne, reverseArrayTwo, arrayOneWithZeroes, arrayTwoWithZeroes, sum;
- String stringOne, stringTwo;
- int arrayLengthOne, arrayLengthTwo, maxLength;
- System.out.print("First number: ");
- stringOne = keyboard.nextLine();
- System.out.print("Second number: ");
- stringTwo = keyboard.nextLine();
- arrayOne = stringOne.toCharArray();
- arrayTwo = stringTwo.toCharArray();
- arrayLengthOne = stringOne.length();
- arrayLengthTwo = stringTwo.length();
- maxLength = max(arrayLengthOne, arrayLengthTwo);
- arrayOneWithZeroes = zeroes(arrayOne, arrayLengthOne);
- arrayTwoWithZeroes = zeroes(arrayTwo, arrayLengthTwo);
- reverseArrayOne = reverse(arrayOneWithZeroes, arrayLengthOne);
- reverseArrayTwo = reverse(arrayTwoWithZeroes, arrayLengthTwo);
- sum = add(arrayOneWithZeroes, arrayTwoWithZeroes, arrayLengthOne, arrayLengthTwo);
- sum = unzero(sum, maxLength);
- sum = reverse(sum, sum.length);
- System.out.print("Sum: ");
- System.out.println(sum);
- }
- public static char[] reverse(char[] array, int arrayLength) //reverse the array so addition algorithm is easier
- {
- int left = 0;
- int right = arrayLength - 1;
- while(left < right)
- {
- char temp = array[left];
- array[left] = array[right];
- array[right] = temp;
- left++;
- right--;
- }
- return array;
- }
- public static char[] add(char[] arrayOne, char[] arrayTwo, int lengthOne, int lengthTwo)
- {
- char[] sum = new char[20];
- int firstNum, secondNum, smallSum;
- boolean carry = false;
- for(int i = 0; i < 20; i++)
- {
- firstNum = (Integer.valueOf(arrayOne[i]) - 48); //48 is the offset of the ASCII value
- secondNum = (Integer.valueOf(arrayTwo[i]) - 48);
- smallSum = firstNum + secondNum;
- if(carry == true)
- {
- smallSum++;
- }
- carry = false;
- if(smallSum >= 10)
- {
- smallSum = smallSum%10;
- carry = true;
- }
- sum[i] = Character.forDigit(smallSum, 10);
- }
- return sum;
- }
- public static char[] zeroes(char[] array, int length) //adds zeroes to the empty spots in the array to make addition easier.
- {
- char[] arrayWithZeroes = new char[20];
- for(int i = 0; i < 20; i++)
- {
- if(i >= length)
- {
- arrayWithZeroes[i] = '0';
- }
- else
- {
- arrayWithZeroes[i] = array[i];
- }
- }
- return arrayWithZeroes;
- }
- public static char[] unzero(char[] array, int length) //takes off the padded zeroes
- {
- char[] unzero = new char[20];
- for(int i = 0; i < 20; i++)
- {
- if(i < length)
- {
- unzero[i] = array[i];
- }
- }
- if(array[length] != '0')
- {
- unzero[length] = array[length];
- }
- return unzero;
- }
- public static int max(int a, int b)
- {
- if(a > b)
- {
- return a;
- }
- else
- {
- return b;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement