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"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</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;
import de.janchristiangruenhage.exceptions.FeatureNotImplementedYetException;
import de.janchristiangruenhage.math.Number;
import java.util.LinkedList;
public class Addition {
private String[] addends;
private LinkedList<String> addends;
private int splitLines;
private String printedCalculation;
private String result;
private int resultLength;
private boolean cutResult;
private String resultCarry;
private boolean calculated;
private int lineLength;
/**
* 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
* see cutResult
* @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.splitLines = splitLines;
this.resultLength = resultLength;
this.cutResult = cutResult;
this.lineLength = lineLength;
}
/**
@ -35,13 +40,13 @@ public class Addition {
*/
public String printCalculation() {
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
throw new FeatureNotImplementedYetException();
//throw new FeatureNotImplementedYetException();
//Throws new Feature not Implemented Yet Exception
//return splitAndPrint();
return splitAndPrint();
//splits and prints the calculation, not working yet
} else {
return print();
@ -58,13 +63,13 @@ public class Addition {
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();
LinkedList<String> shortenedAddends = new LinkedList<>();
for (String addend : addends) {
shortenedAddends.add(addend.substring(1).trim());
//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
shortenedAddends[i].length() : maximum;
shortenedAddends.getLast().length() : maximum;
//Assigns the maximum the correct value, the string length if its bigger, itself otherwise
}
result = "";
@ -81,13 +86,16 @@ public class Addition {
carry = additionColumn.getCarry();
//Get the carry from the Method-Object
}
if (carry != 0) {
result = carry + result;
}
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++) {
for (int i = 0; i < lineLength; i++) {
retVal += " ";
//Adds a line of spaces
}
@ -104,7 +112,7 @@ public class Addition {
+ carries.substring(carries.length() - resultLength);
//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;
//elongates the carries string with spaces
}
@ -112,7 +120,7 @@ public class Addition {
//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++) {
for (int i = 0; i < lineLength; i++) {
retVal += "_";
//draws a line
}
@ -127,14 +135,16 @@ public class Addition {
+ result.substring(result.length() - resultLength);
//adds braces around the carry
}
while (result.length() <= addends[0].length() - 1) {
while (result.length() <= lineLength - 1) {
result = " " + result;
//elongates the result string
}
retVal += result;
//adds the result to the retVal string
retVal += "\n\n\n\n";
retVal += "\n\n";
//adds four new lines after the calculation
printedCalculation = retVal;
//saving the printed calculation for later usage
calculated = true;
//sets calculated to true, that the result doesn't have to be recalculated if it is needed again
return retVal;
@ -157,7 +167,7 @@ public class Addition {
+ addend.substring(addend.length() - resultLength);
//adds braces around the cut part of the addend
}
while (addendCopy.length() < addends[0].length() - 1) {
while (addendCopy.length() < lineLength - 1) {
addendCopy = " " + addendCopy;
//elongates the addend with spaces
}
@ -170,69 +180,58 @@ public class Addition {
return retVal;
}
// /**
// * TODO: Not Working ATM
// * <p>
// * 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
* <p>
* 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() {
LinkedList<Addition> additions = new LinkedList<>();
int carryLine = 0;
while (addends.size() > 0) {
LinkedList<String> addends = new LinkedList<>();
if (carryLine != 0) {
String intermediateResult = additions.getLast().calculateResult();
if (cutResult) {
intermediateResult = intermediateResult.replaceFirst("\\(", "");
intermediateResult = intermediateResult.replaceFirst("\\)", "");
}
addends.add(intermediateResult);
// //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();
// }
// }
// }
}
for (int i = 0; i < splitLines - carryLine; i++) {
addends.add(this.addends.removeFirst());
if (this.addends.size() == 0) {
break;
}
}
additions.add(new Addition(addends, splitLines, resultLength, cutResult, lineLength));
carryLine = 1;
}
String retVal = "";
for (Addition addition : additions) {
retVal += addition.printCalculation();
}
resultCarry = additions.getLast().getResultCarry();
result = additions.getLast().calculateResult();
return retVal;
}
// /**
// * Getter for the Result
// *
// * @return the Result
// */
// private String getResult() {
// return result;
// }
/**
* Calculator for the Result
*
* @return the Result
*/
private String calculateResult() {
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
@ -247,7 +246,7 @@ public class Addition {
* The Class <tt>AdditionColumn</tt> represents one Column of the Addition
*/
private class AdditionColumn {
private String[] shortenedAddends;
private LinkedList<String> shortenedAddends;
private String carries;
private int carry;
private int i;
@ -261,7 +260,7 @@ public class Addition {
* @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) {
public AdditionColumn(LinkedList<String> shortenedAddends, String carries, int carry, int i) {
this.shortenedAddends = shortenedAddends;
this.carries = carries;
this.carry = carry;
@ -300,5 +299,7 @@ public class Addition {
result = resultNum.getNumber() + result;
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.addition.Addition;
import java.util.LinkedList;
/**
* The class <tt>Multiplication</tt> represents a Multiplication fo two relatively large factors,
* with about 25 digits per factor being a reasonable limit.
@ -48,37 +50,28 @@ public class Multiplication {
for (int i = 0; i < 2; i++) {
retVal += " ";
}
//Makes the First line some characters longer
//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
//Saves the length of the first line, this is needed later on since all addends 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
LinkedList<String> addends = new LinkedList<>();
//list of addends, where the resulting addends will go to
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] = "";
//iterates through the second factor, therefore through the addends,
// since there is a addend for each digit in the second factor
String intermediateResult = "";
//constructs the new line string
int carry = 0;
//creates new carry variable
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,
//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
carry = result.getCarry();
//assigns the carry it's new value
lines[i] = result.toString() + lines[i];
intermediateResult = result.toString() + intermediateResult;
//adds the result to the line
}
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
}
while (lines[i].length() < (lineLength - 1)) {
lines[i] = " " + lines[i];
while (intermediateResult.length() < (lineLength - 1)) {
intermediateResult = " " + intermediateResult;
//elongates the line with spaces
}
if (i > 0) {
lines[i] = "+" + lines[i];
intermediateResult = "+" + intermediateResult;
//adds a plus symbol to the line
} else {
lines[i] = " " + lines[i];
intermediateResult = " " + intermediateResult;
//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
retVal += addition.printCalculation();
//prints the addition and adds that to the retVal string