1
0
Fork 0
Fixed Bug with splitLines argument, works now,
arrays (mostly?) replaced with linked lists
This commit is contained in:
Jan Christian Grünhage 2016-01-13 22:45:13 +01:00
parent 76f4567346
commit 20fe63bd5f
5 changed files with 123 additions and 118 deletions

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="" vcs="" /> <mapping directory="$PROJECT_DIR$" vcs="Git" />
</component> </component>
</project> </project>

View file

@ -0,0 +1,15 @@
package de.janchristiangruenhage.demo.math.multiplication;
import de.janchristiangruenhage.math.multiplication.LongMultiplication;
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());
}
}

View file

@ -1,16 +1,19 @@
package de.janchristiangruenhage.math.addition; package de.janchristiangruenhage.math.addition;
import de.janchristiangruenhage.exceptions.FeatureNotImplementedYetException;
import de.janchristiangruenhage.math.Number; import de.janchristiangruenhage.math.Number;
import java.util.LinkedList;
public class Addition { public class Addition {
private String[] addends; private LinkedList<String> addends;
private int splitLines; private int splitLines;
private String printedCalculation;
private String result; private String result;
private int resultLength; private int resultLength;
private boolean cutResult; private boolean cutResult;
private String resultCarry; private String resultCarry;
private boolean calculated; private boolean calculated;
private int lineLength;
/** /**
* Constructs a new <tt>Addition.</tt> * Constructs a new <tt>Addition.</tt>
@ -20,12 +23,14 @@ public class Addition {
* @param resultLength How many digits the result should have, for overflow or a carry * @param resultLength How many digits the result should have, for overflow or a carry
* see cutResult * see cutResult
* @param cutResult If the result should be cut * @param cutResult If the result should be cut
* @param lineLength How long each line should be
*/ */
public Addition(String[] addends, int splitLines, int resultLength, boolean cutResult) { public Addition(LinkedList<String> addends, int splitLines, int resultLength, boolean cutResult, int lineLength) {
this.addends = addends; this.addends = addends;
this.splitLines = splitLines; this.splitLines = splitLines;
this.resultLength = resultLength; this.resultLength = resultLength;
this.cutResult = cutResult; this.cutResult = cutResult;
this.lineLength = lineLength;
} }
/** /**
@ -35,13 +40,13 @@ public class Addition {
*/ */
public String printCalculation() { public String printCalculation() {
if (calculated) { if (calculated) {
return result; return printedCalculation;
} }
if (splitLines != 0) { if (splitLines != 0 && splitLines < addends.size()) {
//decides whether the Addition has to be split before Calculation //decides whether the Addition has to be split before Calculation
throw new FeatureNotImplementedYetException(); //throw new FeatureNotImplementedYetException();
//Throws new Feature not Implemented Yet Exception //Throws new Feature not Implemented Yet Exception
//return splitAndPrint(); return splitAndPrint();
//splits and prints the calculation, not working yet //splits and prints the calculation, not working yet
} else { } else {
return print(); return print();
@ -58,13 +63,13 @@ public class Addition {
private String print() { private String print() {
int maximum = 0; int maximum = 0;
//Variable for the Maximum Length of a Addend String //Variable for the Maximum Length of a Addend String
String[] shortenedAddends = new String[addends.length]; LinkedList<String> shortenedAddends = new LinkedList<>();
for (int i = 0; i < addends.length; i++) { for (String addend : addends) {
shortenedAddends[i] = addends[i].substring(1).trim(); shortenedAddends.add(addend.substring(1).trim());
//The substring for removing the plus symbol at the beginning, the trimming for removing unnecessary spaces //The substring for removing the plus symbol at the beginning, the trimming for removing unnecessary spaces
maximum = (shortenedAddends[i].length() > maximum) ? maximum = (shortenedAddends.getLast().length() > maximum) ?
//Checks whether the length of the i-th String is higher than the maximum variable //Checks whether the length of the i-th String is higher than the maximum variable
shortenedAddends[i].length() : maximum; shortenedAddends.getLast().length() : maximum;
//Assigns the maximum the correct value, the string length if its bigger, itself otherwise //Assigns the maximum the correct value, the string length if its bigger, itself otherwise
} }
result = ""; result = "";
@ -81,13 +86,16 @@ public class Addition {
carry = additionColumn.getCarry(); carry = additionColumn.getCarry();
//Get the carry from the Method-Object //Get the carry from the Method-Object
} }
if (carry != 0) {
result = carry + result;
}
String retVal = ""; String retVal = "";
//Construct the retVal String where the calculation wil be put into //Construct the retVal String where the calculation wil be put into
for (String addend : shortenedAddends) { for (String addend : shortenedAddends) {
retVal = addAddend(retVal, addend); retVal = addAddend(retVal, addend);
//Adds the addend to the retVal string in a new line //Adds the addend to the retVal string in a new line
} }
for (int i = 0; i < addends[0].length(); i++) { for (int i = 0; i < lineLength; i++) {
retVal += " "; retVal += " ";
//Adds a line of spaces //Adds a line of spaces
} }
@ -104,7 +112,7 @@ public class Addition {
+ carries.substring(carries.length() - resultLength); + carries.substring(carries.length() - resultLength);
//adds braces around the cut part of the result, if wanted //adds braces around the cut part of the result, if wanted
} }
while (carries.length() < addends[0].length() - 1) { while (carries.length() < lineLength - 1) {
carries = " " + carries; carries = " " + carries;
//elongates the carries string with spaces //elongates the carries string with spaces
} }
@ -112,7 +120,7 @@ public class Addition {
//adds the plus symbol to the carries string //adds the plus symbol to the carries string
retVal += carries + "\n"; retVal += carries + "\n";
//adds the carries string to the retVal string in a new line //adds the carries string to the retVal string in a new line
for (int i = 0; i < addends[0].length(); i++) { for (int i = 0; i < lineLength; i++) {
retVal += "_"; retVal += "_";
//draws a line //draws a line
} }
@ -127,14 +135,16 @@ public class Addition {
+ result.substring(result.length() - resultLength); + result.substring(result.length() - resultLength);
//adds braces around the carry //adds braces around the carry
} }
while (result.length() <= addends[0].length() - 1) { while (result.length() <= lineLength - 1) {
result = " " + result; result = " " + result;
//elongates the result string //elongates the result string
} }
retVal += result; retVal += result;
//adds the result to the retVal string //adds the result to the retVal string
retVal += "\n\n\n\n"; retVal += "\n\n";
//adds four new lines after the calculation //adds four new lines after the calculation
printedCalculation = retVal;
//saving the printed calculation for later usage
calculated = true; calculated = true;
//sets calculated to true, that the result doesn't have to be recalculated if it is needed again //sets calculated to true, that the result doesn't have to be recalculated if it is needed again
return retVal; return retVal;
@ -157,7 +167,7 @@ public class Addition {
+ addend.substring(addend.length() - resultLength); + addend.substring(addend.length() - resultLength);
//adds braces around the cut part of the addend //adds braces around the cut part of the addend
} }
while (addendCopy.length() < addends[0].length() - 1) { while (addendCopy.length() < lineLength - 1) {
addendCopy = " " + addendCopy; addendCopy = " " + addendCopy;
//elongates the addend with spaces //elongates the addend with spaces
} }
@ -170,69 +180,58 @@ public class Addition {
return retVal; return retVal;
} }
// /** /**
// * TODO: Not Working ATM * TODO: Not Working ATM
// * <p> * <p>
// * Creates an easily readable Addition and returns it. * Creates an easily readable Addition and returns it.
// * This first splits the Additions and then calls their printCalculation() Method * 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 * it is therefore an indirect recursion, since this method is only called by the
// * printCalculation() Method. * printCalculation() Method.
// * *
// * @return String representation of the Calculation * @return String representation of the Calculation
// */ */
// private String splitAndPrint() { private String splitAndPrint() {
// int numberOfAdditions = (addends.length - splitLines) / (splitLines - 1) + 1; LinkedList<Addition> additions = new LinkedList<>();
// if (addends.length - splitLines - numberOfAdditions * (splitLines - 1) != 0) { int carryLine = 0;
// numberOfAdditions += 1; while (addends.size() > 0) {
// } LinkedList<String> addends = new LinkedList<>();
// Addition[] additions = new Addition[numberOfAdditions]; if (carryLine != 0) {
// String[] carries = new String[numberOfAdditions - 1]; String intermediateResult = additions.getLast().calculateResult();
// for (int i = 0; i < additions.length; i++) { if (cutResult) {
// copyAddends(additions, carries, i); intermediateResult = intermediateResult.replaceFirst("\\(", "");
// } intermediateResult = intermediateResult.replaceFirst("\\)", "");
// String retVal = ""; }
// for (Addition addition : additions) { addends.add(intermediateResult);
// retVal = addition.printCalculation() + retVal;
// }
// return retVal;
// }
// //TODO: Not Working ATM }
// private void copyAddends(Addition[] additions, String[] carries, int i) { for (int i = 0; i < splitLines - carryLine; i++) {
// if (i == 0) { addends.add(this.addends.removeFirst());
// String[] addends = new String[splitLines]; if (this.addends.size() == 0) {
// System.arraycopy(this.addends, 0, addends, 0, splitLines); break;
// additions[i] = new Addition(addends, 0, resultLength, cutResult); }
// carries[i] = additions[i].calcResult().getResult(); }
// } else { additions.add(new Addition(addends, splitLines, resultLength, cutResult, lineLength));
// String[] addends = new String[splitLines]; carryLine = 1;
// addends[0] = carries[i - 1]; }
// System.arraycopy(this.addends, splitLines + (i - 1) * (splitLines - 1), addends, 1, splitLines - 1); String retVal = "";
// additions[i] = new Addition(addends, 0, resultLength, cutResult); for (Addition addition : additions) {
// if (i < carries.length) { retVal += addition.printCalculation();
// carries[i] = additions[i].calcResult().getResult(); }
// } resultCarry = additions.getLast().getResultCarry();
// } result = additions.getLast().calculateResult();
// } return retVal;
}
// /** /**
// * Getter for the Result * Calculator for the Result
// * *
// * @return the Result * @return the Result
// */ */
// private String getResult() { private String calculateResult() {
// return result; printCalculation();
// } 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 * Getter for the ResultCarry, which is the Carry of the Result
@ -247,7 +246,7 @@ public class Addition {
* The Class <tt>AdditionColumn</tt> represents one Column of the Addition * The Class <tt>AdditionColumn</tt> represents one Column of the Addition
*/ */
private class AdditionColumn { private class AdditionColumn {
private String[] shortenedAddends; private LinkedList<String> shortenedAddends;
private String carries; private String carries;
private int carry; private int carry;
private int i; private int i;
@ -261,7 +260,7 @@ public class Addition {
* @param carry The Carry for this column * @param carry The Carry for this column
* @param i which column of the Addends should be used * @param i which column of the Addends should be used
*/ */
public AdditionColumn(String[] shortenedAddends, String carries, int carry, int i) { public AdditionColumn(LinkedList<String> shortenedAddends, String carries, int carry, int i) {
this.shortenedAddends = shortenedAddends; this.shortenedAddends = shortenedAddends;
this.carries = carries; this.carries = carries;
this.carry = carry; this.carry = carry;
@ -300,5 +299,7 @@ public class Addition {
result = resultNum.getNumber() + result; result = resultNum.getNumber() + result;
return this; return this;
} }
} }
} }

View file

@ -1,13 +0,0 @@
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());
}
}

View file

@ -3,6 +3,8 @@ package de.janchristiangruenhage.math.multiplication;
import de.janchristiangruenhage.math.Number; import de.janchristiangruenhage.math.Number;
import de.janchristiangruenhage.math.addition.Addition; import de.janchristiangruenhage.math.addition.Addition;
import java.util.LinkedList;
/** /**
* The class <tt>Multiplication</tt> represents a Multiplication fo two relatively large factors, * The class <tt>Multiplication</tt> represents a Multiplication fo two relatively large factors,
* with about 25 digits per factor being a reasonable limit. * with about 25 digits per factor being a reasonable limit.
@ -48,37 +50,28 @@ public class Multiplication {
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
retVal += " "; retVal += " ";
} }
//Makes the First line some characters longer //Makes the first line some characters longer
retVal += factorOne + " * " + factorTwo; retVal += factorOne + " * " + factorTwo;
//Adds the two factors and the Multiplication symbol to the first Line //Adds the two factors and the Multiplication symbol to the first Line
int lineLength = retVal.length(); 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 //Saves the length of the first line, this is needed later on since all addends need to have the same length
retVal += "\n"; retVal += "\n";
for (int i = 0; i < lineLength; i++) { for (int i = 0; i < lineLength; i++) {
retVal += "_"; retVal += "_";
} }
//Draws a line as the second line //Draws a line as the second line
retVal += "\n"; retVal += "\n";
String[] lines; LinkedList<String> addends = new LinkedList<>();
//array of lines, where the resulting addends will go to //list of addends, 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++) { for (int i = 0; i < factorTwo.length(); i++) {
//iterates through the second factor, therefore through the lines, //iterates through the second factor, therefore through the addends,
// since there is a line for each digit in the second factor // since there is a addend for each digit in the second factor
lines[i] = ""; String intermediateResult = "";
//constructs the new line string //constructs the new line string
int carry = 0; int carry = 0;
//creates new carry variable //creates new carry variable
for (int j = 0; j < i; j++) { for (int j = 0; j < i; j++) {
lines[i] = 0 + lines[i]; intermediateResult = 0 + intermediateResult;
//adds zeroes to the addends, depending on the digit they get multiplied with, //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 //for example if its the line of the 3 in 344, then two zeroes get added
} }
@ -91,26 +84,35 @@ public class Multiplication {
//Multiplies the factors, adds the carry after that //Multiplies the factors, adds the carry after that
carry = result.getCarry(); carry = result.getCarry();
//assigns the carry it's new value //assigns the carry it's new value
lines[i] = result.toString() + lines[i]; intermediateResult = result.toString() + intermediateResult;
//adds the result to the line //adds the result to the line
} }
if (carry != 0) { if (carry != 0) {
lines[i] = carry + lines[i]; intermediateResult = carry + intermediateResult;
//if there is a carry left in the end, it gets added to the same line //if there is a carry left in the end, it gets added to the same line
} }
while (lines[i].length() < (lineLength - 1)) { while (intermediateResult.length() < (lineLength - 1)) {
lines[i] = " " + lines[i]; intermediateResult = " " + intermediateResult;
//elongates the line with spaces //elongates the line with spaces
} }
if (i > 0) { if (i > 0) {
lines[i] = "+" + lines[i]; intermediateResult = "+" + intermediateResult;
//adds a plus symbol to the line //adds a plus symbol to the line
} else { } else {
lines[i] = " " + lines[i]; intermediateResult = " " + intermediateResult;
//adds another space to the line //adds another space to the line
} }
addends.add(intermediateResult);
} }
Addition addition = new Addition(lines, splitLines, factorOne.length(), !highestMultiplication); while (carry.length() < (lineLength - 1)) {
carry = " " + carry;
//elongates the carries line with spaces
}
carry = "+" + carry;
//adds a plus symbol to the carries line
addends.add(carry);
//adds carry as last line
Addition addition = new Addition(addends, splitLines, factorOne.length(), !highestMultiplication, lineLength);
//Construct the new addition //Construct the new addition
retVal += addition.printCalculation(); retVal += addition.printCalculation();
//prints the addition and adds that to the retVal string //prints the addition and adds that to the retVal string