diff --git a/src/de/janchristiangruenhage/demo/math/multiplication/Main.java b/src/de/janchristiangruenhage/demo/math/multiplication/Main.java index ab3f05f..9f94d5b 100644 --- a/src/de/janchristiangruenhage/demo/math/multiplication/Main.java +++ b/src/de/janchristiangruenhage/demo/math/multiplication/Main.java @@ -1,15 +1,40 @@ package de.janchristiangruenhage.demo.math.multiplication; import de.janchristiangruenhage.math.multiplication.LongMultiplication; +import de.janchristiangruenhage.util.MultiOutput; + +import java.io.FileNotFoundException; +import java.io.PrintWriter; public class Main { public static void main(String[] args) { - System.out.println(new LongMultiplication( - "32490", - "436", 30, 0).printCalculation()); - System.out.println(new LongMultiplication( - "1234567890987654321234567890987654321234567890", - "1029384756574839201", 30, 6).printCalculation()); + String factorOne = ""; + //Constructs the factorOne string + for (int i = 0; i < 20; i++) { + factorOne += (char) (48 + ((int) (Math.random() * 10))); + //adds a random number character to the factorOne string + } + String factorTwo = ""; + //Constructs the factorTwo string + for (int i = 0; i < 5; i++) { + factorTwo += (char) (48 + ((int) (Math.random() * 10))); + //adds a random number character to the factorTwo string + } + MultiOutput output = getOutputs(factorOne, factorTwo); + //gets the output for these factors; + output.println(new LongMultiplication(factorOne, factorTwo, 50, 0).printCalculation()); + //prints the calculation + } + + private static MultiOutput getOutputs(String factorOne, String factorTwo) { + MultiOutput output = new MultiOutput(); + output.add(System.out); + try { + output.add(new PrintWriter(("Multiplication" + factorOne.hashCode() + factorTwo.hashCode()))); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + return output; } } diff --git a/src/de/janchristiangruenhage/math/addition/Addition.java b/src/de/janchristiangruenhage/math/addition/Addition.java index ab31442..6dc0871 100644 --- a/src/de/janchristiangruenhage/math/addition/Addition.java +++ b/src/de/janchristiangruenhage/math/addition/Addition.java @@ -181,8 +181,6 @@ public class Addition { } /** - * 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 @@ -192,53 +190,69 @@ public class Addition { */ private String splitAndPrint() { LinkedList additions = new LinkedList<>(); + //creates a list of additions int carryLine = 0; + //if there is a carry line, 1 if true, 0 else while (addends.size() > 0) { LinkedList addends = new LinkedList<>(); + //constructs a new addend list if (carryLine != 0) { - String intermediateResult = additions.getLast().calculateResult(); + String intermediateResult = additions.getLast().getResult(); + //gets the result of the addition before if (cutResult) { intermediateResult = intermediateResult.replaceFirst("\\(", ""); intermediateResult = intermediateResult.replaceFirst("\\)", ""); + //cuts away the braces around the carry } addends.add(intermediateResult); - + //adds the result as first addend of new addition } for (int i = 0; i < splitLines - carryLine; i++) { addends.add(this.addends.removeFirst()); + //adds the removed addends if (this.addends.size() == 0) { break; + //if the addend list is empty, it breaks the loop } } additions.add(new Addition(addends, splitLines, resultLength, cutResult, lineLength)); + //constructs new addition carryLine = 1; + //sets the carry line int to one, indicating that there is already an addition } String retVal = ""; + //constructing the retVal String for (Addition addition : additions) { retVal += addition.printCalculation(); + //printing all additions } resultCarry = additions.getLast().getResultCarry(); - result = additions.getLast().calculateResult(); + //saves the resultCarry + result = additions.getLast().getResult(); + //saves the result return retVal; } /** - * Calculator for the Result + * Getter for the result * - * @return the Result + * @return the result */ - private String calculateResult() { + public String getResult() { printCalculation(); + //Ensures, that the result is != null return result; } /** - * Getter for the ResultCarry, which is the Carry of the Result + * Getter for the resultCarry, which is the carry of the result * - * @return the ResultCarry + * @return the resultCarry */ public String getResultCarry() { + printCalculation(); + //Ensures that the resultCarry ist != null return resultCarry; } @@ -285,6 +299,11 @@ public class Addition { return carry; } + /** + * Executes the Method-Object + * + * @return + */ public AdditionColumn invoke() { Number resultNum = new Number(0); for (String addend : shortenedAddends) { diff --git a/src/de/janchristiangruenhage/math/multiplication/LongMultiplication.java b/src/de/janchristiangruenhage/math/multiplication/LongMultiplication.java index 1fa2d03..ad40e46 100644 --- a/src/de/janchristiangruenhage/math/multiplication/LongMultiplication.java +++ b/src/de/janchristiangruenhage/math/multiplication/LongMultiplication.java @@ -56,6 +56,15 @@ public class LongMultiplication { //Calculating the Carry for the following Multiplication } } +// retVal += "\n"; +// retVal += factorOne + "\n"; +// retVal += "*\n"; +// retVal += factorTwo + "\n"; +// retVal += "=\n"; +// for (Multiplication multiplication: multiplications) { +// retVal += multiplication.getResultWithoutCarry(); +// } + retVal += "\n\n\n\n\n\n"; return retVal; } diff --git a/src/de/janchristiangruenhage/math/multiplication/Multiplication.java b/src/de/janchristiangruenhage/math/multiplication/Multiplication.java index a2f427c..b045bdb 100644 --- a/src/de/janchristiangruenhage/math/multiplication/Multiplication.java +++ b/src/de/janchristiangruenhage/math/multiplication/Multiplication.java @@ -16,7 +16,10 @@ public class Multiplication { private boolean highestMultiplication; private String carry; + private String result; private String resultCarry; + private String resultWithoutCarry; + /** * Constructs a new Multiplication Object @@ -116,9 +119,14 @@ public class Multiplication { //Construct the new addition retVal += addition.printCalculation(); //prints the addition and adds that to the retVal string + retVal += "\n\n\n\n"; + //some new lines after the multiplication + result = addition.getResult(); + //saves the result of the addition resultCarry = addition.getResultCarry(); //saves the resultCarry of the addition, - // this is used for the higher multiplications, if there are any + //this is used for the higher multiplications, if there are any + //resultWithoutCarry = result.replaceFirst("\(\d*\)", ""); return retVal; } @@ -139,4 +147,8 @@ public class Multiplication { public String getResultCarry() { return resultCarry; } + + public String getResultWithoutCarry() { + return resultWithoutCarry; + } }