Created
October 16, 2013 20:33
-
-
Save film42/7014348 to your computer and use it in GitHub Desktop.
Use an array of any size to add any two numbers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Counter { | |
private int[] seq = null; | |
public Counter(int size) { | |
this.seq = new int[size]; | |
} | |
public Counter() { | |
this.seq = new int[10]; | |
} | |
public int[] numToArray(int n) { | |
// Convert to string | |
String number = Integer.toString(n); | |
// Init a new array at length | |
int[] arr = new int[number.length()]; | |
// Set the values | |
for(int i = 0; i < number.length(); i++) { | |
arr[i] = number.toCharArray()[i]-48; | |
} | |
return arr; | |
} | |
private void carryOvers() { | |
int carryOver = 0; | |
for(int i = (seq.length -1); i > 0; i--) { | |
seq[i] += carryOver; | |
if(seq[i] > 9) { | |
carryOver = ((seq[i] - (seq[i] % 10)) / 10); | |
seq[i] = seq[i] - 10; | |
} else { | |
carryOver = 0; | |
} | |
} | |
} | |
public void add(int n) { | |
int[] number = numToArray(n); | |
int offset = seq.length - number.length; | |
for(int i = (seq.length - 1); i > 0; i--) { | |
seq[i] += number[i-offset]; | |
if(0 == (i - offset)) break; | |
} | |
carryOvers(); | |
} | |
public String toString() { | |
StringBuilder sb = new StringBuilder(); | |
for(int n : seq) { | |
sb.append(n); | |
} | |
return sb.toString(); | |
} | |
public int toValue() { | |
return Integer.parseInt(this.toString()); | |
} | |
public static void main (String[] args) { | |
// | |
// Example | |
// | |
Counter counter = new Counter(); | |
counter.add(236); | |
counter.add(66); | |
counter.add(10000); | |
counter.add(10000); | |
counter.add(10000); | |
counter.add(10000); | |
System.out.println(counter.toString()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Before; | |
import org.junit.Test; | |
import static org.junit.Assert.*; | |
public class CounterTest { | |
private Counter counter; | |
@Before | |
public void setUp() throws Exception { | |
counter = new Counter(10); | |
} | |
@Test | |
public void testNumToArray() throws Exception { | |
int[] arr = new int[] {1, 2, 3}; | |
assertArrayEquals(arr, counter.numToArray(123)); | |
arr = new int[] {1}; | |
assertArrayEquals(arr, counter.numToArray(1)); | |
arr = new int[] {1,2,3,4,5,6,7,8,9}; | |
assertArrayEquals(arr, counter.numToArray(123456789)); | |
} | |
@Test | |
public void testAdd() throws Exception { | |
counter.add(10); | |
assertEquals(counter.toValue(), 10); | |
counter.add(230); | |
assertEquals(counter.toValue(), 240); | |
counter.add(9); | |
assertEquals(counter.toValue(), 249); | |
counter.add(9); | |
assertEquals(counter.toValue(), 258); | |
counter.add(10000); | |
assertEquals(counter.toValue(), 10258); | |
} | |
@Test | |
public void testToString() throws Exception { | |
counter.add(10); | |
assertEquals(counter.toString(), "0000000010"); | |
counter.add(230); | |
assertEquals(counter.toString(), "0000000240"); | |
counter.add(9); | |
assertEquals(counter.toString(), "0000000249"); | |
counter.add(9); | |
assertEquals(counter.toString(), "0000000258"); | |
counter.add(10000); | |
assertEquals(counter.toString(), "0000010258"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It actually sucks, but I had an hour to burn, so why not?