~cpp 1234 + 567 ---- 1081
~cpp package primaryarithmetic; import junit.framework.TestCase; public class NumberGeneratorTest extends TestCase { public void testNoNumber() { NumberGenerator ng = new NumberGenerator(); assertFalse( ng.hasNext() ); } public void test123() { NumberGenerator ng = new NumberGenerator(123); assertEquals( 3, ng.next() ); assertEquals( 2, ng.next() ); assertTrue( ng.hasNext() ); assertEquals( 1, ng.next() ); assertFalse( ng.hasNext() ); } }
~cpp testNoNumber
는 필요없는것 같다. 나중에 글을 쓰다보니, 같이 쓰게 됬는데 원래는 위의 테스트를 먼저 작성하고 테스트 통과후 아래쪽 테스트를 추가했다. 이번 작업과 별도로 코딩후에 뭔가하자는 결국 놓치는게 많다는걸 다시한번 증명하게 된다. ~cpp package primaryarithmetic; public class NumberGenerator { private int number; private byte[] numbers; private int numPointer; public NumberGenerator() { number = -1; } public NumberGenerator( int number ) { if( number < 0 ) throw new ArithmeticException( "0 또는 그 이상의 정수만 가능합니다: " + number ); this.number = number; init(); } private void init() { numbers = String.valueOf(number).getBytes(); numPointer = numbers.length - 1; } public boolean hasNext() { if( number == -1 ) return false; return numPointer >= 0; } public int next() { return numbers[numPointer--] - '0'; } }
~cpp package primaryarithmetic; import junit.framework.TestCase; public class PrimaryArithmeticTest extends TestCase { public void testCases() { int [][] sets = { { 0, 1, 1 }, { 1, 5, 5 }, { 3, 555, 555 }, { 0, 123, 456 }, { 1, 123, 594 } }; for( int i=0; i<sets.length; i++ ) { int expected = sets[i][0]; int num1 = sets[i][1]; int num2 = sets[i][2]; verify( expected, num1, num2 ); } } private void verify(int expected, int num1, int num2) { int result = PrimaryArithmetic.add(num1, num2); try { assertEquals( expected, result ); } catch( Throwable e ) { throw new ArithmeticException( "We expected " + expected + ", but was " + result + " (num1=" + num1 + ", num2=" + num2 + ")" ); } } }
~cpp package primaryarithmetic; public class PrimaryArithmetic { public static int add( int num1, int num2 ) { NumberGenerator ng1 = new NumberGenerator( Math.max(num1, num2) ); NumberGenerator ng2 = new NumberGenerator( Math.min(num1, num2) ); int counts = 0; int sumUp = 0; while( ng1.hasNext() && ng2.hasNext() ) { int n1 = ng1.next(); int n2 = ng2.next(); int sum = n1 + n2 + sumUp; if( sum >= 10 ) { sumUp = 1; counts ++; } else sumUp = 0; } while( sumUp == 1 && ng1.hasNext() ) { int sum = ng1.next() + sumUp; if( sum >= 10 ) counts ++; else break; } return counts; } }
~cpp package primaryarithmetic; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class PrimaryArithmeticApp { public static void main( String [] args ) throws IOException { BufferedReader in = new BufferedReader( new InputStreamReader( System.in )); String line; while( (line=in.readLine()) != null ) { String [] numbers = line.split( " " ); int num1 = Integer.parseInt( numbers[0] ); int num2 = Integer.parseInt( numbers[1] ); if( num1 == 0 && num2 == 0 ) break; int counts = PrimaryArithmetic.add( num1, num2 ); print( counts ); } } private static void print( int counts ) { String occurs = (counts == 0) ? "No" : String.valueOf(counts); String postfix = (counts > 1) ? "s" : ""; System.out.println( occurs + " carry operation" + postfix + "." ); } }