Finished fixing all the classes.

This commit is contained in:
XDrake99 2017-01-18 17:49:54 +01:00
parent ceb1304675
commit a8508eea8e
28 changed files with 129 additions and 95 deletions

View File

@ -41,10 +41,10 @@ public class Root extends FunctionTwoValues {
@Override
protected boolean isSolvable() {
if (variable1 instanceof Number & variable2 instanceof Number) {
if (variable1 instanceof Number & variable2 instanceof Number) {
if (root.exactMode == false) {
return true;
}
return true;
}
try {
Number exponent = new Number(root, BigDecimal.ONE);
exponent = exponent.divide((Number) variable1);
@ -55,7 +55,7 @@ public class Root extends FunctionTwoValues {
}
} catch (Exception | Error ex) {
ex.printStackTrace();
}
}
}
if (variable1 instanceof Number && ((Number)variable1).equals(new Number(root, 2))) {
return true;
@ -65,7 +65,7 @@ public class Root extends FunctionTwoValues {
@Override
public ArrayList<Function> solve() throws Error {
ArrayList<Function> result = new ArrayList<>();
ArrayList<Function> result = new ArrayList<>();
if (root.exactMode) {
if (variable1 instanceof Number && ((Number)variable1).equals(new Number(root, 2))) {
result.add(new RootSquare(root, variable2));
@ -78,7 +78,7 @@ public class Root extends FunctionTwoValues {
Number exp = (Number) variable1;
Number numb = (Number) variable2;
result.add(numb.pow(new Number(root, 1).divide(exp)));
result.add(numb.pow(new Number(root, 1).divide(exp)));
}
return result;
}

View File

@ -36,8 +36,8 @@ public class RootSquare extends AnteriorFunction {
@Override
protected boolean isSolvable() {
if (variable instanceof Number) {
if (root.exactMode == false) {
if (variable instanceof Number) {
if (root.exactMode == false) {
return true;
}
try {
@ -57,7 +57,7 @@ public class RootSquare extends AnteriorFunction {
@Override
public ArrayList<Function> solve() throws Error {
ArrayList<Function> result = new ArrayList<>();
ArrayList<Function> result = new ArrayList<>();
if (root.exactMode) {
Number exponent = new Number(root, BigInteger.ONE);
exponent = exponent.divide(new Number(root, 2));
@ -66,7 +66,7 @@ public class RootSquare extends AnteriorFunction {
Number exp = new Number(root, 2);
Number numb = (Number) variable;
result.add(numb.pow(new Number(root, 1).divide(exp)));
result.add(numb.pow(new Number(root, 1).divide(exp)));
}
return result;
}

View File

@ -74,14 +74,14 @@ public class Sum extends FunctionTwoValues {
} else if (SumMethod1.compare(this)) {
result = SumMethod1.execute(this);
} else if (variable1.isSolved() & variable2.isSolved()) {
if ((root.getChild().equals(this))) {
if ((root.getChild().equals(this))) {
if (((Number)variable1).term.compareTo(new BigDecimal(2)) == 0 && ((Number)variable2).term.compareTo(new BigDecimal(2)) == 0) {
result.add(new Joke(root, Joke.FISH));
return result;
} else if (((Number)variable1).term.compareTo(new BigDecimal(20)) == 0 && ((Number)variable2).term.compareTo(new BigDecimal(20)) == 0) {
result.add(new Joke(root, Joke.TORNADO));
return result;
} else if (((Number)variable1).term.compareTo(new BigDecimal(29)) == 0 && ((Number)variable2).term.compareTo(new BigDecimal(29)) == 0) {
} else if (((Number)variable1).term.compareTo(new BigDecimal(29)) == 0 && ((Number)variable2).term.compareTo(new BigDecimal(29)) == 0) {
result.add(new Joke(root, Joke.SHARKNADO));
return result;
}

View File

@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules;
import java.util.ArrayList;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Calculator;
import org.warp.picalculator.math.functions.Expression;
import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.Negative;
@ -39,10 +40,10 @@ public class ExpandRule5 {
if (f instanceof Negative) {
Negative fnc = (Negative) f;
result.add(((Negative)((Expression)fnc.getVariable()).getVariable(0)).getVariable().setParent(root));
result.add(((Negative)((Expression)fnc.getVariable()).getVariable(0)).getVariable());
} else if (f instanceof Subtraction) {
Subtraction fnc = (Subtraction) f;
result.add(((Negative)((Expression)fnc.getVariable2()).getVariable(0)).getVariable().setParent(root));
result.add(((Negative)((Expression)fnc.getVariable2()).getVariable(0)).getVariable());
}
return result;
}

View File

@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules;
import java.util.ArrayList;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Calculator;
import org.warp.picalculator.math.functions.Expression;
import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.Multiplication;
@ -29,13 +30,14 @@ public class ExponentRule16 {
}
public static ArrayList<Function> execute(Function f) throws Error {
Calculator root = f.getRoot();
ArrayList<Function> result = new ArrayList<>();
Multiplication fnc = (Multiplication) f;
Power p = new Power(fnc.getParent(), null, null);
Expression expr = new Expression(p);
Function a = fnc.getVariable1().setParent(expr);
Power p = new Power(fnc.getRoot(), null, null);
Expression expr = new Expression(root);
Function a = fnc.getVariable1();
expr.addFunctionToEnd(a);
Number two = new Number(p, 2);
Number two = new Number(root, 2);
p.setVariable1(expr);
p.setVariable2(two);
result.add(p);

View File

@ -17,7 +17,7 @@ public class ExponentRule2 {
public static boolean compare(Function f) {
Power fnc = (Power) f;
if (fnc.getVariable2().equals(new Number(root, 1))) {
if (fnc.getVariable2().equals(new Number(f.getRoot(), 1))) {
return true;
}
return false;
@ -25,7 +25,7 @@ public class ExponentRule2 {
public static ArrayList<Function> execute(Function f) throws Error {
ArrayList<Function> result = new ArrayList<>();
result.add(((Power)f).getVariable1().setParent(root));
result.add(((Power)f).getVariable1());
return result;
}

View File

@ -17,7 +17,7 @@ public class ExponentRule3 {
public static boolean compare(Function f) {
Power fnc = (Power) f;
if (fnc.getVariable2().equals(new Number(root, 0))) {
if (fnc.getVariable2().equals(new Number(f.getRoot(), 0))) {
return true;
}
return false;
@ -25,7 +25,7 @@ public class ExponentRule3 {
public static ArrayList<Function> execute(Function f) throws Error {
ArrayList<Function> result = new ArrayList<>();
result.add(new Number(f.getParent(), 1));
result.add(new Number(f.getRoot(), 1));
return result;
}

View File

@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules;
import java.util.ArrayList;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Calculator;
import org.warp.picalculator.math.functions.Expression;
import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.Multiplication;
@ -26,6 +27,7 @@ public class ExponentRule4 {
}
public static ArrayList<Function> execute(Function f) throws Error {
Calculator root = f.getRoot();
ArrayList<Function> result = new ArrayList<>();
Power fnc = (Power) f;
Expression expr = (Expression) fnc.getVariable1();
@ -33,14 +35,14 @@ public class ExponentRule4 {
Function a = mult.getVariable1();
Function b = mult.getVariable2();
Number n = (Number) fnc.getVariable2();
Multiplication retMult = new Multiplication(f.getParent(), null, null);
Power p1 = new Power(retMult, null, null);
Expression e1 = new Expression(p1);
Multiplication retMult = new Multiplication(root, null, null);
Power p1 = new Power(root, null, null);
Expression e1 = new Expression(root);
e1.addFunctionToEnd(a);
p1.setVariable1(e1);
p1.setVariable2(n);
Power p2 = new Power(retMult, null, null);
Expression e2 = new Expression(p2);
Power p2 = new Power(root, null, null);
Expression e2 = new Expression(root);
e2.addFunctionToEnd(b);
p2.setVariable1(e2);
p2.setVariable2(n);

View File

@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules;
import java.util.ArrayList;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Calculator;
import org.warp.picalculator.math.functions.Division;
import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.Number;
@ -16,25 +17,26 @@ import org.warp.picalculator.math.functions.Number;
public class FractionsRule1 {
public static boolean compare(Function f) {
Division fnc = (Division) f;
if (fnc.getVariable1() instanceof Number) {
Number numb1 = (Number) fnc.getVariable1();
if (numb1.equals(new Number(root, 0))) {
if (fnc.getVariable2() instanceof Number) {
Number numb2 = (Number) fnc.getVariable2();
if (numb2.equals(new Number(root, 0))) {
return false;
}
Calculator root = f.getRoot();
Division fnc = (Division) f;
if (fnc.getVariable1() instanceof Number) {
Number numb1 = (Number) fnc.getVariable1();
if (numb1.equals(new Number(root, 0))) {
if (fnc.getVariable2() instanceof Number) {
Number numb2 = (Number) fnc.getVariable2();
if (numb2.equals(new Number(root, 0))) {
return false;
}
return true;
}
return true;
}
}
return false;
}
public static ArrayList<Function> execute(Function f) throws Error {
ArrayList<Function> result = new ArrayList<>();
result.add(new Number(f.getParent(), 0));
result.add(new Number(f.getRoot(), 0));
return result;
}

View File

@ -19,7 +19,7 @@ public class FractionsRule2 {
Division fnc = (Division) f;
if (fnc.getVariable2() instanceof Number) {
Number numb = (Number) fnc.getVariable2();
if (numb.equals(new Number(root, 1))) {
if (numb.equals(new Number(f.getRoot(), 1))) {
return true;
}
}
@ -29,7 +29,7 @@ public class FractionsRule2 {
public static ArrayList<Function> execute(Function f) throws Error {
ArrayList<Function> result = new ArrayList<>();
Division fnc = (Division) f;
result.add(fnc.getVariable1().setParent(root));
result.add(fnc.getVariable1());
return result;
}

View File

@ -25,7 +25,7 @@ public class FractionsRule3 {
public static ArrayList<Function> execute(Function f) throws Error {
ArrayList<Function> result = new ArrayList<>();
result.add(new Number(f.getParent(), 1));
result.add(new Number(f.getRoot(), 1));
return result;
}

View File

@ -20,7 +20,7 @@ public class FractionsRule4 {
Power fnc = (Power) f;
if (fnc.getVariable1() instanceof Division && fnc.getVariable2() instanceof Number) {
Number n2 = (Number) fnc.getVariable2();
if (n2.equals(new Number(root, -1))) {
if (n2.equals(new Number(f.getRoot(), -1))) {
return true;
}
}
@ -32,7 +32,7 @@ public class FractionsRule4 {
Power fnc = (Power) f;
Function a = ((Division)fnc.getVariable1()).getVariable1();
Function b = ((Division)fnc.getVariable1()).getVariable2();
Division res = new Division(f.getParent(), b, a);
Division res = new Division(f.getRoot(), b, a);
result.add(res);
return result;
}

View File

@ -1,11 +1,12 @@
package org.warp.picalculator.math.rules;
import java.math.BigDecimal;
import java.util.ArrayList;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.functions.Division;
import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.Number;
package org.warp.picalculator.math.rules;
import java.math.BigDecimal;
import java.util.ArrayList;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Calculator;
import org.warp.picalculator.math.functions.Division;
import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.Number;
import org.warp.picalculator.math.functions.Power;
/**
@ -20,7 +21,7 @@ public class FractionsRule5 {
Power fnc = (Power) f;
if (fnc.getVariable1() instanceof Division && fnc.getVariable2() instanceof Number) {
Number n2 = (Number) fnc.getVariable2();
if (n2.getTerm().compareTo(BigDecimal.ZERO) < 0) {
if (n2.getTerm().compareTo(BigDecimal.ZERO) < 0) {
return true;
}
}
@ -28,14 +29,14 @@ public class FractionsRule5 {
}
public static ArrayList<Function> execute(Function f) throws Error {
Calculator root = f.getRoot();
ArrayList<Function> result = new ArrayList<>();
Power fnc = (Power) f;
Function a = ((Division)fnc.getVariable1()).getVariable1();
Function b = ((Division)fnc.getVariable1()).getVariable2();
Function c = ((Number)fnc.getVariable2()).multiply(new Number(root, "-1"));
Division dv = new Division(root, b, a);
Power pow = new Power(f.getParent(), dv, c);
dv.setParent(pow);
Power pow = new Power(root, dv, c);
result.add(pow);
return result;
}

View File

@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules;
import java.util.ArrayList;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Calculator;
import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.Multiplication;
import org.warp.picalculator.math.functions.Number;
@ -16,6 +17,7 @@ import org.warp.picalculator.math.functions.Number;
public class NumberRule1 {
public static boolean compare(Function f) {
Calculator root = f.getRoot();
Multiplication mult = (Multiplication) f;
if (mult.getVariable1() instanceof Number) {
Number numb = (Number) mult.getVariable1();
@ -34,7 +36,7 @@ public class NumberRule1 {
public static ArrayList<Function> execute(Function f) throws Error {
ArrayList<Function> result = new ArrayList<>();
result.add(new Number(f.getParent(), "0"));
result.add(new Number(f.getRoot(), "0"));
return result;
}

View File

@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules;
import java.util.ArrayList;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Calculator;
import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.Multiplication;
import org.warp.picalculator.math.functions.Number;
@ -16,6 +17,7 @@ import org.warp.picalculator.math.functions.Number;
public class NumberRule2 {
public static boolean compare(Function f) {
Calculator root = f.getRoot();
Multiplication mult = (Multiplication) f;
if (mult.getVariable1() instanceof Number) {
Number numb = (Number) mult.getVariable1();
@ -33,6 +35,7 @@ public class NumberRule2 {
}
public static ArrayList<Function> execute(Function f) throws Error {
Calculator root = f.getRoot();
ArrayList<Function> result = new ArrayList<>();
Function a = null;
boolean aFound = false;

View File

@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules;
import java.util.ArrayList;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Calculator;
import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.Multiplication;
import org.warp.picalculator.math.functions.Negative;
@ -45,14 +46,15 @@ public class NumberRule3 {
}
public static ArrayList<Function> execute(Function f) throws Error {
Calculator root = f.getRoot();
ArrayList<Function> result = new ArrayList<>();
if (f instanceof SumSubtraction) {
Multiplication mul = new Multiplication(f.getParent(), null, null);
Multiplication mul = new Multiplication(root, null, null);
mul.setVariable1(new Number(root, 2));
mul.setVariable2(f);
result.add(mul);
}
result.add(new Number(f.getParent(), 0));
result.add(new Number(root, 0));
return result;
}

View File

@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules;
import java.util.ArrayList;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Calculator;
import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.Subtraction;
import org.warp.picalculator.math.functions.Sum;
@ -24,10 +25,11 @@ public class NumberRule4 {
}
public static ArrayList<Function> execute(Function f) throws Error {
Calculator root = f.getRoot();
ArrayList<Function> result = new ArrayList<>();
SumSubtraction ss = (SumSubtraction) f;
result.add(new Sum(f.getParent(), ss.getVariable1(), ss.getVariable2()));
result.add(new Subtraction(f.getParent(), ss.getVariable1(), ss.getVariable2()));
result.add(new Sum(root, ss.getVariable1(), ss.getVariable2()));
result.add(new Subtraction(root, ss.getVariable1(), ss.getVariable2()));
return result;
}

View File

@ -22,6 +22,7 @@ import org.warp.picalculator.math.functions.Number;
public class NumberRule5 {
public static boolean compare(Function f) {
Calculator root = f.getRoot();
FunctionTwoValues fnc = (FunctionTwoValues) f;
if (fnc.getVariable1().equals(new Number(root, 0)) || fnc.getVariable2().equals(new Number(root, 0))) {
return true;

View File

@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules;
import java.util.ArrayList;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Calculator;
import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.Multiplication;
import org.warp.picalculator.math.functions.Negative;
@ -17,6 +18,7 @@ import org.warp.picalculator.math.functions.Number;
public class NumberRule6 {
public static boolean compare(Function f) {
Calculator root = f.getRoot();
Multiplication mult = (Multiplication) f;
if (mult.getVariable1() instanceof Number) {
Number numb = (Number) mult.getVariable1();
@ -34,6 +36,7 @@ public class NumberRule6 {
}
public static ArrayList<Function> execute(Function f) throws Error {
Calculator root = f.getRoot();
ArrayList<Function> result = new ArrayList<>();
Function a = null;
boolean aFound = false;
@ -53,7 +56,7 @@ public class NumberRule6 {
}
}
Negative minus = new Negative(f.getParent(), null);
Negative minus = new Negative(root, null);
minus.setVariable(a);
result.add(minus);

View File

@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules;
import java.util.ArrayList;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Calculator;
import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.FunctionTwoValues;
import org.warp.picalculator.math.functions.Multiplication;
@ -27,6 +28,7 @@ public class SyntaxRule1 {
}
public static ArrayList<Function> execute(Function f) throws Error {
Calculator root = f.getRoot();
ArrayList<Function> result = new ArrayList<>();
FunctionTwoValues mOut = (FunctionTwoValues) f;
Function a = ((FunctionTwoValues)mOut.getVariable1()).getVariable1();
@ -34,9 +36,9 @@ public class SyntaxRule1 {
Function c = mOut.getVariable2();
FunctionTwoValues mIn;
if (f instanceof Multiplication) {
mIn = new Multiplication(mOut, null, null);
mIn = new Multiplication(root, null, null);
} else {
mIn = new Sum(mOut, null, null);
mIn = new Sum(root, null, null);
}
mOut.setVariable1(a);
mIn.setVariable1(b);

View File

@ -29,6 +29,7 @@ public class SyntaxRule2 {
}
public static ArrayList<Function> execute(Sum f) throws Error {
Calculator root = f.getRoot();
ArrayList<Function> result = new ArrayList<>();
Function a = f.getVariable1();
Function b, c;
@ -39,7 +40,7 @@ public class SyntaxRule2 {
b = ((Sum)((Expression)f.getVariable2()).getVariable(0)).getVariable1();
c = ((Sum)((Expression)f.getVariable2()).getVariable(0)).getVariable2();
}
Sum mIn = new Sum(f, null, null);
Sum mIn = new Sum(root, null, null);
f.setVariable1(mIn);
mIn.setVariable1(a);
mIn.setVariable2(b);

View File

@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules;
import java.util.ArrayList;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Calculator;
import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.Number;
import org.warp.picalculator.math.functions.Power;
@ -17,6 +18,7 @@ import org.warp.picalculator.math.functions.Undefined;
public class UndefinedRule1 {
public static boolean compare(Function f) {
Calculator root = f.getRoot();
Power fnc = (Power) f;
if (fnc.getVariable1().equals(new Number(root, 0)) && fnc.getVariable2().equals(new Number(root, 0))) {
return true;
@ -25,6 +27,7 @@ public class UndefinedRule1 {
}
public static ArrayList<Function> execute(Function f) throws Error {
Calculator root = f.getRoot();
ArrayList<Function> result = new ArrayList<>();
result.add(new Undefined(root));
return result;

View File

@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules;
import java.util.ArrayList;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Calculator;
import org.warp.picalculator.math.functions.Division;
import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.Number;
@ -17,6 +18,7 @@ import org.warp.picalculator.math.functions.Undefined;
public class UndefinedRule2 {
public static boolean compare(Function f) {
Calculator root = f.getRoot();
Division fnc = (Division) f;
if (fnc.getVariable2() instanceof Number) {
Number numb = (Number) fnc.getVariable2();
@ -28,6 +30,7 @@ public class UndefinedRule2 {
}
public static ArrayList<Function> execute(Function f) throws Error {
Calculator root = f.getRoot();
ArrayList<Function> result = new ArrayList<>();
result.add(new Undefined(root));
return result;

View File

@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules;
import java.util.ArrayList;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Calculator;
import org.warp.picalculator.math.functions.Expression;
import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.FunctionTwoValues;
@ -30,6 +31,7 @@ public class VariableRule1 {
}
public static ArrayList<Function> execute(FunctionTwoValues fnc) throws Error {
Calculator root = fnc.getRoot();
ArrayList<Function> result = new ArrayList<>();
Multiplication m1 = (Multiplication) fnc.getVariable1();
Multiplication m2 = (Multiplication) fnc.getVariable2();
@ -37,13 +39,13 @@ public class VariableRule1 {
Function b = m2.getVariable1();
Function x = m1.getVariable2();
Multiplication retm = new Multiplication(fnc.getParent(), null, null);
Expression rete = new Expression(retm);
Multiplication retm = new Multiplication(root, null, null);
Expression rete = new Expression(root);
FunctionTwoValues rets;
if (fnc instanceof Sum){
rets = new Sum(rete, null, null);
rets = new Sum(root, null, null);
} else {
rets = new Subtraction(rete, null, null);
rets = new Subtraction(root, null, null);
}
rets.setVariable1(a);
rets.setVariable2(b);

View File

@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules;
import java.util.ArrayList;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Calculator;
import org.warp.picalculator.math.functions.Expression;
import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.FunctionTwoValues;
@ -30,22 +31,23 @@ public class VariableRule2 {
}
public static ArrayList<Function> execute(FunctionTwoValues fnc) throws Error {
Calculator root = fnc.getRoot();
ArrayList<Function> result = new ArrayList<>();
Multiplication m1 = (Multiplication) fnc.getVariable1();
Function a = m1.getVariable1();
Function x = fnc.getVariable2();
Multiplication retm = new Multiplication(fnc.getParent(), null, null);
Expression rete = new Expression(retm);
Multiplication retm = new Multiplication(root, null, null);
Expression rete = new Expression(root);
FunctionTwoValues rets;
if (fnc instanceof Sum) {
rets = new Sum(rete, null, null);
rets = new Sum(root, null, null);
} else {
rets = new Subtraction(rete, null, null);
rets = new Subtraction(root, null, null);
}
rets.setVariable1(a);
rets.setVariable2(new Number(rets, 1));
rets.setVariable2(new Number(root, 1));
rete.addFunctionToEnd(rets);
retm.setVariable1(rete);
retm.setVariable2(x);

View File

@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules;
import java.util.ArrayList;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Calculator;
import org.warp.picalculator.math.functions.Expression;
import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.FunctionTwoValues;
@ -30,21 +31,22 @@ public class VariableRule3 {
}
public static ArrayList<Function> execute(FunctionTwoValues fnc) throws Error {
Calculator root = fnc.getRoot();
ArrayList<Function> result = new ArrayList<>();
Multiplication m2 = (Multiplication) fnc.getVariable2();
Function a = m2.getVariable1();
Function x = fnc.getVariable1();
Multiplication retm = new Multiplication(fnc.getParent(), null, null);
Expression rete = new Expression(retm);
Multiplication retm = new Multiplication(root, null, null);
Expression rete = new Expression(root);
FunctionTwoValues rets;
if (fnc instanceof Sum) {
rets = new Sum(rete, null, null);
rets = new Sum(root, null, null);
} else {
rets = new Subtraction(rete, null, null);
rets = new Subtraction(root, null, null);
}
rets.setVariable1(new Number(rets, 1));
rets.setVariable1(new Number(root, 1));
rets.setVariable2(a);
rete.addFunctionToEnd(rets);
retm.setVariable1(rete);

View File

@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules.methods;
import java.util.ArrayList;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Calculator;
import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.Multiplication;
import org.warp.picalculator.math.functions.Number;
@ -21,6 +22,7 @@ public class MultiplicationMethod1 {
public static ArrayList<Function> execute(Function f) throws Error {
Function result;
Calculator root = f.getRoot();
ArrayList<Function> elements = getMultiplicationElements(f);
int[] workingElementCouple = getFirstWorkingMultiplicationCouple(elements);
Function elem1 = elements.get(workingElementCouple[0]);
@ -28,18 +30,13 @@ public class MultiplicationMethod1 {
final int size = elements.size();
Function prec = new Multiplication(root, 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(root, a, b);
a.setParent(prec);
b.setParent(prec);
}
}
prec.setParent(root);
result = prec;
@ -62,9 +59,13 @@ public class MultiplicationMethod1 {
final int size = elements.size();
Function a;
Function b;
if (elements.size() == 0) {
return null;
}
if (elements.size() == 2) {
return null;
}
Calculator root = elements.get(0).getRoot();
for (int i = 0; i < size; i++) {
a = elements.get(i);
for (int j = 0; j < size; j++) {

View File

@ -4,6 +4,7 @@ import java.math.BigDecimal;
import java.util.ArrayList;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Calculator;
import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.FunctionTwoValues;
import org.warp.picalculator.math.functions.Negative;
@ -20,40 +21,35 @@ import org.warp.picalculator.math.functions.Sum;
public class SumMethod1 {
public static boolean compare(Function f) {
return (f instanceof Sum || f instanceof Subtraction) && ((FunctionTwoValues)f).getVariable1().isSolved() && ((FunctionTwoValues)f).getVariable2().isSolved() && !(((FunctionTwoValues)f).getVariable1() instanceof Number && ((FunctionTwoValues)f).getVariable2() instanceof Number) && getFirstWorkingSumCouple(getSumElements(f)) != null;
Calculator root = f.getRoot();
return (f instanceof Sum || f instanceof Subtraction) && ((FunctionTwoValues)f).getVariable1().isSolved() && ((FunctionTwoValues)f).getVariable2().isSolved() && !(((FunctionTwoValues)f).getVariable1() instanceof Number && ((FunctionTwoValues)f).getVariable2() instanceof Number) && getFirstWorkingSumCouple(root, getSumElements(f)) != null;
}
public static ArrayList<Function> execute(Function f) throws Error {
Function result;
Calculator root = f.getRoot();
ArrayList<Function> elements = getSumElements(f);
int[] workingElementCouple = getFirstWorkingSumCouple(elements);
int[] workingElementCouple = getFirstWorkingSumCouple(root, elements);
Function elem1 = elements.get(workingElementCouple[0]);
Function elem2 = elements.get(workingElementCouple[1]);
final int size = elements.size();
Function prec = new Sum(root, 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);
if (b instanceof Negative) {
prec = new Subtraction(root, a, ((Negative)b).getVariable());
a.setParent(prec);
((FunctionTwoValues)prec).getVariable2().setParent(prec);
((FunctionTwoValues)prec).getVariable2();
} else if (b instanceof Number && ((Number) b).getTerm().compareTo(BigDecimal.ZERO) < 0) {
prec = new Subtraction(root, a, ((Number)b).multiply(new Number(root, -1)));
a.setParent(prec);
((FunctionTwoValues)prec).getVariable2().setParent(prec);
((FunctionTwoValues)prec).getVariable2();
} else {
prec = new Sum(root, a, b);
a.setParent(prec);
b.setParent(prec);
}
}
}
prec.setParent(root);
result = prec;
@ -63,6 +59,7 @@ public class SumMethod1 {
}
private static ArrayList<Function> getSumElements(Function sum) {
Calculator root = sum.getRoot();
ArrayList<Function> elements = new ArrayList<>();
while (sum instanceof Sum || sum instanceof Subtraction) {
if (sum instanceof Sum) {
@ -76,7 +73,7 @@ public class SumMethod1 {
return elements;
}
private static int[] getFirstWorkingSumCouple(ArrayList<Function> elements) {
private static int[] getFirstWorkingSumCouple(Calculator root, ArrayList<Function> elements) {
final int size = elements.size();
Function a;
Function b;
@ -95,7 +92,7 @@ public class SumMethod1 {
testFunc = new Subtraction(root, a, ((Number)b).multiply(new Number(root, -1)));
} else if (a instanceof Negative) {
testFunc = new Subtraction(root, b, ((Negative)a).getVariable());
} else if (a instanceof Number && ((Number) a).getTerm().compareTo(BigDecimal.ZERO) < 0) {
} else if (a instanceof Number && ((Number) a).getTerm().compareTo(BigDecimal.ZERO) < 0) {
testFunc = new Subtraction(root, b, ((Number)a).multiply(new Number(root, -1)));
} else {
testFunc = new Sum(root, a, b);