Added timeout and Multiplication Method 1
This commit is contained in:
parent
7417fcefe7
commit
e3912397d7
Binary file not shown.
|
@ -1,2 +1,3 @@
|
|||
# PICalculator
|
||||
Scientific calculator in java.
|
||||
# PICalculator
|
||||
Scientific calculator in java.
|
||||
![alt text](https://github.com/XDrake99/PICalculator/blob/master/res/decimal.png "Example expression")
|
Binary file not shown.
After Width: | Height: | Size: 7.4 KiB |
BIN
res/font_ex.rft
BIN
res/font_ex.rft
Binary file not shown.
|
@ -1,5 +1,5 @@
|
|||
package org.warp.picalculator;
|
||||
|
||||
public enum Errors {
|
||||
ERROR, DIVISION_BY_ZERO, UNBALANCED_BRACKETS, NOT_IMPLEMENTED, NEGATIVE_PARAMETER, NUMBER_TOO_LARGE, NUMBER_TOO_SMALL, CONVERSION_ERROR, SYNTAX_ERROR, NOT_AN_EQUATION
|
||||
ERROR, DIVISION_BY_ZERO, UNBALANCED_BRACKETS, NOT_IMPLEMENTED, NEGATIVE_PARAMETER, NUMBER_TOO_LARGE, NUMBER_TOO_SMALL, CONVERSION_ERROR, SYNTAX_ERROR, NOT_AN_EQUATION, TIMEOUT
|
||||
}
|
||||
|
|
|
@ -73,7 +73,12 @@ public class Calculator {
|
|||
while (Utils.allSolved(results) == false) {
|
||||
for (Function itm : results) {
|
||||
if (itm.isSolved() == false) {
|
||||
long t1 = System.currentTimeMillis();
|
||||
List<Function> dt = itm.solveOneStep();
|
||||
long t2 = System.currentTimeMillis();
|
||||
if (t2-t1 >= 3000) {
|
||||
throw new Error(Errors.TIMEOUT);
|
||||
}
|
||||
partialResults.addAll(dt);
|
||||
} else {
|
||||
partialResults.add(itm);
|
||||
|
|
|
@ -9,6 +9,7 @@ import org.warp.picalculator.math.rules.NumberRule1;
|
|||
import org.warp.picalculator.math.rules.NumberRule2;
|
||||
import org.warp.picalculator.math.rules.NumberRule6;
|
||||
import org.warp.picalculator.math.rules.SyntaxRule1;
|
||||
import org.warp.picalculator.math.rules.methods.MultiplicationMethod1;
|
||||
|
||||
public class Multiplication extends FunctionTwoValues {
|
||||
|
||||
|
@ -40,6 +41,7 @@ public class Multiplication extends FunctionTwoValues {
|
|||
if (NumberRule2.compare(this)) return true;
|
||||
if (NumberRule6.compare(this)) return true;
|
||||
if (ExponentRule15.compare(this)) return true;
|
||||
if (MultiplicationMethod1.compare(this)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -56,6 +58,8 @@ public class Multiplication extends FunctionTwoValues {
|
|||
result = NumberRule6.execute(this);
|
||||
} else if (ExponentRule15.compare(this)) {
|
||||
result = ExponentRule15.execute(this);
|
||||
} else if (MultiplicationMethod1.compare(this)) {
|
||||
result = MultiplicationMethod1.execute(this);
|
||||
} else if (variable1.isSolved() & variable2.isSolved()) {
|
||||
result.add(((Number)variable1).multiply((Number)variable2));
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import static org.warp.picalculator.device.graphicengine.Display.Render.glGetStr
|
|||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.nevec.rjm.BigDecimalMath;
|
||||
|
@ -289,4 +290,49 @@ public class Number implements Function {
|
|||
* return 6*toString().length()-1;
|
||||
* }
|
||||
*/
|
||||
|
||||
public boolean canBeFactorized() {
|
||||
if (Utils.isIntegerValue(getTerm())) {
|
||||
return getTerm().toBigIntegerExact().compareTo(BigInteger.valueOf(1)) > 1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public LinkedList<BigInteger> getFactors()
|
||||
{
|
||||
BigInteger n = getTerm().toBigIntegerExact();
|
||||
BigInteger two = BigInteger.valueOf(2);
|
||||
LinkedList<BigInteger> fs = new LinkedList<BigInteger>();
|
||||
|
||||
if (n.compareTo(two) < 0)
|
||||
{
|
||||
throw new IllegalArgumentException("must be greater than one");
|
||||
}
|
||||
|
||||
while (n.mod(two).equals(BigInteger.ZERO))
|
||||
{
|
||||
fs.add(two);
|
||||
n = n.divide(two);
|
||||
}
|
||||
|
||||
if (n.compareTo(BigInteger.ONE) > 0)
|
||||
{
|
||||
BigInteger f = BigInteger.valueOf(3);
|
||||
while (f.multiply(f).compareTo(n) <= 0)
|
||||
{
|
||||
if (n.mod(f).equals(BigInteger.ZERO))
|
||||
{
|
||||
fs.add(f);
|
||||
n = n.divide(f);
|
||||
}
|
||||
else
|
||||
{
|
||||
f = f.add(two);
|
||||
}
|
||||
}
|
||||
fs.add(n);
|
||||
}
|
||||
|
||||
return fs;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ import org.warp.picalculator.math.rules.NumberRule5;
|
|||
import org.warp.picalculator.math.rules.VariableRule1;
|
||||
import org.warp.picalculator.math.rules.VariableRule2;
|
||||
import org.warp.picalculator.math.rules.VariableRule3;
|
||||
import org.warp.picalculator.math.rules.methods.SumMethod1;
|
||||
|
||||
public class Subtraction extends FunctionTwoValues {
|
||||
|
||||
|
@ -40,6 +41,7 @@ public class Subtraction extends FunctionTwoValues {
|
|||
if (ExpandRule1.compare(this)) return true;
|
||||
if (ExpandRule5.compare(this)) return true;
|
||||
if (NumberRule5.compare(this)) return true;
|
||||
if (SumMethod1.compare(this)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -60,6 +62,8 @@ public class Subtraction extends FunctionTwoValues {
|
|||
result = ExpandRule5.execute(this);
|
||||
} else if (NumberRule5.compare(this)) {
|
||||
result = NumberRule5.execute(this);
|
||||
} else if (SumMethod1.compare(this)) {
|
||||
result = SumMethod1.execute(this);
|
||||
} else if (variable1.isSolved() & variable2.isSolved()) {
|
||||
result.add(((Number)variable1).add(((Number)variable2).multiply(new Number(this.parent, "-1"))));
|
||||
}
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
package org.warp.picalculator.math.rules.methods;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.Multiplication;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
|
||||
/**
|
||||
* Multiplication method<br>
|
||||
* <b>Example: X*3*X*2 = 6*X^2</b>
|
||||
* @author Andrea Cavalli
|
||||
*
|
||||
*/
|
||||
public class MultiplicationMethod1 {
|
||||
|
||||
public static boolean compare(Function f) {
|
||||
return ((Multiplication)f).getVariable1().isSolved() && ((Multiplication)f).getVariable2().isSolved() && !(((Multiplication)f).getVariable1() instanceof Number && ((Multiplication)f).getVariable2() instanceof Number) && getFirstWorkingMultiplicationCouple(getMultiplicationElements(f)) != null;
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
Function result;
|
||||
ArrayList<Function> elements = getMultiplicationElements(f);
|
||||
int[] workingElementCouple = getFirstWorkingMultiplicationCouple(elements);
|
||||
Function elem1 = elements.get(workingElementCouple[0]);
|
||||
Function elem2 = elements.get(workingElementCouple[1]);
|
||||
|
||||
final int size = elements.size();
|
||||
Function prec = new Multiplication(null, elem1, elem2);
|
||||
elem1.setParent(prec);
|
||||
elem2.setParent(prec);
|
||||
for (int i = size-1; i >= 0; i--) {
|
||||
if (i != workingElementCouple[0] & i != workingElementCouple[1]) {
|
||||
Function a = prec;
|
||||
Function b = elements.get(i);
|
||||
prec = new Multiplication(null, a, b);
|
||||
a.setParent(prec);
|
||||
b.setParent(prec);
|
||||
}
|
||||
}
|
||||
prec.setParent(f.getParent());
|
||||
|
||||
result = prec;
|
||||
|
||||
ArrayList<Function> results = new ArrayList<>();
|
||||
results.add(result);
|
||||
return results;
|
||||
}
|
||||
|
||||
private static ArrayList<Function> getMultiplicationElements(Function mult) {
|
||||
ArrayList<Function> elements = new ArrayList<>();
|
||||
while (mult instanceof Multiplication) {
|
||||
elements.add(((Multiplication) mult).getVariable1());
|
||||
mult = ((Multiplication) mult).getVariable2();
|
||||
}
|
||||
elements.add(mult);
|
||||
return elements;
|
||||
}
|
||||
|
||||
private static int[] getFirstWorkingMultiplicationCouple(ArrayList<Function> elements) {
|
||||
final int size = elements.size();
|
||||
Function a;
|
||||
Function b;
|
||||
if (elements.size() == 2) {
|
||||
return null;
|
||||
}
|
||||
for (int i = 0; i < size; i++) {
|
||||
a = elements.get(i);
|
||||
for (int j = 0; j < size; j++) {
|
||||
b = elements.get(j);
|
||||
if (i != j) {
|
||||
Function testFunc;
|
||||
testFunc = new Multiplication(null, a, b);
|
||||
if (!testFunc.isSolved()) {
|
||||
return new int[]{i, j};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue