Minus
by Isai Damier, Android Engineer @ Google

/*******************************************************************
 * Author: Isai Damier
 * Title: LargeInteger.java
 * Project: geekviewpoint
 * Package: datastructure
 *
 * Description: Normally modern computers cannot handle very large
 *    numbers. So the task here is to create a datastructure that
 *    can handle numbers of perhaps infinite size. And to prove that
 *    the datastructure works, included are arithmetic operators.
 *
 *    Naturally the numbers are represented as Strings. And to
 *    perform addition and subtraction, stacks are used as the media.
 *    Finally, as any trusty calculator, this datastructure returns
 *    erroneous values for erroneous inputs and correct values for
 *    correct inputs. This is to say, if an input string does not
 *    represent a valid decimal integer, then the output string
 *    will be bogus.
 ******************************************************************/ 
 import java.util.Stack;

public class LargeInteger {

  private String number;

  public LargeInteger(String number) {
    this.number = number;
  }//constructor

  /***************************************************************
   * Function: minus
   * Inputs: @param subtrahend
   *         The value to be subtracted from this LargeInteger
   * Output: String
   *         The difference of the subtraction
   *
   * Description: Return a string representation of the difference
   *    between this LargeInteger and the given subtrahend.
   *
   * Technical Details: Subtraction is easy once the larger number
   *    is on top. Hence this algorithm progresses by first
   *    checking which number is larger and then placing that
   *    number on top. The second step performs the subtraction as
   *    with a pencil. The third step adds a minus if the numbers
   *    were flipped in step one.
   *
   *    The result is assembled in reverse just to keep things
   *    simple (Java has a nice append function). The result could
   *    have otherwise been assembled as result =
   *    difference.concat(result);
   *
   **************************************************************/
  public String minus(String subtrahend) {
    Stack<Integer> minuendStack, subtrahendStack;
    boolean flipped = isSmaller(subtrahend);
    if (flipped) {
      subtrahendStack = toIntegerStack(this.number);
      minuendStack = toIntegerStack(subtrahend);
    } else {
      minuendStack = toIntegerStack(this.number);
      subtrahendStack = toIntegerStack(subtrahend);
    }

    StringBuffer result = new StringBuffer();
    int carry = 0, difference = 0, radix = 10;

    while (!minuendStack.isEmpty() && !subtrahendStack.isEmpty()) {
      difference =
              minuendStack.pop() - subtrahendStack.pop() - carry;
      if (0 > difference) {
        carry = 1;
        difference = radix + difference;
      } else {
        carry = 0;
      }
      result.append(difference);
    }

    while (!minuendStack.isEmpty()) {
      difference = minuendStack.pop() - carry;
      if (0 > difference) {
        carry = 1;
        difference = radix + difference;
      } else {
        carry = 0;
      }
      result.append(difference);
    }

    if (flipped) {
      result.append("-");
    }
    result.reverse();
    return result.toString();
  }
}
import org.junit.Test;
import static org.junit.Assert.*;

public class LargeIntegerTest {

  /**
   * Test of plus method, of class LargeInteger.
   */
  @Test
  public void testPlus() {
    System.out.println("plus");
    LargeInteger bigInteger;

    String a = "8787284297498234897287345";
    String b = "7971813897918381831891879249579839712938";
    String p = "7971813897918390619176176747814737000283";
    bigInteger = new LargeInteger(a);
    assertEquals(p, bigInteger.plus(b));

    int max = 54321;
    for (int x = 0; x <= max; x += 31) {
      for (int y = 0; y <= max; y += 31) {
        bigInteger = new LargeInteger(x + "");
        assertEquals(x + y, Integer.parseInt(bigInteger.plus(y + "")));
        assertEquals(x + "", bigInteger.value());
        assertEquals((x + "").length(), bigInteger.size());
      }
    }
  }
}