From 76f45673468b140a26bacb84a12d56bc62030f9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Christian=20Gr=C3=BCnhage?= Date: Wed, 13 Jan 2016 18:48:57 +0100 Subject: [PATCH] Initial commit --- .idea/compiler.xml | 22 ++ .idea/copyright/profiles_settings.xml | 3 + .idea/description.html | 1 + .idea/misc.xml | 12 + .idea/modules.xml | 8 + .idea/project-template.xml | 3 + .idea/uiDesigner.xml | 124 +++++++ .idea/vcs.xml | 6 + Multiplication.iml | 12 + .../FeatureNotImplementedYetException.java | 11 + src/de/janchristiangruenhage/math/Number.java | 166 ++++++++++ .../math/addition/Addition.java | 304 ++++++++++++++++++ .../multiplication/LongMultiplication.java | 63 ++++ .../math/multiplication/Main.java | 13 + .../math/multiplication/Multiplication.java | 140 ++++++++ .../util/MultiOutput.java | 41 +++ 16 files changed, 929 insertions(+) create mode 100644 .idea/compiler.xml create mode 100644 .idea/copyright/profiles_settings.xml create mode 100644 .idea/description.html create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/project-template.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 Multiplication.iml create mode 100644 src/de/janchristiangruenhage/exceptions/FeatureNotImplementedYetException.java create mode 100644 src/de/janchristiangruenhage/math/Number.java create mode 100644 src/de/janchristiangruenhage/math/addition/Addition.java create mode 100644 src/de/janchristiangruenhage/math/multiplication/LongMultiplication.java create mode 100644 src/de/janchristiangruenhage/math/multiplication/Main.java create mode 100644 src/de/janchristiangruenhage/math/multiplication/Multiplication.java create mode 100644 src/de/janchristiangruenhage/util/MultiOutput.java diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/description.html b/.idea/description.html new file mode 100644 index 0000000..db5f129 --- /dev/null +++ b/.idea/description.html @@ -0,0 +1 @@ +Simple Java application that includes a class with main() method \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..e29b6f0 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..03e7a62 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/project-template.xml b/.idea/project-template.xml new file mode 100644 index 0000000..1f08b88 --- /dev/null +++ b/.idea/project-template.xml @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..6564d52 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Multiplication.iml b/Multiplication.iml new file mode 100644 index 0000000..d5c0743 --- /dev/null +++ b/Multiplication.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/src/de/janchristiangruenhage/exceptions/FeatureNotImplementedYetException.java b/src/de/janchristiangruenhage/exceptions/FeatureNotImplementedYetException.java new file mode 100644 index 0000000..d8a329a --- /dev/null +++ b/src/de/janchristiangruenhage/exceptions/FeatureNotImplementedYetException.java @@ -0,0 +1,11 @@ +package de.janchristiangruenhage.exceptions; + +public class FeatureNotImplementedYetException extends RuntimeException { + public FeatureNotImplementedYetException() { + super(); + } + + public FeatureNotImplementedYetException(String message) { + super(message); + } +} diff --git a/src/de/janchristiangruenhage/math/Number.java b/src/de/janchristiangruenhage/math/Number.java new file mode 100644 index 0000000..854da94 --- /dev/null +++ b/src/de/janchristiangruenhage/math/Number.java @@ -0,0 +1,166 @@ +package de.janchristiangruenhage.math; + +/** + * The class Number represents a one digit number and a multiple digit carry + *

+ * Examples: The Number representation of: + * 12 would be 2 carry 1 + * 123 would be 3 carry 12 + * 5351 would be 1 carry 535 + *

+ * and so on. + */ +public class Number { + private int number; + private int carry; + + /** + * Constructs a new Number using a number and a carry. + *

+ * if the number has more than one digit, it will be split + * and the carry will be enlarged accordingly. + * + * @param number the number + * @param carry the carry + */ + public Number(int number, int carry) { + this.number = intToNumber(number).getNumber(); + this.carry = carry + intToNumber(number).getCarry(); + } + + private Number(int number, int carry, boolean calculateNumber) { + if (calculateNumber) { + this.number = intToNumber(number).getNumber(); + this.carry = carry + intToNumber(number).getCarry(); + } else { + this.number = number; + this.carry = carry; + } + } + + /** + * Constructs a new Number using an int only. + *

+ * The lowest value digit will be the number, the rest will be the carry + * + * @param number the int input + */ + public Number(int number) { + this.number = intToNumber(number).getNumber(); + this.carry = intToNumber(number).getCarry(); + } + + /** + * Adds a number to this number. + * + * @param number the number to be added + */ + public void add(Number number) { + Number tmp = add(this, number); + this.number = tmp.getNumber(); + this.carry = tmp.getCarry(); + } + + /** + * Parses a number from a char. + * + * @param input the char input + * @return a number that represents this char + */ + public static Number parseNumber(char input) { + switch (input) { + case '0': + return new Number(0); + case '1': + return new Number(1); + case '2': + return new Number(2); + case '3': + return new Number(3); + case '4': + return new Number(4); + case '5': + return new Number(5); + case '6': + return new Number(6); + case '7': + return new Number(7); + case '8': + return new Number(8); + case '9': + return new Number(9); + default: + return new Number(0); + } + + } + + /** + * Getter for the number variable. + * + * @return the number variable + */ + public int getNumber() { + return number; + } + + /** + * Getter for the carry variable. + * + * @return the carry variable + */ + public int getCarry() { + return carry; + } + + /** + * Multiplies two numbers without a carry and the carry to the result num. + * + * @param factorOne first factor + * @param factorTwo second factor + * @param carry carry + * @return (new Number(factorOne * factorTwo + carry)) + */ + public static Number multiply(Number factorOne, Number factorTwo, int carry) { + int result = (factorOne.getNumber() * factorTwo.getNumber()) + carry; + return intToNumber(result); + } + + /** + * Adds up two numbers and their carries + * + * @param addendOne first addend + * @param addendTwo second addend + * @return the resulting number + */ + public static Number add(Number addendOne, Number addendTwo) { + int result = addendOne.getNumber() + + addendTwo.getNumber() + + addendOne.getCarry() * 10 + + addendTwo.getCarry() * 10; + //The carries have to be multiplied by ten, + // since they are ten times as weighted as the normal numbers + return intToNumber(result); + } + + private static Number intToNumber(int input) { + Number result; + int number = input % 10; + //Gets the first digit + int carry = (input - number) / 10; + //removes the first digit from the input and assign that to the carry + result = new Number(number, carry, false); + //private constructor as only way to instantiate + // the Number with the ability to directly set the number and carry + // without them getting pushed through this method again + // for ensuring that the number is a one digit number. + // If they were pushed through this method again, + // it would result in an endless loop resulting in a Stack Overflow + return result; + } + + @Override + public String toString() { + return "" + number; + } +} diff --git a/src/de/janchristiangruenhage/math/addition/Addition.java b/src/de/janchristiangruenhage/math/addition/Addition.java new file mode 100644 index 0000000..5e7fba0 --- /dev/null +++ b/src/de/janchristiangruenhage/math/addition/Addition.java @@ -0,0 +1,304 @@ +package de.janchristiangruenhage.math.addition; + +import de.janchristiangruenhage.exceptions.FeatureNotImplementedYetException; +import de.janchristiangruenhage.math.Number; + +public class Addition { + private String[] addends; + private int splitLines; + private String result; + private int resultLength; + private boolean cutResult; + private String resultCarry; + private boolean calculated; + + /** + * Constructs a new Addition. + * + * @param addends The Addends + * @param splitLines After how many lines an additional Result will be calculated + * @param resultLength How many digits the result should have, for overflow or a carry + * see cutResult + * @param cutResult If the result should be cut + */ + public Addition(String[] addends, int splitLines, int resultLength, boolean cutResult) { + this.addends = addends; + this.splitLines = splitLines; + this.resultLength = resultLength; + this.cutResult = cutResult; + } + + /** + * Creates an easily readable Addition and returns it. + * + * @return String representation of the Calculation + */ + public String printCalculation() { + if (calculated) { + return result; + } + if (splitLines != 0) { + //decides whether the Addition has to be split before Calculation + throw new FeatureNotImplementedYetException(); + //Throws new Feature not Implemented Yet Exception + //return splitAndPrint(); + //splits and prints the calculation, not working yet + } else { + return print(); + //just prints the calculation + } + } + + /** + * Creates an easily readable Addition and returns it. + * This is used if the Addition doesn't have to be split anymore + * + * @return String representation of the Calculation + */ + private String print() { + int maximum = 0; + //Variable for the Maximum Length of a Addend String + String[] shortenedAddends = new String[addends.length]; + for (int i = 0; i < addends.length; i++) { + shortenedAddends[i] = addends[i].substring(1).trim(); + //The substring for removing the plus symbol at the beginning, the trimming for removing unnecessary spaces + maximum = (shortenedAddends[i].length() > maximum) ? + //Checks whether the length of the i-th String is higher than the maximum variable + shortenedAddends[i].length() : maximum; + //Assigns the maximum the correct value, the string length if its bigger, itself otherwise + } + result = ""; + //Constructs the new String where the result will be saved in + String carries = "0"; + //Constructs the new String where the carries will be saved in + int carry = 0; + //New variable for the carry at the point of the calculation + for (int i = 0; i < maximum; i++) { + AdditionColumn additionColumn = new AdditionColumn(shortenedAddends, carries, carry, i).invoke(); + //Method-Object for calculating a single column of the addition + carries = additionColumn.getCarries(); + //Get the carries string from the Method-Object + carry = additionColumn.getCarry(); + //Get the carry from the Method-Object + } + String retVal = ""; + //Construct the retVal String where the calculation wil be put into + for (String addend : shortenedAddends) { + retVal = addAddend(retVal, addend); + //Adds the addend to the retVal string in a new line + } + for (int i = 0; i < addends[0].length(); i++) { + retVal += " "; + //Adds a line of spaces + } + retVal += "\n"; + //Adds a new line + while (carries.length() > 0 && carries.charAt(0) == '0') { + carries = carries.substring(1, carries.length()); + //removes leading zeroes from the carries string + } + if (cutResult && carries.length() > resultLength) { + carries = "(" + + carries.substring(0, carries.length() - resultLength) + + ")" + + carries.substring(carries.length() - resultLength); + //adds braces around the cut part of the result, if wanted + } + while (carries.length() < addends[0].length() - 1) { + carries = " " + carries; + //elongates the carries string with spaces + } + carries = "+" + carries; + //adds the plus symbol to the carries string + retVal += carries + "\n"; + //adds the carries string to the retVal string in a new line + for (int i = 0; i < addends[0].length(); i++) { + retVal += "_"; + //draws a line + } + retVal += "\n"; + //adds new line + if (cutResult && result.length() > resultLength) { + resultCarry = result.substring(0, result.length() - resultLength); + //cuts the carry from the result + result = "(" + + resultCarry + + ")" + + result.substring(result.length() - resultLength); + //adds braces around the carry + } + while (result.length() <= addends[0].length() - 1) { + result = " " + result; + //elongates the result string + } + retVal += result; + //adds the result to the retVal string + retVal += "\n\n\n\n"; + //adds four new lines after the calculation + calculated = true; + //sets calculated to true, that the result doesn't have to be recalculated if it is needed again + return retVal; + } + + private String addAddend(String retVal, String addend) { + String addendCopy = addend; + while (addendCopy.length() > 0 && addendCopy.charAt(0) == '0') { + addendCopy = addendCopy.substring(1, addendCopy.length()); + //cuts leading zeroes + } + if (addendCopy.length() == 0) { + return retVal; + //skips the addend, if it is zero and therefore an empty line + } + if (cutResult && addend.length() > resultLength) { + addendCopy = "(" + + addend.substring(0, addend.length() - resultLength) + + ")" + + addend.substring(addend.length() - resultLength); + //adds braces around the cut part of the addend + } + while (addendCopy.length() < addends[0].length() - 1) { + addendCopy = " " + addendCopy; + //elongates the addend with spaces + } + addendCopy = "+" + addendCopy; + //adds a plus symbol in front of the addend + retVal += addendCopy; + //adds the addend to the retVal string + retVal += "\n"; + //adds a new line to the retVal string + return retVal; + } + +// /** +// * TODO: Not Working ATM +// *

+// * Creates an easily readable Addition and returns it. +// * This first splits the Additions and then calls their printCalculation() Method +// * it is therefore an indirect recursion, since this method is only called by the +// * printCalculation() Method. +// * +// * @return String representation of the Calculation +// */ +// private String splitAndPrint() { +// int numberOfAdditions = (addends.length - splitLines) / (splitLines - 1) + 1; +// if (addends.length - splitLines - numberOfAdditions * (splitLines - 1) != 0) { +// numberOfAdditions += 1; +// } +// Addition[] additions = new Addition[numberOfAdditions]; +// String[] carries = new String[numberOfAdditions - 1]; +// for (int i = 0; i < additions.length; i++) { +// copyAddends(additions, carries, i); +// } +// String retVal = ""; +// for (Addition addition : additions) { +// retVal = addition.printCalculation() + retVal; +// } +// return retVal; +// } + +// //TODO: Not Working ATM +// private void copyAddends(Addition[] additions, String[] carries, int i) { +// if (i == 0) { +// String[] addends = new String[splitLines]; +// System.arraycopy(this.addends, 0, addends, 0, splitLines); +// additions[i] = new Addition(addends, 0, resultLength, cutResult); +// carries[i] = additions[i].calcResult().getResult(); +// } else { +// String[] addends = new String[splitLines]; +// addends[0] = carries[i - 1]; +// System.arraycopy(this.addends, splitLines + (i - 1) * (splitLines - 1), addends, 1, splitLines - 1); +// additions[i] = new Addition(addends, 0, resultLength, cutResult); +// if (i < carries.length) { +// carries[i] = additions[i].calcResult().getResult(); +// } +// } +// } + +// /** +// * Getter for the Result +// * +// * @return the Result +// */ +// private String getResult() { +// return result; +// } + +// /** +// * Calculates the Result, but doesn't return the result, it returns itself instead. +// * +// * @return Itself +// */ +// private Addition calcResult() { +// printCalculation(); +// return this; +// } + + /** + * Getter for the ResultCarry, which is the Carry of the Result + * + * @return the ResultCarry + */ + public String getResultCarry() { + return resultCarry; + } + + /** + * The Class AdditionColumn represents one Column of the Addition + */ + private class AdditionColumn { + private String[] shortenedAddends; + private String carries; + private int carry; + private int i; + + /** + * Constructs a new AdditionColumn + * + * @param shortenedAddends The Addend Strings, + * without additional spacing and the plus symbol + * @param carries The String of Carries + * @param carry The Carry for this column + * @param i which column of the Addends should be used + */ + public AdditionColumn(String[] shortenedAddends, String carries, int carry, int i) { + this.shortenedAddends = shortenedAddends; + this.carries = carries; + this.carry = carry; + this.i = i; + } + + /** + * Getter for the Carries + * + * @return the carries + */ + public String getCarries() { + return carries; + } + + /** + * Getter for the Carry + * + * @return the Carry + */ + public int getCarry() { + return carry; + } + + public AdditionColumn invoke() { + Number resultNum = new Number(0); + for (String addend : shortenedAddends) { + if ((addend.length() - i - 1) >= 0) { + resultNum.add(Number.parseNumber(addend.charAt(addend.length() - i - 1))); + } + } + resultNum.add(new Number(carry)); + carry = resultNum.getCarry(); + Number carryNum = new Number(carry); + carries = carryNum.getNumber() + carries; + result = resultNum.getNumber() + result; + return this; + } + } +} diff --git a/src/de/janchristiangruenhage/math/multiplication/LongMultiplication.java b/src/de/janchristiangruenhage/math/multiplication/LongMultiplication.java new file mode 100644 index 0000000..1fa2d03 --- /dev/null +++ b/src/de/janchristiangruenhage/math/multiplication/LongMultiplication.java @@ -0,0 +1,63 @@ +package de.janchristiangruenhage.math.multiplication; + +import java.util.LinkedList; + +/** + * The class LongMultiplication represents a Multiplication of two factors, + * one very very large one (basically limitless) + * and a relatively large one (more than about 25 digits make the calculation pretty confusing) + */ +public class LongMultiplication { + private String factorOne; + private String factorTwo; + private int splitInLine; + private int splitLines; + + /** + * @param factorOne First Factor + * @param factorTwo Second Factor + * @param splitInLine How many digits of the first Factor will be used per multiplication + * @param splitLines How many lines of the multiplication will be added in one addition, + * If != 0 and smaller than the number of digits in the second factor, + * there will be multiple Additions per Multiplication. + */ + public LongMultiplication(String factorOne, String factorTwo, int splitInLine, int splitLines) { + this.factorOne = factorOne; + this.factorTwo = factorTwo; + this.splitInLine = splitInLine; + this.splitLines = splitLines; + } + + /** + * Creates and easily readable Multiplication and returns it. + * + * @return String representation of the Calculation + */ + public String printCalculation() { + LinkedList multiplications = new LinkedList<>(); + //Constructing new List for Multiplications + while (factorOne.length() > splitInLine && splitInLine != 0) { + String splitFactorString = factorOne.substring(factorOne.length() - splitInLine); + factorOne = factorOne.substring(0, factorOne.length() - splitInLine); + //Cutting String for Multiplication Parts + multiplications.add(new Multiplication(splitFactorString, factorTwo, splitLines, false)); + //Constructing new Multiplication and adding it to the List + } + multiplications.add(new Multiplication(factorOne, factorTwo, splitLines, true)); + //Constructing last new Multiplication and adding i to the List + String retVal = ""; + //Initialising the retVal String, which will be returned + for (int i = 0; i < multiplications.size(); i++) { + retVal = multiplications.get(i).printCalculation() + retVal; + //"Printing" the Multiplication, adding it to the retVal String + if (i < multiplications.size() - 1) { + //This is needed to avoid a index out of bounds exception since the next line always uses the following multiplication too + multiplications.get(i + 1).setCarry(multiplications.get(i).getResultCarry()); + //Calculating the Carry for the following Multiplication + } + } + return retVal; + } + + +} diff --git a/src/de/janchristiangruenhage/math/multiplication/Main.java b/src/de/janchristiangruenhage/math/multiplication/Main.java new file mode 100644 index 0000000..1f993f3 --- /dev/null +++ b/src/de/janchristiangruenhage/math/multiplication/Main.java @@ -0,0 +1,13 @@ +package de.janchristiangruenhage.math.multiplication; + +public class Main { + + public static void main(String[] args) { + System.out.println(new LongMultiplication( + "1234567890987654321234567890987654321234567890", + "1029384756574839201", 30, 0).printCalculation()); + System.out.println(new LongMultiplication( + "1234567890987654321234567890987654321234567890", + "1029384756574839201", 30, 3).printCalculation()); + } +} diff --git a/src/de/janchristiangruenhage/math/multiplication/Multiplication.java b/src/de/janchristiangruenhage/math/multiplication/Multiplication.java new file mode 100644 index 0000000..84920f0 --- /dev/null +++ b/src/de/janchristiangruenhage/math/multiplication/Multiplication.java @@ -0,0 +1,140 @@ +package de.janchristiangruenhage.math.multiplication; + +import de.janchristiangruenhage.math.Number; +import de.janchristiangruenhage.math.addition.Addition; + +/** + * The class Multiplication represents a Multiplication fo two relatively large factors, + * with about 25 digits per factor being a reasonable limit. + */ +public class Multiplication { + private String factorOne; + private String factorTwo; + private int splitLines; + private boolean highestMultiplication; + private String carry; + + private String resultCarry; + + /** + * Constructs a new Multiplication Object + * + * @param factorOne First Factor + * @param factorTwo Second Factor + * @param splitLines How many lines of the multiplication will be added in one addition, + * If != 0 and smaller than the number of digits in the second factor, + * there will be multiple Additions per Multiplication. + * @param highestMultiplication If this is a single or the top one of a row of multiplications, + * there is no carry that will be passed over from this one to another Multiplication, + * so the part that would be the carry has not to be enclosed in braces. + */ + public Multiplication(String factorOne, String factorTwo, int splitLines, boolean highestMultiplication) { + this.factorOne = factorOne; + this.factorTwo = factorTwo; + this.splitLines = splitLines; + this.highestMultiplication = highestMultiplication; + this.carry = "0"; + //Initializes carry as zero in case it is the lowest multiplication, + //In case it is not set to another value later + } + + /** + * Creates and easily readable Multiplication and returns it. + * + * @return String representation of the Calculation + */ + public String printCalculation() { + String retVal = ""; + for (int i = 0; i < 2; i++) { + retVal += " "; + } + //Makes the First line some characters longer + retVal += factorOne + " * " + factorTwo; + //Adds the two factors and the Multiplication symbol to the first Line + int lineLength = retVal.length(); + //Saves the length of the first line, this is needed later on since all lines need to have the same length + retVal += "\n"; + for (int i = 0; i < lineLength; i++) { + retVal += "_"; + } + //Draws a line as the second line + retVal += "\n"; + String[] lines; + //array of lines, where the resulting addends will go to + lines = new String[factorTwo.length() + 1]; + lines[lines.length - 1] = carry; + //adds carry as last line + while (lines[lines.length - 1].length() < (lineLength - 1)) { + lines[lines.length - 1] = " " + lines[lines.length - 1]; + //elongates the carries line with spaces + } + lines[lines.length - 1] = "+" + lines[lines.length - 1]; + //adds a plus symbol to the carries line + for (int i = 0; i < factorTwo.length(); i++) { + //iterates through the second factor, therefore through the lines, + // since there is a line for each digit in the second factor + lines[i] = ""; + //constructs the new line string + int carry = 0; + //creates new carry variable + for (int j = 0; j < i; j++) { + lines[i] = 0 + lines[i]; + //adds zeroes to the addends, depending on the digit they get multiplied with, + //for example if its the line of the 3 in 344, then two zeroes get added + } + Number factorTwoNum = Number.parseNumber(factorTwo.charAt(factorTwo.length() - i - 1)); + //Parses the first one digit factor from the first factor string + for (int j = 0; j < factorOne.length(); j++) { + Number factorOneNum = Number.parseNumber(factorOne.charAt(factorOne.length() - j - 1)); + //Parses the second one digit factor from the first factor string + Number result = Number.multiply(factorOneNum, factorTwoNum, carry); + //Multiplies the factors, adds the carry after that + carry = result.getCarry(); + //assigns the carry it's new value + lines[i] = result.toString() + lines[i]; + //adds the result to the line + } + if (carry != 0) { + lines[i] = carry + lines[i]; + //if there is a carry left in the end, it gets added to the same line + } + while (lines[i].length() < (lineLength - 1)) { + lines[i] = " " + lines[i]; + //elongates the line with spaces + } + if (i > 0) { + lines[i] = "+" + lines[i]; + //adds a plus symbol to the line + } else { + lines[i] = " " + lines[i]; + //adds another space to the line + } + } + Addition addition = new Addition(lines, splitLines, factorOne.length(), !highestMultiplication); + //Construct the new addition + retVal += addition.printCalculation(); + //prints the addition and adds that to the retVal string + resultCarry = addition.getResultCarry(); + //saves the resultCarry of the addition, + // this is used for the higher multiplications, if there are any + return retVal; + } + + /** + * Setter for the carry + * + * @param carry the carry + */ + public void setCarry(String carry) { + this.carry = carry; + } + + /** + * Getter for the carry of the result + * + * @return the carry of the result + */ + public String getResultCarry() { + return resultCarry; + } +} diff --git a/src/de/janchristiangruenhage/util/MultiOutput.java b/src/de/janchristiangruenhage/util/MultiOutput.java new file mode 100644 index 0000000..8236962 --- /dev/null +++ b/src/de/janchristiangruenhage/util/MultiOutput.java @@ -0,0 +1,41 @@ +package de.janchristiangruenhage.util; + +import java.io.IOException; +import java.io.PrintStream; +import java.io.Writer; +import java.util.LinkedList; + +/** + * Created by green on 12/8/2015. + */ +public class MultiOutput { + LinkedList writers; + LinkedList printStreams; + + public MultiOutput() { + writers = new LinkedList<>(); + printStreams = new LinkedList<>(); + } + + public boolean add(Writer writer) { + return writers.add(writer); + } + + public boolean add(PrintStream printStream) { + return printStreams.add(printStream); + } + + public void println(String out) { + for (Writer writer : writers) { + try { + writer.write(out + "\n"); + writer.flush(); + } catch (IOException e) { + e.printStackTrace(); + } + } + for (PrintStream printStream : printStreams) { + printStream.println(out); + } + } +}