2016-09-19 18:48:27 +02:00
|
|
|
package org.warp.picalculator.math.functions;
|
2016-09-12 16:30:51 +02:00
|
|
|
|
|
|
|
import java.math.BigInteger;
|
2016-09-18 14:33:25 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2016-09-12 16:30:51 +02:00
|
|
|
|
2016-09-19 18:48:27 +02:00
|
|
|
import org.warp.picalculator.Error;
|
|
|
|
import org.warp.picalculator.Errors;
|
2016-10-02 16:01:41 +02:00
|
|
|
import org.warp.picalculator.Utils;
|
2016-09-19 18:48:27 +02:00
|
|
|
import org.warp.picalculator.math.MathematicalSymbols;
|
|
|
|
import org.warp.picalculator.math.Variable;
|
2016-11-02 21:56:40 +01:00
|
|
|
import org.warp.picalculator.math.rules.NumberRule1;
|
|
|
|
import org.warp.picalculator.math.rules.NumberRule2;
|
2016-09-12 16:30:51 +02:00
|
|
|
|
2016-09-19 18:48:27 +02:00
|
|
|
public class Multiplication extends FunctionTwoValues {
|
|
|
|
|
2016-11-02 21:56:40 +01:00
|
|
|
public Multiplication(Function parent, Function value1, Function value2) {
|
|
|
|
super(parent, value1, value2);
|
2016-09-12 16:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getSymbol() {
|
|
|
|
return MathematicalSymbols.MULTIPLICATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-10-02 16:01:41 +02:00
|
|
|
protected boolean isSolvable() throws Error {
|
|
|
|
if (variable1 instanceof Number & variable2 instanceof Number) {
|
|
|
|
return true;
|
2016-09-18 14:33:25 +02:00
|
|
|
}
|
2016-11-02 21:56:40 +01:00
|
|
|
if (NumberRule1.compare(this)) return true;
|
|
|
|
if (NumberRule2.compare(this)) return true;
|
2016-10-02 16:01:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<Function> solveOneStep() throws Error {
|
2016-11-02 21:56:40 +01:00
|
|
|
List<Function> result = new ArrayList<>();
|
|
|
|
if (NumberRule1.compare(this)) {
|
|
|
|
result = NumberRule1.execute(this);
|
|
|
|
} else if (NumberRule2.compare(this)) {
|
|
|
|
result = NumberRule2.execute(this);
|
|
|
|
} else if (variable1.isSolved() & variable2.isSolved()) {
|
2016-09-18 14:33:25 +02:00
|
|
|
result.add(((Number)variable1).multiply((Number)variable2));
|
|
|
|
} else {
|
|
|
|
List<Function> l1 = new ArrayList<Function>();
|
|
|
|
List<Function> l2 = new ArrayList<Function>();
|
2016-10-02 16:01:41 +02:00
|
|
|
if (variable1.isSolved()) {
|
2016-09-18 14:33:25 +02:00
|
|
|
l1.add(variable1);
|
|
|
|
} else {
|
2016-10-02 16:01:41 +02:00
|
|
|
l1.addAll(variable1.solveOneStep());
|
|
|
|
}
|
|
|
|
if (variable2.isSolved()) {
|
2016-09-18 14:33:25 +02:00
|
|
|
l2.add(variable2);
|
2016-10-02 16:01:41 +02:00
|
|
|
} else {
|
|
|
|
l2.addAll(variable2.solveOneStep());
|
2016-09-18 14:33:25 +02:00
|
|
|
}
|
2016-10-02 16:01:41 +02:00
|
|
|
|
|
|
|
Function[][] results = Utils.joinFunctionsResults(l1, l2);
|
2016-09-18 14:33:25 +02:00
|
|
|
|
|
|
|
for (Function[] f : results) {
|
2016-11-02 21:56:40 +01:00
|
|
|
result.add(new Multiplication(this.parent, f[0], f[1]));
|
2016-09-18 14:33:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2016-09-12 16:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean drawSignum() {
|
|
|
|
Function[] tmpVar = new Function[] { variable1, variable2 };
|
|
|
|
boolean[] ok = new boolean[] { false, false };
|
|
|
|
for (int val = 0; val < 2; val++) {
|
|
|
|
while (!ok[val]) {
|
|
|
|
if (tmpVar[val] instanceof Division) {
|
|
|
|
ok[0] = true;
|
|
|
|
ok[1] = true;
|
|
|
|
} else if (tmpVar[val] instanceof Variable) {
|
|
|
|
ok[val] = true;
|
|
|
|
} else if (tmpVar[val] instanceof Number) {
|
|
|
|
if (val == 0) {
|
|
|
|
ok[val] = true;
|
|
|
|
} else {
|
|
|
|
if (!(tmpVar[0] instanceof Number)) {
|
|
|
|
ok[val] = true;
|
|
|
|
} else {
|
|
|
|
if (((Number) tmpVar[val]).term.isBigInteger(false)) { // TODO: prima era tmpVar[0], ma crashava. RICONTROLLARE! La logica potrebbe essere sbagliata
|
|
|
|
if (((Number) tmpVar[val]).term.toBigInteger(true).compareTo(new BigInteger("1")) == 0) {
|
|
|
|
if (((Number) tmpVar[val]).term.toNumeroAvanzato().getVariableY().count() > 0) {
|
|
|
|
ok[val] = true;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ok[val] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (tmpVar[val] instanceof Power) {
|
|
|
|
tmpVar[val] = ((Power) tmpVar[val]).variable1;
|
|
|
|
} else if (tmpVar[val] instanceof Root) {
|
2016-11-02 21:56:40 +01:00
|
|
|
if (val == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2016-09-12 16:30:51 +02:00
|
|
|
ok[val] = true;
|
|
|
|
} else if (tmpVar[val] instanceof RootSquare) {
|
2016-11-02 21:56:40 +01:00
|
|
|
if (val == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2016-09-12 16:30:51 +02:00
|
|
|
ok[val] = true;
|
2016-11-02 21:56:40 +01:00
|
|
|
} else if (tmpVar[val] instanceof Undefined) {
|
|
|
|
break;
|
|
|
|
} else if (tmpVar[val] instanceof Joke) {
|
|
|
|
break;
|
2016-09-12 16:30:51 +02:00
|
|
|
} else if (tmpVar[val] instanceof Expression) {
|
|
|
|
ok[0] = true;
|
|
|
|
ok[1] = true;
|
2016-09-19 18:48:27 +02:00
|
|
|
} else if (tmpVar[val] instanceof FunctionTwoValues) {
|
2016-09-12 16:30:51 +02:00
|
|
|
if (val == 0) {
|
2016-09-19 18:48:27 +02:00
|
|
|
tmpVar[val] = ((FunctionTwoValues) tmpVar[val]).variable2;
|
2016-09-12 16:30:51 +02:00
|
|
|
} else {
|
2016-09-19 18:48:27 +02:00
|
|
|
tmpVar[val] = ((FunctionTwoValues) tmpVar[val]).variable1;
|
2016-09-12 16:30:51 +02:00
|
|
|
}
|
2016-09-19 18:48:27 +02:00
|
|
|
} else if (tmpVar[val] instanceof AnteriorFunction) {
|
|
|
|
tmpVar[val] = ((AnteriorFunction) tmpVar[val]).variable;
|
2016-09-12 16:30:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ok[0] == true && ok[1] == true) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|