public static void formatMult(BigNumber b1, BigNumber b2, BigNumber[] res) { int maxInput = Math.max(b1.bigNumber.length, b2.bigNumber.length + 1); int maxLength = Math.max(maxInput, res[res.length - 1].bigNumber.length); StringBuffer toFill = new StringBuffer(); for (int i = 0; i < maxLength - b1.bigNumber.length; i++) { toFill.append(" "); } System.out.println(toFill + b1.toString()); toFill = new StringBuffer(); for (int i = 0; i < maxLength - (b2.bigNumber.length + 1); i++) toFill.append(" "); System.out.println(toFill + "*" + b2.toString()); toFill = new StringBuffer(); if (b2.bigNumber.length != 1) { for (int i = 0; i < maxLength - maxInput; i++) toFill.append(" "); for (int i = 0; i < maxInput; i++) toFill.append("-"); } else { for (int i = 0; i < maxLength; i++) toFill.append("-"); } System.out.println(toFill); for (int i = 0; i < res.length - 1; i++) { toFill = new StringBuffer(); for (int j = 0; j < maxLength - (res[i].bigNumber.length + i); j++) toFill.append(" "); System.out.println(toFill + res[i].toString()); } if (b2.bigNumber.length == 1) return; toFill = new StringBuffer(); for (int i = 0; i < maxLength; i++) toFill.append("-"); System.out.println(toFill); toFill = new StringBuffer(); for (int i = 0; i < maxLength - res[res.length - 1].bigNumber.length; i++) toFill.append(" "); System.out.println(toFill + res[res.length - 1].toString()); // lastMult.size()+Math.max(b1.bigNumber.length-1, b2.bigNumber.length-1); }
public static void formatOp(BigNumber b1, BigNumber b2, BigNumber res, String op) { int maxLength = Math.max(b1.bigNumber.length, Math.max(b2.bigNumber.length + 1, res.bigNumber.length)); StringBuffer toFill = new StringBuffer(); for (int i = 0; i < maxLength - b1.bigNumber.length; i++) { toFill.append(" "); } System.out.println(toFill + b1.toString()); toFill = new StringBuffer(); for (int i = 0; i < maxLength - (b2.bigNumber.length + 1); i++) { toFill.append(" "); } System.out.println(toFill + op + b2.toString()); toFill = new StringBuffer(); for (int i = 0; i < maxLength; i++) { toFill.append("-"); } System.out.println(toFill); toFill = new StringBuffer(); for (int i = 0; i < maxLength - res.bigNumber.length; i++) { toFill.append(" "); } System.out.println(toFill + res.toString()); }
private static void handleInput() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = null; int testCases = -1; while (null != (line = br.readLine())) { if (testCases == -1) { testCases = Integer.parseInt(line); continue; } BigNumber b1 = null; BigNumber b2 = null; BigNumber res = null; BigNumber[] resArr = null; if (line.split("-").length > 1) { String[] substr = line.split("-"); b1 = new BigNumber(substr[0]); b2 = new BigNumber(substr[1]); res = b1.subtract(b2); BigNumber.formatOp(b1, b2, res, "-"); } else if (line.split("\\*").length > 1) { String[] mult = line.split("\\*"); b1 = new BigNumber(mult[0]); b2 = new BigNumber(mult[1]); resArr = b1.multiply(b2); BigNumber.formatMult(b1, b2, resArr); } else if (line.split("\\+").length > 1) { String[] add = line.split("\\+"); b1 = new BigNumber(add[0]); b2 = new BigNumber(add[1]); res = b1.add(b2); BigNumber.formatOp(b1, b2, res, "+"); } System.out.println(); if (--testCases <= 0) break; } }
private static void testOperations() { BigNumber bn1 = null; BigNumber bn2 = null; BigNumber res = null; bn1 = new BigNumber("1"); bn2 = new BigNumber("10"); res = bn1.add(bn2); BigNumber.formatOp(bn1, bn2, res, "+"); System.out.println(); bn1 = new BigNumber("1"); bn2 = new BigNumber("1"); res = bn1.subtract(bn2); BigNumber.formatOp(bn1, bn2, res, "-"); System.out.println(); bn1 = new BigNumber("35"); bn2 = new BigNumber("111111111"); BigNumber[] resArr = bn1.multiply(bn2); BigNumber.formatMult(bn1, bn2, resArr); }