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

@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules;
import java.util.ArrayList; import java.util.ArrayList;
import org.warp.picalculator.Error; import org.warp.picalculator.Error;
import org.warp.picalculator.math.Calculator;
import org.warp.picalculator.math.functions.Expression; import org.warp.picalculator.math.functions.Expression;
import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.Negative; import org.warp.picalculator.math.functions.Negative;
@ -39,10 +40,10 @@ public class ExpandRule5 {
if (f instanceof Negative) { if (f instanceof Negative) {
Negative fnc = (Negative) f; 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) { } else if (f instanceof Subtraction) {
Subtraction fnc = (Subtraction) f; 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; return result;
} }

View File

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

View File

@ -17,7 +17,7 @@ public class ExponentRule2 {
public static boolean compare(Function f) { public static boolean compare(Function f) {
Power fnc = (Power) 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 true;
} }
return false; return false;
@ -25,7 +25,7 @@ public class ExponentRule2 {
public static ArrayList<Function> execute(Function f) throws Error { public static ArrayList<Function> execute(Function f) throws Error {
ArrayList<Function> result = new ArrayList<>(); ArrayList<Function> result = new ArrayList<>();
result.add(((Power)f).getVariable1().setParent(root)); result.add(((Power)f).getVariable1());
return result; return result;
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -3,6 +3,7 @@ import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import org.warp.picalculator.Error; import org.warp.picalculator.Error;
import org.warp.picalculator.math.Calculator;
import org.warp.picalculator.math.functions.Division; import org.warp.picalculator.math.functions.Division;
import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.Number; import org.warp.picalculator.math.functions.Number;
@ -28,14 +29,14 @@ public class FractionsRule5 {
} }
public static ArrayList<Function> execute(Function f) throws Error { public static ArrayList<Function> execute(Function f) throws Error {
Calculator root = f.getRoot();
ArrayList<Function> result = new ArrayList<>(); ArrayList<Function> result = new ArrayList<>();
Power fnc = (Power) f; Power fnc = (Power) f;
Function a = ((Division)fnc.getVariable1()).getVariable1(); Function a = ((Division)fnc.getVariable1()).getVariable1();
Function b = ((Division)fnc.getVariable1()).getVariable2(); Function b = ((Division)fnc.getVariable1()).getVariable2();
Function c = ((Number)fnc.getVariable2()).multiply(new Number(root, "-1")); Function c = ((Number)fnc.getVariable2()).multiply(new Number(root, "-1"));
Division dv = new Division(root, b, a); Division dv = new Division(root, b, a);
Power pow = new Power(f.getParent(), dv, c); Power pow = new Power(root, dv, c);
dv.setParent(pow);
result.add(pow); result.add(pow);
return result; return result;
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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