Initial commit
This commit is contained in:
commit
76f4567346
16 changed files with 929 additions and 0 deletions
22
.idea/compiler.xml
Normal file
22
.idea/compiler.xml
Normal file
|
@ -0,0 +1,22 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<resourceExtensions />
|
||||
<wildcardResourcePatterns>
|
||||
<entry name="!?*.java" />
|
||||
<entry name="!?*.form" />
|
||||
<entry name="!?*.class" />
|
||||
<entry name="!?*.groovy" />
|
||||
<entry name="!?*.scala" />
|
||||
<entry name="!?*.flex" />
|
||||
<entry name="!?*.kt" />
|
||||
<entry name="!?*.clj" />
|
||||
<entry name="!?*.aj" />
|
||||
</wildcardResourcePatterns>
|
||||
<annotationProcessing>
|
||||
<profile default="true" name="Default" enabled="false">
|
||||
<processorPath useClasspath="true" />
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
</component>
|
||||
</project>
|
3
.idea/copyright/profiles_settings.xml
Normal file
3
.idea/copyright/profiles_settings.xml
Normal file
|
@ -0,0 +1,3 @@
|
|||
<component name="CopyrightManager">
|
||||
<settings default="" />
|
||||
</component>
|
1
.idea/description.html
Normal file
1
.idea/description.html
Normal file
|
@ -0,0 +1 @@
|
|||
<html>Simple <b>Java</b> application that includes a class with <code>main()</code> method</html>
|
12
.idea/misc.xml
Normal file
12
.idea/misc.xml
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="EntryPointsManager">
|
||||
<entry_points version="2.0" />
|
||||
</component>
|
||||
<component name="ProjectKey">
|
||||
<option name="state" value="project://e2804f05-5315-4fc6-a121-c522a6c26470" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/Multiplication.iml" filepath="$PROJECT_DIR$/Multiplication.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
3
.idea/project-template.xml
Normal file
3
.idea/project-template.xml
Normal file
|
@ -0,0 +1,3 @@
|
|||
<template>
|
||||
<input-field default="com.company">IJ_BASE_PACKAGE</input-field>
|
||||
</template>
|
124
.idea/uiDesigner.xml
Normal file
124
.idea/uiDesigner.xml
Normal file
|
@ -0,0 +1,124 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Palette2">
|
||||
<group name="Swing">
|
||||
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
|
||||
</item>
|
||||
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
|
||||
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
|
||||
<initial-values>
|
||||
<property name="text" value="Button" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="RadioButton" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="CheckBox" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
|
||||
<initial-values>
|
||||
<property name="text" value="Label" />
|
||||
</initial-values>
|
||||
</item>
|
||||
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||
<preferred-size width="150" height="-1" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||
<preferred-size width="150" height="50" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||
<preferred-size width="200" height="200" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||
<preferred-size width="200" height="200" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
|
||||
</item>
|
||||
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
|
||||
<preferred-size width="-1" height="20" />
|
||||
</default-constraints>
|
||||
</item>
|
||||
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
|
||||
</item>
|
||||
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
||||
</item>
|
||||
</group>
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="" />
|
||||
</component>
|
||||
</project>
|
12
Multiplication.iml
Normal file
12
Multiplication.iml
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package de.janchristiangruenhage.exceptions;
|
||||
|
||||
public class FeatureNotImplementedYetException extends RuntimeException {
|
||||
public FeatureNotImplementedYetException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public FeatureNotImplementedYetException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
166
src/de/janchristiangruenhage/math/Number.java
Normal file
166
src/de/janchristiangruenhage/math/Number.java
Normal file
|
@ -0,0 +1,166 @@
|
|||
package de.janchristiangruenhage.math;
|
||||
|
||||
/**
|
||||
* The class <tt>Number</tt> represents a one digit number and a multiple digit carry
|
||||
* <p>
|
||||
* Examples: The Number representation of:
|
||||
* 12 would be 2 carry 1
|
||||
* 123 would be 3 carry 12
|
||||
* 5351 would be 1 carry 535
|
||||
* <p>
|
||||
* and so on.
|
||||
*/
|
||||
public class Number {
|
||||
private int number;
|
||||
private int carry;
|
||||
|
||||
/**
|
||||
* Constructs a new Number using a number and a carry.
|
||||
* <p>
|
||||
* 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.
|
||||
* <p>
|
||||
* 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;
|
||||
}
|
||||
}
|
304
src/de/janchristiangruenhage/math/addition/Addition.java
Normal file
304
src/de/janchristiangruenhage/math/addition/Addition.java
Normal file
|
@ -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 <tt>Addition.</tt>
|
||||
*
|
||||
* @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
|
||||
// * <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
|
||||
// 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 <tt>AdditionColumn</tt> represents one Column of the Addition
|
||||
*/
|
||||
private class AdditionColumn {
|
||||
private String[] shortenedAddends;
|
||||
private String carries;
|
||||
private int carry;
|
||||
private int i;
|
||||
|
||||
/**
|
||||
* Constructs a new <tt>AdditionColumn</tt>
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package de.janchristiangruenhage.math.multiplication;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
* The class <tt>LongMultiplication</tt> 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<Multiplication> 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
13
src/de/janchristiangruenhage/math/multiplication/Main.java
Normal file
13
src/de/janchristiangruenhage/math/multiplication/Main.java
Normal file
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,140 @@
|
|||
package de.janchristiangruenhage.math.multiplication;
|
||||
|
||||
import de.janchristiangruenhage.math.Number;
|
||||
import de.janchristiangruenhage.math.addition.Addition;
|
||||
|
||||
/**
|
||||
* The class <tt>Multiplication</tt> 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;
|
||||
}
|
||||
}
|
41
src/de/janchristiangruenhage/util/MultiOutput.java
Normal file
41
src/de/janchristiangruenhage/util/MultiOutput.java
Normal file
|
@ -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<Writer> writers;
|
||||
LinkedList<PrintStream> 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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue