Added angle functions

This commit is contained in:
XDrake99 2016-12-04 22:22:16 +01:00
parent 09387176bb
commit 29bbe47439
29 changed files with 565 additions and 237 deletions

View File

@ -9,5 +9,5 @@ encoding//src/org/warp/picalculator/math/functions/Expression.java=UTF-8
encoding//src/org/warp/picalculator/math/functions/Root.java=UTF-8
encoding//src/org/warp/picalculator/math/functions/RootSquare.java=UTF-8
encoding//src/org/warp/picalculator/math/functions/Sum.java=UTF-8
encoding//src/org/warp/picalculator/screens/EquationScreen.java=UTF-8
encoding//src/org/warp/picalculator/screens/MathInputScreen.java=UTF-8
encoding/<project>=UTF-8

Binary file not shown.

Binary file not shown.

View File

@ -475,4 +475,12 @@ public class Utils {
}
return false;
}
public static CharSequence multipleChars(String string, int i) {
String result = "";
for (int j = 0; j < i; j++) {
result+=string;
}
return result;
}
}

View File

@ -491,7 +491,14 @@ public class Keyboard {
}
public static enum Key {
POWER, debug_DEG, debug_RAD, debug_GRA, SHIFT, ALPHA, NONE, HISTORY_BACK, HISTORY_FORWARD, SURD_MODE, DRG_CYCLE, LETTER_X, LETTER_Y, SIMPLIFY, SOLVE, BRIGHTNESS_CYCLE, BRIGHTNESS_CYCLE_REVERSE, DOT, NUM0, NUM1, NUM2, NUM3, NUM4, NUM5, NUM6, NUM7, NUM8, NUM9, PARENTHESIS_OPEN, PARENTHESIS_CLOSE, PLUS, MINUS, PLUS_MINUS, MULTIPLY, DIVIDE, EQUAL, DELETE, RESET, LEFT, RIGHT, UP, DOWN, OK, debug1, debug2, debug3, debug4, debug5, SQRT, ROOT, POWER_OF_2, POWER_OF_x
POWER, debug_DEG, debug_RAD, debug_GRA, SHIFT, ALPHA, NONE,
HISTORY_BACK, HISTORY_FORWARD, SURD_MODE, DRG_CYCLE,
LETTER_X, LETTER_Y, SIMPLIFY, SOLVE, BRIGHTNESS_CYCLE,
BRIGHTNESS_CYCLE_REVERSE, DOT, NUM0, NUM1, NUM2, NUM3, NUM4, NUM5, NUM6, NUM7, NUM8, NUM9,
PARENTHESIS_OPEN, PARENTHESIS_CLOSE, PLUS, MINUS, PLUS_MINUS, MULTIPLY, DIVIDE, EQUAL,
DELETE, RESET, LEFT, RIGHT, UP, DOWN, OK, debug1, debug2, debug3, debug4, debug5,
SQRT, ROOT, POWER_OF_2, POWER_OF_x,
SINE, COSINE, TANGENT, ARCSINE, ARCCOSINE, ARCTANGENT
}
}

View File

@ -69,6 +69,33 @@ public class Frame extends JFrame {
case KeyEvent.VK_ESCAPE:
Keyboard.keyPressed(Key.POWER);
break;
case KeyEvent.VK_S:
if (Keyboard.shift) {
Keyboard.keyPressed(Key.ARCSINE);
} else if (Keyboard.alpha) {
Keyboard.keyPressed(Key.NONE);
} else {
Keyboard.keyPressed(Key.SINE);
}
break;
case KeyEvent.VK_C:
if (Keyboard.shift) {
Keyboard.keyPressed(Key.ARCCOSINE);
} else if (Keyboard.alpha) {
Keyboard.keyPressed(Key.NONE);
} else {
Keyboard.keyPressed(Key.COSINE);
}
break;
case KeyEvent.VK_T:
if (Keyboard.shift) {
Keyboard.keyPressed(Key.ARCTANGENT);
} else if (Keyboard.alpha) {
Keyboard.keyPressed(Key.NONE);
} else {
Keyboard.keyPressed(Key.TANGENT);
}
break;
case KeyEvent.VK_D:
if (!Keyboard.shift && !Keyboard.alpha) {
Keyboard.keyPressed(Key.debug_DEG);
@ -201,15 +228,6 @@ public class Frame extends JFrame {
Keyboard.keyPressed(Key.NONE);
}
break;
case KeyEvent.VK_S:
if (!Keyboard.shift && !Keyboard.alpha) {
Keyboard.keyPressed(Key.SURD_MODE);
} else if (Keyboard.shift) {
Keyboard.keyPressed(Key.NONE);
} else {
Keyboard.keyPressed(Key.NONE);
}
break;
case KeyEvent.VK_ADD:
if (!Keyboard.shift && !Keyboard.alpha) {
Keyboard.keyPressed(Key.PLUS);

View File

@ -17,7 +17,7 @@ import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.Number;
import org.warp.picalculator.math.functions.equations.Equation;
import org.warp.picalculator.math.functions.equations.EquationsSystem;
import org.warp.picalculator.screens.EquationScreen;
import org.warp.picalculator.screens.MathInputScreen;
import org.warp.picalculator.screens.SolveEquationScreen;
public class Calculator {
@ -88,7 +88,7 @@ public class Calculator {
return results;
}
public static void solve(EquationScreen es) throws Error {
public static void solve(MathInputScreen es) throws Error {
for (Function f : es.f) {
if (f instanceof Equation) {
PIDisplay.INSTANCE.setScreen(new SolveEquationScreen(es));
@ -134,7 +134,7 @@ public class Calculator {
}
}*/
public static void simplify(EquationScreen es) throws Error {
public static void simplify(MathInputScreen es) throws Error {
ArrayList<Function> results = new ArrayList<>();
ArrayList<Function> partialResults = new ArrayList<>();
for (Function f : es.f2) {

View File

@ -18,6 +18,12 @@ public class MathematicalSymbols {
public static final String POWER = "";
public static final String EQUATION = "=";
public static final String SYSTEM = "{";
public static final String SINE = "";
public static final String COSINE = "";
public static final String TANGENT = "";
public static final String ARC_SINE = "";
public static final String ARC_COSINE = "";
public static final String ARC_TANGENT = "";
public static final String[] functions() {
return concat(functionsNSN(), functionsSN());
@ -28,7 +34,7 @@ public class MathematicalSymbols {
}
public static final String[] functionsSN() {
return new String[] { SQUARE_ROOT, MINUS };
return new String[] { SQUARE_ROOT, MINUS, SINE, COSINE, TANGENT, ARC_SINE, ARC_COSINE, ARC_TANGENT };
}
public static final String[] signums(boolean withMultiplication) {
@ -57,8 +63,8 @@ public class MathematicalSymbols {
.replace("", "SIN")
.replace("", "COS")
.replace("", "TAN")
.replace("", "SIN-1")
.replace("", "COS-1")
.replace("", "TAN-1");
.replace("", "ASIN")
.replace("", "ACOS")
.replace("", "ATAN");
}
}

View File

@ -4,9 +4,11 @@ import static org.warp.picalculator.device.graphicengine.Display.Render.glDrawSt
import static org.warp.picalculator.device.graphicengine.Display.Render.glGetStringWidth;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import org.warp.picalculator.Error;
import org.warp.picalculator.Errors;
import org.warp.picalculator.Utils;
import org.warp.picalculator.device.graphicengine.Display;
@ -18,6 +20,8 @@ public abstract class AnteriorFunction implements Function {
setVariable(value);
}
protected abstract Function NewInstance(Function parent2, Function f);
protected Function parent;
protected Function variable = new Number(null, BigInteger.ZERO);
protected int width;
@ -46,7 +50,29 @@ public abstract class AnteriorFunction implements Function {
public abstract String getSymbol();
@Override
public abstract List<Function> solveOneStep() throws Error;
public final ArrayList<Function> solveOneStep() throws Error {
final boolean solved = variable.isSolved();
ArrayList<Function> result = solved?solve():null;
if (result == null || result.isEmpty()) {
result = new ArrayList<>();
ArrayList<Function> l1 = new ArrayList<Function>();
if (variable.isSolved()) {
l1.add(variable);
} else {
l1.addAll(variable.solveOneStep());
}
for (Function f : l1) {
result.add(NewInstance(this.parent, (Function)f));
}
}
return result;
}
protected abstract ArrayList<Function> solve() throws Error;
@Override
public boolean isSolved() {

View File

@ -24,6 +24,11 @@ public class Division extends FunctionTwoValues {
super(parent, value1, value2);
}
@Override
protected Function NewInstance(Function parent2, Function value1, Function value2) {
return new Division(parent, value1, value2);
}
@Override
public String getSymbol() {
return MathematicalSymbols.DIVISION;
@ -40,7 +45,7 @@ public class Division extends FunctionTwoValues {
}
@Override
public List<Function> solveOneStep() throws Error {
public ArrayList<Function> solve() throws Error {
ArrayList<Function> result = new ArrayList<>();
if (FractionsRule1.compare(this)) {
result = FractionsRule1.execute(this);
@ -50,25 +55,6 @@ public class Division extends FunctionTwoValues {
result = FractionsRule3.execute(this);
} else if (UndefinedRule2.compare(this)) {
result = UndefinedRule2.execute(this);
} else {
List<Function> l1 = new ArrayList<Function>();
List<Function> l2 = new ArrayList<Function>();
if (variable1.isSolved()) {
l1.add(variable1);
} else {
l1.addAll(variable1.solveOneStep());
}
if (variable2.isSolved()) {
l2.add(variable2);
} else {
l2.addAll(variable2.solveOneStep());
}
Function[][] results = Utils.joinFunctionsResults(l1, l2);
for (Function[] f : results) {
result.add(new Division(this.parent, (Function)f[0], (Function)f[1]));
}
}
return result;
}

View File

@ -16,6 +16,12 @@ import org.warp.picalculator.Error;
import org.warp.picalculator.Errors;
import org.warp.picalculator.Utils;
import org.warp.picalculator.math.MathematicalSymbols;
import org.warp.picalculator.math.functions.trigonometry.ArcCosine;
import org.warp.picalculator.math.functions.trigonometry.ArcSine;
import org.warp.picalculator.math.functions.trigonometry.ArcTangent;
import org.warp.picalculator.math.functions.trigonometry.Cosine;
import org.warp.picalculator.math.functions.trigonometry.Sine;
import org.warp.picalculator.math.functions.trigonometry.Tangent;
public class Expression extends FunctionMultipleValues {
@ -162,18 +168,18 @@ public class Expression extends FunctionMultipleValues {
Utils.debug.println(debugSpaces + "•Resolved signs:" + processExpression);
}
// Aggiungi le parentesi implicite per le potenze con una incognita
pattern = Pattern.compile("(?<!(?:\\(|^))(["+Utils.ArrayToRegex(MathematicalSymbols.variables())+"]+"+MathematicalSymbols.POWER+"[^" + Utils.ArrayToRegex(Utils.add(concat(MathematicalSymbols.functionsNSN(), concat(MathematicalSymbols.signums(true), MathematicalSymbols.genericSyntax())), ")")) + "])(?!\\))");
matcher = pattern.matcher(processExpression);
symbolsChanged = false;
while (matcher.find()) {
symbolsChanged = true;
String correzione = "("+matcher.group().replace(MathematicalSymbols.POWER, "")+")";
processExpression = processExpression.substring(0, matcher.start(0)) + correzione + processExpression.substring(matcher.start(0) + matcher.group(0).length(), processExpression.length());
matcher = pattern.matcher(processExpression);
}
processExpression = processExpression.replace("", MathematicalSymbols.POWER);
// // Aggiungi le parentesi implicite per le potenze con una incognita
// pattern = Pattern.compile("(?<!(?:\\(|^))(["+Utils.ArrayToRegex(MathematicalSymbols.variables())+"]+"+MathematicalSymbols.POWER+"[^" + Utils.ArrayToRegex(Utils.add(concat(MathematicalSymbols.functionsNSN(), concat(MathematicalSymbols.signums(true), MathematicalSymbols.genericSyntax())), ")")) + "])(?!\\))");
// matcher = pattern.matcher(processExpression);
// symbolsChanged = false;
// while (matcher.find()) {
// symbolsChanged = true;
// String correzione = matcher.group().replace(MathematicalSymbols.POWER, "");
// processExpression = processExpression.substring(0, matcher.start(0)) + correzione + processExpression.substring(matcher.start(0) + matcher.group(0).length(), processExpression.length());
// matcher = pattern.matcher(processExpression);
// }
//
// processExpression = processExpression.replace("", MathematicalSymbols.POWER);
// Aggiungi i segni * accanto alle parentesi
pattern = Pattern.compile("\\([^\\(]+?\\)");
@ -247,6 +253,24 @@ public class Expression extends FunctionMultipleValues {
case MathematicalSymbols.POWER:
f = new Power(this, null, null);
break;
case MathematicalSymbols.SINE:
f = new Sine(this, null);
break;
case MathematicalSymbols.COSINE:
f = new Cosine(this, null);
break;
case MathematicalSymbols.TANGENT:
f = new Tangent(this, null);
break;
case MathematicalSymbols.ARC_SINE:
f = new ArcSine(this, null);
break;
case MathematicalSymbols.ARC_COSINE:
f = new ArcCosine(this, null);
break;
case MathematicalSymbols.ARC_TANGENT:
f = new ArcTangent(this, null);
break;
case MathematicalSymbols.PARENTHESIS_OPEN:
// Find the last closed parenthesis
// cerca l'ultima parentesi chiusa
@ -557,7 +581,11 @@ public class Expression extends FunctionMultipleValues {
}
} while (((oldFunctionsList.size() != before || step != "sums") && oldFunctionsList.size() > 1));
}
setVariables(oldFunctionsList);
if(oldFunctionsList.isEmpty()) {
setVariables(new Function[]{new Number(this, 0)});
} else {
setVariables(oldFunctionsList);
}
dsl = debugSpaces.length();
debugSpaces = "";
@ -641,7 +669,9 @@ public class Expression extends FunctionMultipleValues {
} else {
if (functions.length == 1) {
Function f = functions[0];
if (f instanceof Number || f instanceof Variable || f instanceof Expression || f instanceof Division || f instanceof Joke || f instanceof Undefined || f instanceof Power) {
if (f instanceof Number || f instanceof Variable || f instanceof Expression || f instanceof Division || f instanceof Joke
|| f instanceof Undefined || f instanceof Power || f instanceof Sine || f instanceof Cosine || f instanceof Tangent
|| f instanceof ArcSine || f instanceof ArcCosine || f instanceof ArcTangent) {
parenthesisneeded = false;
}
if (f instanceof Multiplication) {

View File

@ -3,8 +3,7 @@ package org.warp.picalculator.math.functions;
import static org.warp.picalculator.device.graphicengine.Display.Render.glDrawStringLeft;
import static org.warp.picalculator.device.graphicengine.Display.Render.glGetStringWidth;
import java.math.BigInteger;
import java.util.List;
import java.util.ArrayList;
import org.warp.picalculator.Error;
import org.warp.picalculator.Utils;
@ -19,6 +18,8 @@ public abstract class FunctionTwoValues implements Function {
variable2 = value2;
}
protected abstract Function NewInstance(Function parent2, Function value1, Function value2);
protected Function parent;
protected Function variable1 = null;
@ -66,7 +67,37 @@ public abstract class FunctionTwoValues implements Function {
protected abstract boolean isSolvable();
@Override
public abstract List<Function> solveOneStep() throws Error;
public final ArrayList<Function> solveOneStep() throws Error {
final boolean solved = variable1.isSolved() & variable2.isSolved();
ArrayList<Function> result = solved?solve():null;;
if (result == null || result.isEmpty()) {
result = new ArrayList<>();
ArrayList<Function> l1 = new ArrayList<Function>();
ArrayList<Function> l2 = new ArrayList<Function>();
if (variable1.isSolved()) {
l1.add(variable1);
} else {
l1.addAll(variable1.solveOneStep());
}
if (variable2.isSolved()) {
l2.add(variable2);
} else {
l2.addAll(variable2.solveOneStep());
}
Function[][] results = Utils.joinFunctionsResults(l1, l2);
for (Function[] f : results) {
result.add(NewInstance(this.parent, (Function)f[0], (Function)f[1]));
}
}
return result;
}
protected abstract ArrayList<Function> solve() throws Error;
@Override
public void generateGraphics() {

View File

@ -1,7 +1,6 @@
package org.warp.picalculator.math.functions;
import java.util.ArrayList;
import java.util.List;
import org.warp.picalculator.Error;
import org.warp.picalculator.Utils;
@ -22,6 +21,11 @@ public class Multiplication extends FunctionTwoValues {
}
}
@Override
protected Function NewInstance(Function parent2, Function value1, Function value2) {
return new Multiplication(parent, value1, value2);
}
@Override
public String getSymbol() {
return MathematicalSymbols.MULTIPLICATION;
@ -41,8 +45,8 @@ public class Multiplication extends FunctionTwoValues {
}
@Override
public List<Function> solveOneStep() throws Error {
List<Function> result = new ArrayList<>();
public ArrayList<Function> solve() throws Error {
ArrayList<Function> result = new ArrayList<>();
if (SyntaxRule1.compare(this)) {
result = SyntaxRule1.execute(this);
} else if (NumberRule1.compare(this)) {
@ -55,25 +59,6 @@ public class Multiplication extends FunctionTwoValues {
result = ExponentRule15.execute(this);
} else if (variable1.isSolved() & variable2.isSolved()) {
result.add(((Number)variable1).multiply((Number)variable2));
} else {
List<Function> l1 = new ArrayList<Function>();
List<Function> l2 = new ArrayList<Function>();
if (variable1.isSolved()) {
l1.add(variable1);
} else {
l1.addAll(variable1.solveOneStep());
}
if (variable2.isSolved()) {
l2.add(variable2);
} else {
l2.addAll(variable2.solveOneStep());
}
Function[][] results = Utils.joinFunctionsResults(l1, l2);
for (Function[] f : results) {
result.add(new Multiplication(this.parent, f[0], f[1]));
}
}
return result;
}

View File

@ -1,7 +1,6 @@
package org.warp.picalculator.math.functions;
import java.util.ArrayList;
import java.util.List;
import org.warp.picalculator.Error;
import org.warp.picalculator.Errors;
@ -17,6 +16,11 @@ public class Negative extends AnteriorFunction {
super(parent, value);
}
@Override
public Function NewInstance(Function parent, Function value) {
return new Negative(parent, value);
}
@Override
public String getSymbol() {
return MathematicalSymbols.MINUS;
@ -40,8 +44,7 @@ public class Negative extends AnteriorFunction {
return false;
}
@Override
public List<Function> solveOneStep() throws Error {
public ArrayList<Function> solve() throws Error {
if (variable == null) {
throw new Error(Errors.SYNTAX_ERROR);
}
@ -62,7 +65,7 @@ public class Negative extends AnteriorFunction {
throw new Error(Errors.NUMBER_TOO_SMALL);
}
} else {
List<Function> l1 = new ArrayList<Function>();
ArrayList<Function> l1 = new ArrayList<Function>();
if (variable.isSolved()) {
l1.add(variable);
} else {

View File

@ -20,6 +20,11 @@ public class Power extends FunctionTwoValues {
super(parent, value1, value2);
}
@Override
protected Function NewInstance(Function parent2, Function value1, Function value2) {
return new Power(parent, value1, value2);
}
@Override
public String getSymbol() {
return MathematicalSymbols.POWER;
@ -54,7 +59,7 @@ public class Power extends FunctionTwoValues {
}
@Override
public List<Function> solveOneStep() throws Error {
public ArrayList<Function> solve() throws Error {
ArrayList<Function> result = new ArrayList<>();
if (UndefinedRule1.compare(this)) {
result.addAll(UndefinedRule1.execute(this));
@ -72,25 +77,6 @@ public class Power extends FunctionTwoValues {
result.addAll(FractionsRule5.execute(this));
} else if (variable1 instanceof Number & variable2 instanceof Number) {
result.add((Function) ((Number)variable1).pow((Number)variable2));
} else {
List<Function> l1 = new ArrayList<Function>();
List<Function> l2 = new ArrayList<Function>();
if (variable1.isSolved()) {
l1.add(variable1);
} else {
l1.addAll(variable1.solveOneStep());
}
if (variable2.isSolved()) {
l2.add(variable2);
} else {
l2.addAll(variable2.solveOneStep());
}
Function[][] results = Utils.joinFunctionsResults(l1, l2);
for (Function[] f : results) {
result.add(new Power(this.parent, (Function)f[0], (Function)f[1]));
}
}
return result;
}

View File

@ -16,6 +16,11 @@ public class Root extends FunctionTwoValues {
super(parent, value1, value2);
}
@Override
protected Function NewInstance(Function parent2, Function value1, Function value2) {
return new Root(parent, value1, value2);
}
@Override
public String getSymbol() {
return MathematicalSymbols.NTH_ROOT;
@ -37,41 +42,31 @@ public class Root extends FunctionTwoValues {
@Override
protected boolean isSolvable() {
if (variable1 instanceof Number & variable2 instanceof Number) {
try {
Number exponent = new Number(this.parent, BigInteger.ONE);
exponent = exponent.divide((Number) variable1);
if (((Number)variable2).pow(exponent).pow((Number)variable1).equals(variable2)) {
return true;
}
} catch (Exception | Error ex) {
}
}
if (variable1 instanceof Number && ((Number)variable1).equals(new Number(null, 2))) {
return true;
}
return false;
}
@Override
public List<Function> solveOneStep() throws Error {
public ArrayList<Function> solve() throws Error {
ArrayList<Function> result = new ArrayList<>();
if (variable1.isSolved() & variable2.isSolved()) {
if (variable1 instanceof Number && ((Number)variable1).equals(new Number(null, 2))) {
result.add(new RootSquare(parent, variable2));
} else {
Number exponent = new Number(this.parent, BigInteger.ONE);
exponent = exponent.divide((Number) variable1);
result.add(((Number)variable2).pow(exponent));
} else {
List<Function> l1 = new ArrayList<Function>();
List<Function> l2 = new ArrayList<Function>();
if (variable1.isSolved()) {
l1.add(variable1);
} else {
l1.addAll(variable1.solveOneStep());
}
if (variable2.isSolved()) {
l2.add(variable2);
} else {
l2.addAll(variable2.solveOneStep());
}
Function[][] results = Utils.joinFunctionsResults(l1, l2);
for (Function[] f : results) {
if (f[0] instanceof Number && ((Number)f[0]).equals(new Number(null, "2"))) {
result.add(new RootSquare(this.parent, (Function)f[1]));
} else {
result.add(new Root(this.parent, (Function)f[0], (Function)f[1]));
}
}
}
return result;
}

View File

@ -15,6 +15,11 @@ public class RootSquare extends AnteriorFunction {
super(parent, value);
}
@Override
public Function NewInstance(Function parent, Function value) {
return new RootSquare(parent, value);
}
@Override
public String getSymbol() {
return MathematicalSymbols.SQUARE_ROOT;
@ -41,33 +46,17 @@ public class RootSquare extends AnteriorFunction {
}
@Override
public List<Function> solveOneStep() throws Error {
if (variable == null) {
throw new Error(Errors.SYNTAX_ERROR);
}
public ArrayList<Function> solve() throws Error {
ArrayList<Function> result = new ArrayList<>();
if (variable.isSolved()) {
try {
Number var = (Number) getVariable();
result.add(new Number(this.getParent(), BigIntegerMath.isqrt(var.term)));
} catch(NullPointerException ex) {
throw new Error(Errors.ERROR);
} catch(NumberFormatException ex) {
throw new Error(Errors.SYNTAX_ERROR);
} catch(ArithmeticException ex) {
throw new Error(Errors.NUMBER_TOO_SMALL);
}
} else {
List<Function> l1 = new ArrayList<Function>();
if (variable.isSolved()) {
l1.add(variable);
} else {
l1.addAll(variable.solveOneStep());
}
for (Function f : l1) {
result.add(new RootSquare(this.parent, (Function)f));
}
try {
Number var = (Number) getVariable();
result.add(new Number(this.getParent(), BigIntegerMath.isqrt(var.term)));
} catch(NullPointerException ex) {
throw new Error(Errors.ERROR);
} catch(NumberFormatException ex) {
throw new Error(Errors.SYNTAX_ERROR);
} catch(ArithmeticException ex) {
throw new Error(Errors.NUMBER_TOO_SMALL);
}
return result;
}

View File

@ -21,6 +21,11 @@ public class Subtraction extends FunctionTwoValues {
super(parent, value1, value2);
}
@Override
protected Function NewInstance(Function parent2, Function value1, Function value2) {
return new Subtraction(parent, value1, value2);
}
@Override
public String getSymbol() {
return MathematicalSymbols.SUBTRACTION;
@ -42,10 +47,7 @@ public class Subtraction extends FunctionTwoValues {
}
@Override
public List<Function> solveOneStep() throws Error {
if (variable1 == null || variable2 == null) {
throw new Error(Errors.SYNTAX_ERROR);
}
public ArrayList<Function> solve() throws Error {
ArrayList<Function> result = new ArrayList<>();
if (VariableRule1.compare(this)) {
result = VariableRule1.execute(this);
@ -63,25 +65,6 @@ public class Subtraction extends FunctionTwoValues {
result = NumberRule5.execute(this);
} else if (variable1.isSolved() & variable2.isSolved()) {
result.add(((Number)variable1).add(((Number)variable2).multiply(new Number(this.parent, "-1"))));
} else {
List<Function> l1 = new ArrayList<Function>();
List<Function> l2 = new ArrayList<Function>();
if (variable1.isSolved()) {
l1.add(variable1);
} else {
l1.addAll(variable1.solveOneStep());
}
if (variable2.isSolved()) {
l2.add(variable2);
} else {
l2.addAll(variable2.solveOneStep());
}
Function[][] results = Utils.joinFunctionsResults(l1, l2);
for (Function[] f : results) {
result.add(new Subtraction(this.parent, (Function)f[0], (Function)f[1]));
}
}
return result;
}

View File

@ -26,6 +26,11 @@ public class Sum extends FunctionTwoValues {
super(parent, value1, value2);
}
@Override
protected Function NewInstance(Function parent2, Function value1, Function value2) {
return new Sum(parent, value1, value2);
}
@Override
public String getSymbol() {
return MathematicalSymbols.SUM;
@ -48,7 +53,7 @@ public class Sum extends FunctionTwoValues {
}
@Override
public List<Function> solveOneStep() throws Error {
public ArrayList<Function> solve() throws Error {
if (variable1 == null || variable2 == null) {
throw new Error(Errors.SYNTAX_ERROR);
}
@ -83,25 +88,6 @@ public class Sum extends FunctionTwoValues {
}
}
result.add(((Number)variable1).add((Number)variable2));
} else {
List<Function> l1 = new ArrayList<Function>();
List<Function> l2 = new ArrayList<Function>();
if (variable1.isSolved()) {
l1.add(variable1);
} else {
l1.addAll(variable1.solveOneStep());
}
if (variable2.isSolved()) {
l2.add(variable2);
} else {
l2.addAll(variable2.solveOneStep());
}
Function[][] results = Utils.joinFunctionsResults(l1, l2);
for (Function[] f : results) {
result.add(new Sum(this.parent, (Function)f[0], (Function)f[1]));
}
}
return result;
}

View File

@ -22,6 +22,11 @@ public class SumSubtraction extends FunctionTwoValues {
super(parent, value1, value2);
}
@Override
protected Function NewInstance(Function parent2, Function value1, Function value2) {
return new SumSubtraction(parent, value1, value2);
}
@Override
public String getSymbol() {
return MathematicalSymbols.SUM_SUBTRACTION;
@ -40,7 +45,7 @@ public class SumSubtraction extends FunctionTwoValues {
}
@Override
public List<Function> solveOneStep() throws Error {
public ArrayList<Function> solve() throws Error {
if (variable1 == null || variable2 == null) {
throw new Error(Errors.SYNTAX_ERROR);
}
@ -56,25 +61,6 @@ public class SumSubtraction extends FunctionTwoValues {
} else if (variable1.isSolved() & variable2.isSolved()) {
result.add(((Number)variable1).add((Number)variable2));
result.add(((Number)variable1).add(((Number)variable2).multiply(new Number(this.parent, "-1"))));
} else {
List<Function> l1 = new ArrayList<Function>();
List<Function> l2 = new ArrayList<Function>();
if (variable1.isSolved()) {
l1.add(variable1);
} else {
l1.addAll(variable1.solveOneStep());
}
if (variable2.isSolved()) {
l2.add(variable2);
} else {
l2.addAll(variable2.solveOneStep());
}
Function[][] results = Utils.joinFunctionsResults(l1, l2);
for (Function[] f : results) {
result.add(new SumSubtraction(this.parent, f[0], f[1]));
}
}
return result;
}

View File

@ -0,0 +1,45 @@
package org.warp.picalculator.math.functions.trigonometry;
import java.util.ArrayList;
import java.util.List;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.MathematicalSymbols;
import org.warp.picalculator.math.functions.AnteriorFunction;
import org.warp.picalculator.math.functions.Function;
public class ArcCosine extends AnteriorFunction {
public ArcCosine(Function parent, Function value) {
super(parent, value);
}
@Override
public Function NewInstance(Function parent, Function value) {
return new ArcCosine(parent, value);
}
@Override
public String getSymbol() {
return MathematicalSymbols.getGraphicRepresentation(MathematicalSymbols.ARC_COSINE);
}
@Override
public ArrayList<Function> solve() throws Error {
// TODO Auto-generated method stub
return null;
}
@Override
protected boolean isSolvable() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean equals(Object o) {
// TODO Auto-generated method stub
return false;
}
}

View File

@ -0,0 +1,45 @@
package org.warp.picalculator.math.functions.trigonometry;
import java.util.ArrayList;
import java.util.List;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.MathematicalSymbols;
import org.warp.picalculator.math.functions.AnteriorFunction;
import org.warp.picalculator.math.functions.Function;
public class ArcSine extends AnteriorFunction {
public ArcSine(Function parent, Function value) {
super(parent, value);
}
@Override
public Function NewInstance(Function parent, Function value) {
return new ArcSine(parent, value);
}
@Override
public String getSymbol() {
return MathematicalSymbols.getGraphicRepresentation(MathematicalSymbols.ARC_SINE);
}
@Override
public ArrayList<Function> solve() throws Error {
// TODO Auto-generated method stub
return null;
}
@Override
protected boolean isSolvable() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean equals(Object o) {
// TODO Auto-generated method stub
return false;
}
}

View File

@ -0,0 +1,45 @@
package org.warp.picalculator.math.functions.trigonometry;
import java.util.ArrayList;
import java.util.List;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.MathematicalSymbols;
import org.warp.picalculator.math.functions.AnteriorFunction;
import org.warp.picalculator.math.functions.Function;
public class ArcTangent extends AnteriorFunction {
public ArcTangent(Function parent, Function value) {
super(parent, value);
}
@Override
public Function NewInstance(Function parent, Function value) {
return new ArcTangent(parent, value);
}
@Override
public String getSymbol() {
return MathematicalSymbols.getGraphicRepresentation(MathematicalSymbols.ARC_TANGENT);
}
@Override
public ArrayList<Function> solve() throws Error {
// TODO Auto-generated method stub
return null;
}
@Override
protected boolean isSolvable() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean equals(Object o) {
// TODO Auto-generated method stub
return false;
}
}

View File

@ -0,0 +1,45 @@
package org.warp.picalculator.math.functions.trigonometry;
import java.util.ArrayList;
import java.util.List;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.MathematicalSymbols;
import org.warp.picalculator.math.functions.AnteriorFunction;
import org.warp.picalculator.math.functions.Function;
public class Cosine extends AnteriorFunction {
public Cosine(Function parent, Function value) {
super(parent, value);
}
@Override
public Function NewInstance(Function parent, Function value) {
return new Cosine(parent, value);
}
@Override
public String getSymbol() {
return MathematicalSymbols.getGraphicRepresentation(MathematicalSymbols.COSINE);
}
@Override
public ArrayList<Function> solve() throws Error {
// TODO Auto-generated method stub
return null;
}
@Override
protected boolean isSolvable() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean equals(Object o) {
// TODO Auto-generated method stub
return false;
}
}

View File

@ -0,0 +1,52 @@
package org.warp.picalculator.math.functions.trigonometry;
import java.util.ArrayList;
import java.util.List;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.MathematicalSymbols;
import org.warp.picalculator.math.functions.AnteriorFunction;
import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.FunctionTwoValues;
import org.warp.picalculator.math.functions.Sum;
public class Sine extends AnteriorFunction {
public Sine(Function parent, Function value) {
super(parent, value);
}
@Override
public Function NewInstance(Function parent, Function value) {
return new Sine(parent, value);
}
@Override
public String getSymbol() {
return MathematicalSymbols.getGraphicRepresentation(MathematicalSymbols.SINE);
}
@Override
public ArrayList<Function> solve() throws Error {
// TODO Auto-generated method stub
return null;
}
@Override
protected boolean isSolvable() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean equals(Object o) {
if (o instanceof Sine) {
AnteriorFunction f = (AnteriorFunction) o;
if (variable.equals(f.getVariable())) {
return true;
}
}
return false;
}
}

View File

@ -0,0 +1,45 @@
package org.warp.picalculator.math.functions.trigonometry;
import java.util.ArrayList;
import java.util.List;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.MathematicalSymbols;
import org.warp.picalculator.math.functions.AnteriorFunction;
import org.warp.picalculator.math.functions.Function;
public class Tangent extends AnteriorFunction {
public Tangent(Function parent, Function value) {
super(parent, value);
}
@Override
public Function NewInstance(Function parent, Function value) {
return new Tangent(parent, value);
}
@Override
public String getSymbol() {
return MathematicalSymbols.getGraphicRepresentation(MathematicalSymbols.TANGENT);
}
@Override
public ArrayList<Function> solve() throws Error {
// TODO Auto-generated method stub
return null;
}
@Override
protected boolean isSolvable() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean equals(Object o) {
// TODO Auto-generated method stub
return false;
}
}

View File

@ -52,7 +52,7 @@ public class LoadingScreen extends Screen {
endLoading += dt;
if (endLoading >= 2) {
loading = false;
PIDisplay.INSTANCE.setScreen(new EquationScreen());
PIDisplay.INSTANCE.setScreen(new MathInputScreen());
}
mustRefresh = true;
}

View File

@ -23,7 +23,7 @@ import org.warp.picalculator.math.MathematicalSymbols;
import org.warp.picalculator.math.functions.Function;
import org.warp.picalculator.math.functions.Expression;
public class EquationScreen extends Screen {
public class MathInputScreen extends Screen {
public volatile String equazioneCorrente = "";
public volatile String nuovaEquazione = "";
@ -39,7 +39,7 @@ public class EquationScreen extends Screen {
boolean mustRefresh = true;
boolean afterDoNextStep = false;
public EquationScreen() {
public MathInputScreen() {
super();
canBeInHistory = true;
}
@ -118,7 +118,11 @@ public class EquationScreen extends Screen {
}
ArrayList<Function> fncs = new ArrayList<Function>();
if (eqn.length() > 0) {
fncs.add(Calculator.parseString(eqn.replace("sqrt", "").replace("^", "")));
try {
fncs.add(Calculator.parseString(eqn.replace("sqrt", "").replace("^", "")));
} catch (Exception ex) {
}
}
f = fncs;
for (Function f : f) {
@ -157,7 +161,8 @@ public class EquationScreen extends Screen {
final int textColor = 0xFF000000;
final int padding = 4;
glColor(textColor);
final String inputText = MathematicalSymbols.getGraphicRepresentation(nuovaEquazione.substring(0, caretPos)+(showCaret?"|":"")+nuovaEquazione.substring(((showCaret==false||nuovaEquazione.length()<=caretPos)?caretPos:caretPos+1), nuovaEquazione.length()));
final int caretRealPos = MathematicalSymbols.getGraphicRepresentation(nuovaEquazione.substring(0, caretPos)).length()*(fontBig.charW+1);
final String inputTextWithoutCaret = MathematicalSymbols.getGraphicRepresentation(nuovaEquazione);
final boolean tooLongI = padding+glGetStringWidth(fontBig, nuovaEquazione)+padding >= Main.screenSize[0];
int scrollI = 0;
if (tooLongI) {
@ -168,7 +173,10 @@ public class EquationScreen extends Screen {
scrollI = 0;
}
}
glDrawStringLeft(padding+scrollI, padding+20, inputText);
glDrawStringLeft(padding+scrollI, padding+20, inputTextWithoutCaret);
if (showCaret) {
glDrawStringLeft(padding+scrollI+caretRealPos, padding+20, "|");
}
if (tooLongI) {
glColor(clearcolor);
glFillRect(Main.screenSize[0]-16-2, padding+20, fontBig.charH, Main.screenSize[0]);
@ -363,10 +371,10 @@ public class EquationScreen extends Screen {
typeChar("");
return true;
case POWER_OF_2:
typeChar("^2");
typeChar(MathematicalSymbols.POWER+"2");
return true;
case POWER_OF_x:
typeChar("^");
typeChar(MathematicalSymbols.POWER);
return true;
case LETTER_X:
typeChar(MathematicalSymbols.variables()[23]);
@ -374,6 +382,24 @@ public class EquationScreen extends Screen {
case LETTER_Y:
typeChar(MathematicalSymbols.variables()[24]);
return true;
case SINE:
typeChar(MathematicalSymbols.SINE);
return true;
case COSINE:
typeChar(MathematicalSymbols.COSINE);
return true;
case TANGENT:
typeChar(MathematicalSymbols.TANGENT);
return true;
case ARCSINE:
typeChar(MathematicalSymbols.ARC_SINE);
return true;
case ARCCOSINE:
typeChar(MathematicalSymbols.ARC_COSINE);
return true;
case ARCTANGENT:
typeChar(MathematicalSymbols.ARC_TANGENT);
return true;
case DELETE:
if (nuovaEquazione.length() > 0) {
if (caretPos > 0) {
@ -449,7 +475,7 @@ public class EquationScreen extends Screen {
return true;
case HISTORY_BACK:
if (PIDisplay.INSTANCE.canGoBack()) {
if (equazioneCorrente != null && equazioneCorrente.length() > 0 & Calculator.sessions[Calculator.currentSession+1] instanceof EquationScreen) {
if (equazioneCorrente != null && equazioneCorrente.length() > 0 & Calculator.sessions[Calculator.currentSession+1] instanceof MathInputScreen) {
nuovaEquazione = equazioneCorrente;
try {
interpreta(true);
@ -460,7 +486,7 @@ public class EquationScreen extends Screen {
return false;
case HISTORY_FORWARD:
if (PIDisplay.INSTANCE.canGoForward()) {
if (equazioneCorrente != null && equazioneCorrente.length() > 0 & Calculator.sessions[Calculator.currentSession-1] instanceof EquationScreen) {
if (equazioneCorrente != null && equazioneCorrente.length() > 0 & Calculator.sessions[Calculator.currentSession-1] instanceof MathInputScreen) {
nuovaEquazione = equazioneCorrente;
try {
interpreta(true);
@ -476,7 +502,7 @@ public class EquationScreen extends Screen {
private void changeEquationScreen() {
if (equazioneCorrente != null && equazioneCorrente.length() > 0) {
EquationScreen cloned = clone();
MathInputScreen cloned = clone();
cloned.caretPos = cloned.equazioneCorrente.length();
cloned.nuovaEquazione = cloned.equazioneCorrente;
cloned.scrollX = glGetStringWidth(fontBig, cloned.equazioneCorrente);
@ -510,9 +536,9 @@ public class EquationScreen extends Screen {
}
@Override
public EquationScreen clone() {
EquationScreen es = this;
EquationScreen es2 = new EquationScreen();
public MathInputScreen clone() {
MathInputScreen es = this;
MathInputScreen es2 = new MathInputScreen();
es2.scrollX = es.scrollX;
es2.nuovaEquazione = es.nuovaEquazione;
es2.equazioneCorrente = es.equazioneCorrente;

View File

@ -14,9 +14,9 @@ import org.warp.picalculator.math.Calculator;
public class SolveEquationScreen extends Screen {
@SuppressWarnings("unused")
private EquationScreen es;
private MathInputScreen es;
public SolveEquationScreen(EquationScreen es) {
public SolveEquationScreen(MathInputScreen es) {
super();
canBeInHistory = false;
@ -59,8 +59,8 @@ public class SolveEquationScreen extends Screen {
Calculator.solveExpression('X');
} catch (Error e) {
Screen scr = PIDisplay.INSTANCE.getScreen();
if (scr instanceof EquationScreen) {
EquationScreen escr = (EquationScreen) scr;
if (scr instanceof MathInputScreen) {
MathInputScreen escr = (MathInputScreen) scr;
escr.errorLevel = 1;
escr.err2 = e;
} else {