diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index ea4e391d..edde778f 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -8,5 +8,6 @@ encoding//src/org/warp/picalculator/math/MathematicalSymbols.java=UTF-8 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/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000..ace45cee --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/res/font_32.rft b/res/font_32.rft new file mode 100644 index 00000000..fa9e6236 Binary files /dev/null and b/res/font_32.rft differ diff --git a/res/font_big.rft b/res/font_big.rft index 3a2fa102..5aba8162 100644 Binary files a/res/font_big.rft and b/res/font_big.rft differ diff --git a/res/font_big_2x.rft b/res/font_big_2x.rft index f128024e..8a6da0cb 100644 Binary files a/res/font_big_2x.rft and b/res/font_big_2x.rft differ diff --git a/res/font_small.rft b/res/font_small.rft index 328f7dfb..8d4148d4 100644 Binary files a/res/font_small.rft and b/res/font_small.rft differ diff --git a/res/font_small_2x.rft b/res/font_small_2x.rft index 5b449bae..283cc025 100644 Binary files a/res/font_small_2x.rft and b/res/font_small_2x.rft differ diff --git a/res/temp.rft b/res/font_square.rft similarity index 87% rename from res/temp.rft rename to res/font_square.rft index 509ba74b..b6b29d74 100644 Binary files a/res/temp.rft and b/res/font_square.rft differ diff --git a/res/palettetmp.bmp b/res/palettetmp.bmp new file mode 100644 index 00000000..db22ad4f Binary files /dev/null and b/res/palettetmp.bmp differ diff --git a/src/org/nevec/rjm/NumeroAvanzatoVec.java b/src/org/nevec/rjm/NumeroAvanzatoVec.java index 39a75a89..a844874e 100644 --- a/src/org/nevec/rjm/NumeroAvanzatoVec.java +++ b/src/org/nevec/rjm/NumeroAvanzatoVec.java @@ -512,6 +512,17 @@ public class NumeroAvanzatoVec implements Comparable { return (this.terms.size() <= 1); } + + + public boolean hasVariables() { + for (NumeroAvanzato s : terms) { + if ((s.getVariableX().count() | s.getVariableY().count() | s.getVariableZ().count()) != 0) { + return true; + } + } + return false; + } + /** * True if the value is BigInteger. Equivalent to the indication whether a * conversion to a BigInteger can be exact. diff --git a/src/org/warp/picalculator/Main.java b/src/org/warp/picalculator/Main.java index 8864a375..d0970ed6 100644 --- a/src/org/warp/picalculator/Main.java +++ b/src/org/warp/picalculator/Main.java @@ -48,13 +48,6 @@ public class Main { } public static void main(String[] args) throws InterruptedException { - if (test()) { - return; - } new Main(); } - - private static boolean test() { - return false; - } } diff --git a/src/org/warp/picalculator/Utils.java b/src/org/warp/picalculator/Utils.java index 21413aa1..0e16b998 100644 --- a/src/org/warp/picalculator/Utils.java +++ b/src/org/warp/picalculator/Utils.java @@ -2,6 +2,8 @@ package org.warp.picalculator; import static org.warp.picalculator.device.graphicengine.Display.Render.glDrawLine; +import com.rits.cloning.Cloner; + import java.awt.Font; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -44,6 +46,8 @@ public class Utils { public static boolean debugOn; + public static Cloner cloner = new Cloner(); + public static final class DebugStream extends StringWriter { public void println(String str) { @@ -68,13 +72,30 @@ public class Utils { return contains; } + private static final String[] regexNormalSymbols = new String[]{"\\", ".", "[", "]", "{", "}", "(", ")", "*", "+", "-", "?", "^", "$", "|"}; + public static String ArrayToRegex(String[] array) { String regex = null; for (String symbol : array) { - if (regex != null) { - regex += "|\\" + symbol; + boolean contained = false; + for (String smb : regexNormalSymbols) { + if (smb.equals(symbol)) { + contained = true; + break; + } + } + if (contained) { + if (regex != null) { + regex += "|\\" + symbol; + } else { + regex = "\\" + symbol; + } } else { - regex = "\\" + symbol; + if (regex != null) { + regex += "|" + symbol; + } else { + regex = symbol; + } } } return regex; @@ -99,7 +120,7 @@ public class Utils { public static boolean areThereOnlySettedUpFunctionsSumsEquationsAndSystems(ArrayList fl) { for (int i = 0; i < fl.size(); i++) { - if (!(fl.get(i) instanceof Number || fl.get(i) instanceof Sum || fl.get(i) instanceof SumSubtraction || fl.get(i) instanceof Equation || fl.get(i) instanceof EquationsSystemPart || fl.get(i) instanceof Expression)) { + if (!(fl.get(i) instanceof Number || fl.get(i) instanceof Sum || fl.get(i) instanceof SumSubtraction || fl.get(i) instanceof Subtraction || fl.get(i) instanceof Equation || fl.get(i) instanceof EquationsSystemPart || fl.get(i) instanceof Expression)) { if (fl.get(i) instanceof AnteriorFunction) { if (((AnteriorFunction) fl.get(i)).getVariable() == null) { return false; @@ -118,7 +139,7 @@ public class Utils { public static boolean areThereOnlySettedUpFunctionsSumsMultiplicationsEquationsAndSystems(ArrayList fl) { for (int i = 0; i < fl.size(); i++) { - if (!(fl.get(i) instanceof Number || fl.get(i) instanceof Multiplication || fl.get(i) instanceof PrioritaryMultiplication || fl.get(i) instanceof Sum || fl.get(i) instanceof SumSubtraction || fl.get(i) instanceof Equation || fl.get(i) instanceof EquationsSystemPart || fl.get(i) instanceof Expression)) { + if (!(fl.get(i) instanceof Number || fl.get(i) instanceof Multiplication || fl.get(i) instanceof PrioritaryMultiplication || fl.get(i) instanceof Sum || fl.get(i) instanceof SumSubtraction || fl.get(i) instanceof Subtraction || fl.get(i) instanceof Equation || fl.get(i) instanceof EquationsSystemPart || fl.get(i) instanceof Expression)) { if (fl.get(i) instanceof AnteriorFunction) { if (((AnteriorFunction) fl.get(i)).getVariable() == null) { return false; @@ -220,7 +241,7 @@ public class Utils { public static boolean areThereEmptySums(ArrayList fl) { for (int i = 0; i < fl.size(); i++) { - if (fl.get(i) instanceof Sum || fl.get(i) instanceof SumSubtraction) { + if (fl.get(i) instanceof Sum || fl.get(i) instanceof SumSubtraction || fl.get(i) instanceof Subtraction) { if (((FunctionTwoValues) fl.get(i)).getVariable1() == null && ((FunctionTwoValues) fl.get(i)).getVariable2() == null) { return true; } @@ -358,17 +379,21 @@ public class Utils { } public static final RAWFont getFont(boolean small, boolean zoomed) { + return PIDisplay.fonts[getFontIndex(small, zoomed)]; + } + + public static final int getFontIndex(boolean small, boolean zoomed) { if (small) { if (zoomed) { - return PIDisplay.fonts[3]; + return 3; } else { - return PIDisplay.fonts[1]; + return 1; } } else { if (zoomed) { - return PIDisplay.fonts[2]; + return 2; } else { - return PIDisplay.fonts[0]; + return 0; } } } diff --git a/src/org/warp/picalculator/device/Keyboard.java b/src/org/warp/picalculator/device/Keyboard.java index bd44cb84..50d87292 100644 --- a/src/org/warp/picalculator/device/Keyboard.java +++ b/src/org/warp/picalculator/device/Keyboard.java @@ -454,7 +454,7 @@ public class Keyboard { refresh = true; } if (refresh) { - Display.repaint(true); + Display.repaint(); } } } @@ -478,7 +478,7 @@ public class Keyboard { } } if (refresh) { - Display.repaint(true); + Display.repaint(); } } } diff --git a/src/org/warp/picalculator/device/PIDisplay.java b/src/org/warp/picalculator/device/PIDisplay.java index 2464081c..e6061fa4 100644 --- a/src/org/warp/picalculator/device/PIDisplay.java +++ b/src/org/warp/picalculator/device/PIDisplay.java @@ -33,7 +33,7 @@ public final class PIDisplay { private int[] skin; private int[] skinSize; - public static RAWFont[] fonts = new RAWFont[4]; + public static RAWFont[] fonts; public static String error = null; public String[] errorStackTrace = null; @@ -45,7 +45,7 @@ public final class PIDisplay { public PIDisplay(Screen screen) { setScreen(screen); INSTANCE = this; - run(); + loop(); } /* * private void load_skin() { @@ -100,6 +100,30 @@ public final class PIDisplay { System.exit(0); } } + + public void replaceScreen(Screen screen) { + if (screen.initialized == false) { + if (screen.canBeInHistory) { + Calculator.sessions[Calculator.currentSession] = screen; + } else { + Calculator.currentSession = -1; + for (int i = 0; i < Calculator.sessions.length - 2; i++) { + Calculator.sessions[i] = Calculator.sessions[i + 1]; + } + } + } + screen.d = this; + try { + screen.create(); + PIDisplay.screen = screen; + if (screen.initialized == false) { + screen.initialize(); + } + } catch (Exception e) { + e.printStackTrace(); + System.exit(0); + } + } public boolean canGoBack() { if (Calculator.currentSession == -1) { @@ -176,6 +200,7 @@ public final class PIDisplay { } private void load_fonts() { + fonts = new RAWFont[7]; fonts[0] = new RAWFont(); fonts[0].create("big"); fonts[1] = new RAWFont(); @@ -184,7 +209,11 @@ public final class PIDisplay { fonts[2].create("big_2x"); fonts[3] = new RAWFont(); fonts[3].create("small_2x"); - setFont(fonts[0]); + fonts[4] = new RAWFont(); + fonts[4].create("32"); + fonts[5] = new RAWFont(); + fonts[5].create("square"); + glSetFont(fonts[0]); } private void draw_init() { @@ -280,7 +309,7 @@ public final class PIDisplay { glColor3f(255, 255, 255); if (error != null) { - setFont(Utils.getFont(false, false)); + glSetFont(Utils.getFont(false, false)); glColor3f(129, 28, 22); glDrawStringRight(Main.screenSize[0] - 2, Main.screenSize[1]- this.glyphsHeight[1] - 2, "ANDREA CAVALLI'S CALCULATOR"); glColor3f(149, 32, 26); @@ -291,7 +320,7 @@ public final class PIDisplay { glDrawStringLeft(2, 22 + i, stackPart); i += 11; } - setFont(fonts[0]); + glSetFont(fonts[0]); glColor3f(129, 28, 22); glDrawStringCenter((Main.screenSize[0] / 2), 11, "UNEXPECTED EXCEPTION"); } else { @@ -308,16 +337,13 @@ public final class PIDisplay { private long precTime = -1; - public void refresh(boolean forced) { + public void refresh() { float dt = 0; long newtime = System.nanoTime(); if (precTime == -1) { dt = 0; } else { dt = (float) ((newtime - precTime) / 1000000000d); - if (dt < 0.03 && !forced) { - return; - } } precTime = newtime; /* @@ -327,7 +353,7 @@ public final class PIDisplay { screen.beforeRender(dt); - if(forced==true || screen.mustBeRefreshed()) { + if(dt >= 0.03 | screen.mustBeRefreshed()) { draw(); } @@ -340,7 +366,7 @@ public final class PIDisplay { } }; - public void run() { + public void loop() { try { load_skin(); load_fonts(); @@ -359,10 +385,10 @@ public final class PIDisplay { double extratime = 0; while (Display.initialized) { - long start = System.nanoTime(); - Display.repaint(false); - long end = System.nanoTime(); - double delta = (end - start) / 1000000000; + long start = System.currentTimeMillis(); + Display.repaint(); + long end = System.currentTimeMillis(); + double delta = (end - start) / 1000d; int deltaInt = (int) Math.floor(delta); int extraTimeInt = (int) Math.floor(extratime); if (extraTimeInt + deltaInt < 50) { diff --git a/src/org/warp/picalculator/device/graphicengine/Display.java b/src/org/warp/picalculator/device/graphicengine/Display.java index b3662c2d..51365054 100644 --- a/src/org/warp/picalculator/device/graphicengine/Display.java +++ b/src/org/warp/picalculator/device/graphicengine/Display.java @@ -7,15 +7,15 @@ import java.awt.image.DataBufferInt; import java.util.ArrayList; import org.warp.picalculator.Main; +import org.warp.picalculator.Utils; import org.warp.picalculator.device.PIDisplay; -import org.warp.picalculator.device.PIFrame; public class Display { - private static PIFrame INSTANCE = new PIFrame(); + private static Frame INSTANCE = new Frame(); public static int[] size = new int[] { 1, 1 }; - public static BufferedImage g = new BufferedImage(size[0], size[1], BufferedImage.TYPE_INT_ARGB); - private static int[] canvas2d = new int[1]; + public static BufferedImage g = new BufferedImage(size[0], size[1], BufferedImage.TYPE_INT_RGB); + static int[] canvas2d = new int[1]; public static int color = 0xFF000000; public static boolean initialized = false; @@ -38,6 +38,7 @@ public class Display { } public static void create() { + Display.setResizable(Utils.debugOn); Display.setDisplayMode(Main.screenSize[0], Main.screenSize[1]); INSTANCE.setVisible(true); initialized = true; @@ -74,38 +75,14 @@ public class Display { @Deprecated() public static void refresh() { if (PIDisplay.screen == null || (PIDisplay.error != null && PIDisplay.error.length() > 0) || PIDisplay.screen == null || PIDisplay.screen.mustBeRefreshed()) { - Display.INSTANCE.c.forcerefresh = false; Display.INSTANCE.c.repaint(); } } - public static void repaint(boolean force) { - Display.INSTANCE.c.forcerefresh = force; + public static void repaint() { Display.INSTANCE.c.repaint(); } -// private static ArrayList mediaValori = new ArrayList(); - - public static void update(Graphics g, boolean forcerefresh) { -// long time1 = System.nanoTime(); - PIDisplay.INSTANCE.refresh(forcerefresh); - - final int[] a = ((DataBufferInt) Display.g.getRaster().getDataBuffer()).getData(); -// System.arraycopy(canvas2d, 0, a, 0, canvas2d.length); - canvas2d = a; - g.clearRect(0, 0, size[0], size[1]); - g.drawImage(Display.g, 0, 0, null); -// long time2 = System.nanoTime(); -// double timeDelta = ((double)(time2-time1))/1000000000d; -// double mediaAttuale = timeDelta; -// mediaValori.add(mediaAttuale); -// double somma = 0; -// for (Double val : mediaValori) { -// somma+=val; -// } -// System.out.println(somma/((double)mediaValori.size())); - } - public static abstract class Startable { public Startable() { this.force = false; @@ -286,20 +263,20 @@ public class Display { } public static void glDrawStringCenter(int x, int y, String text) { - glDrawStringLeft(x - (getStringWidth(text) / 2), y, text); + glDrawStringLeft(x - (glGetStringWidth(text) / 2), y, text); } public static void glDrawStringRight(int x, int y, String text) { - glDrawStringLeft(x - getStringWidth(text), y, text); + glDrawStringLeft(x - glGetStringWidth(text), y, text); } - public static void setFont(RAWFont font) { + public static void glSetFont(RAWFont font) { if (currentFont != font) { currentFont = font; } } - public static int getStringWidth(String text) { + public static int glGetStringWidth(String text) { int w =(currentFont.charW+1)*text.length(); if (text.length() > 0) { return w-1; @@ -309,11 +286,21 @@ public class Display { // return text.length()*6; } - public static int getWidth(FontMetrics fm, String text) { + public static int glGetStringWidth(RAWFont rf, String text) { + int w =(rf.charW+1)*text.length(); + if (text.length() > 0) { + return w-1; + } else { + return 0; + } + // return text.length()*6; + } + + public static int glGetFontWidth(FontMetrics fm, String text) { return fm.stringWidth(text); } - public static int getFontHeight() { + public static int glGetCurrentFontHeight() { return currentFont.charH; } diff --git a/src/org/warp/picalculator/device/PIFrame.java b/src/org/warp/picalculator/device/graphicengine/Frame.java similarity index 87% rename from src/org/warp/picalculator/device/PIFrame.java rename to src/org/warp/picalculator/device/graphicengine/Frame.java index fa145532..445562a1 100644 --- a/src/org/warp/picalculator/device/PIFrame.java +++ b/src/org/warp/picalculator/device/graphicengine/Frame.java @@ -1,9 +1,10 @@ -package org.warp.picalculator.device; +package org.warp.picalculator.device.graphicengine; +import java.awt.AlphaComposite; import java.awt.Cursor; import java.awt.Dimension; -import java.awt.Frame; import java.awt.Graphics; +import java.awt.Graphics2D; import java.awt.Point; import java.awt.Toolkit; import java.awt.event.ComponentEvent; @@ -11,23 +12,26 @@ import java.awt.event.ComponentListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.image.BufferedImage; +import java.awt.image.DataBufferInt; +import java.util.ArrayList; import javax.swing.JFrame; import javax.swing.JPanel; import org.warp.picalculator.Main; import org.warp.picalculator.Utils; +import org.warp.picalculator.device.Keyboard; +import org.warp.picalculator.device.PIDisplay; import org.warp.picalculator.device.Keyboard.Key; -import org.warp.picalculator.device.graphicengine.Display; -public class PIFrame extends JFrame { +public class Frame extends JFrame { private static final long serialVersionUID = 2945898937634075491L; public CustomCanvas c; public boolean wasResized = false; - public PIFrame() { + public Frame() { c = new CustomCanvas(); - c.setDoubleBuffered(true); + c.setDoubleBuffered(false); this.add(c); // this.setExtendedState(Frame.MAXIMIZED_BOTH); Toolkit.getDefaultToolkit().setDynamicLayout(false); @@ -432,6 +436,8 @@ public class PIFrame extends JFrame { return c.getHeight(); } +// private static ArrayList mediaValori = new ArrayList(); + public static class CustomCanvas extends JPanel { /** @@ -440,16 +446,24 @@ public class PIFrame extends JFrame { private static final long serialVersionUID = 605243927485370885L; @Override - public void paintComponent(Graphics graphics) { - Display.update(graphics, forcerefresh); - } + public void paintComponent(Graphics g) { +// long time1 = System.nanoTime(); + PIDisplay.INSTANCE.refresh(); - @Override - public void repaint() { - forcerefresh = false; - super.repaint(); + final int[] a = ((DataBufferInt) Display.g.getRaster().getDataBuffer()).getData(); +// System.arraycopy(canvas2d, 0, a, 0, canvas2d.length); + Display.canvas2d = a; + g.clearRect(0, 0, Display.size[0], Display.size[1]); + g.drawImage(Display.g, 0, 0, null); +// long time2 = System.nanoTime(); +// double timeDelta = ((double)(time2-time1))/1000000000d; +// double mediaAttuale = timeDelta; +// mediaValori.add(mediaAttuale); +// double somma = 0; +// for (Double val : mediaValori) { +// somma+=val; +// } +// System.out.println(somma/((double)mediaValori.size())); } - - public boolean forcerefresh = false; } } diff --git a/src/org/warp/picalculator/device/graphicengine/RAWFont.java b/src/org/warp/picalculator/device/graphicengine/RAWFont.java index 67d1f74b..a41bab03 100644 --- a/src/org/warp/picalculator/device/graphicengine/RAWFont.java +++ b/src/org/warp/picalculator/device/graphicengine/RAWFont.java @@ -52,9 +52,6 @@ public class RAWFont { currentBit += 1; } } else { - if (charIndex == 65 && name == "big_2x") { - System.out.println("test"); - } int currentInt = 0; int currentBit = 0; for (int i = 0; i < charS; i++) { @@ -81,18 +78,18 @@ public class RAWFont { int[] file = Utils.realBytes(Utils.convertStreamToByteArray(res.openStream(), res.getFile().length())); int filelength = file.length; if (filelength >= 16) { - if (file[0x0] == 114 && file[0x1] == 97 && file[0x2] == 119 && file[0x3] == 0xFF && file[0x6] == 0xFF && file[0xB] == 0xFF) { - charW = file[0x4]; - charH = file[0x5]; + if (file[0x0] == 114 && file[0x1] == 97 && file[0x2] == 119 && file[0x3] == 0xFF && file[0x8] == 0xFF && file[0xD] == 0xFF) { + charW = file[0x4] << 8 | file[0x5]; + charH = file[0x6] << 8 | file[0x7]; charS = charW*charH; charIntCount = (int) Math.ceil(((double)charS)/((double)intBits)); - minBound = file[0x7] << 24 | file[0x8] << 16 | file[0x9] << 8 | file[0xA]; - maxBound = file[0xC] << 24 | file[0xD] << 16 | file[0xE] << 8 | file[0xF]; + minBound = file[0x9] << 24 | file[0xA] << 16 | file[0xB] << 8 | file[0xC]; + maxBound = file[0xE] << 24 | file[0xF] << 16 | file[0x10] << 8 | file[0x11]; if (maxBound <= minBound) { maxBound = 10000; //TODO remove it: temp fix } rawchars = new boolean[maxBound-minBound][]; - int index = 0x10; + int index = 0x12; while (index < filelength) { try { int charIndex = file[index] << 8 | file[index+1]; diff --git a/src/org/warp/picalculator/math/Calculator.java b/src/org/warp/picalculator/math/Calculator.java index 23a2f971..42a7e23f 100644 --- a/src/org/warp/picalculator/math/Calculator.java +++ b/src/org/warp/picalculator/math/Calculator.java @@ -11,6 +11,7 @@ import org.warp.picalculator.Error; import org.warp.picalculator.Errors; import org.warp.picalculator.Utils; import org.warp.picalculator.device.PIDisplay; +import org.warp.picalculator.device.graphicengine.Display; import org.warp.picalculator.device.graphicengine.Screen; import org.warp.picalculator.math.functions.Expression; import org.warp.picalculator.math.functions.Function; @@ -33,7 +34,7 @@ public class Calculator { throw new Error(Errors.SYNTAX_ERROR); } String[] parts = string.substring(1).split("\\{"); - EquationsSystem s = new EquationsSystem(); + EquationsSystem s = new EquationsSystem(null); for (String part : parts) { s.addVariableToEnd(parseEquationString(part)); } @@ -41,63 +42,67 @@ public class Calculator { } else if (string.contains("=")) { return parseEquationString(string); } else { - return new Expression(string); + return new Expression(null, string); } } public static Function parseEquationString(String string) throws Error { String[] parts = string.split("="); if (parts.length == 1) { - return new Equation(new Expression(parts[0]), new Number(NumeroAvanzato.ZERO)); + Equation e = new Equation(null, null, null); + e.setVariable1(new Expression(e, parts[0])); + e.setVariable2(new Number(e, NumeroAvanzato.ZERO)); + return e; } else if (parts.length == 2) { - return new Equation(new Expression(parts[0]), new Expression(parts[1])); + Equation e = new Equation(null, null, null); + e.setVariable1(new Expression(e, parts[0])); + e.setVariable2(new Expression(e, parts[1])); + return e; } else { throw new Error(Errors.SYNTAX_ERROR); } } - public static void solve() throws Error { - if (Calculator.currentSession == 0 && Calculator.sessions[0] instanceof EquationScreen) { - EquationScreen es = (EquationScreen) Calculator.sessions[0]; - List results = new ArrayList<>(); - List partialResults = new ArrayList<>(); - for (Function f : es.f) { - if (f instanceof Equation) { - PIDisplay.INSTANCE.setScreen(new SolveEquationScreen(es)); - } else { - results.add(f); - while (Utils.allSolved(results) == false) { - for (Function itm : results) { - if (itm.isSolved() == false) { - List dt = itm.solveOneStep(); - partialResults.addAll(dt); - } else { - partialResults.add(itm); - } + public static void solve(EquationScreen es) throws Error { + List results = new ArrayList<>(); + List partialResults = new ArrayList<>(); + for (Function f : es.f) { + if (f instanceof Equation) { + PIDisplay.INSTANCE.setScreen(new SolveEquationScreen(es)); + } else { + results.add(f); + while (Utils.allSolved(results) == false) { + for (Function itm : results) { + if (itm.isSolved() == false) { + List dt = itm.solveOneStep(); + partialResults.addAll(dt); + } else { + partialResults.add(itm); } - results = new ArrayList(partialResults); - partialResults.clear(); } + results = new ArrayList(partialResults); + partialResults.clear(); } } - if (results.size() == 0) { - - } else { - Collections.reverse(results); -// // add elements to al, including duplicates -// Set hs = new LinkedHashSet<>(); -// hs.addAll(results); -// results.clear(); -// results.addAll(hs); - es.f2 = results; - for (Function rf : es.f2) { - rf.generateGraphics(); - } + } + if (results.size() == 0) { + es.resultsCount = 0; + } else { + es.resultsCount = results.size(); + Collections.reverse(results); + // add elements to al, including duplicates + Set hs = new LinkedHashSet<>(); + hs.addAll(results); + results.clear(); + results.addAll(hs); + es.f2 = results; + for (Function rf : es.f2) { + rf.generateGraphics(); } } } - public static void solve(char letter) throws Error { + public static void solve(EquationScreen equationScreen, char letter) throws Error { if (Calculator.currentSession == 0 && Calculator.sessions[0] instanceof EquationScreen) { EquationScreen es = (EquationScreen) Calculator.sessions[0]; List f = es.f; @@ -117,7 +122,7 @@ public class Calculator { } } - public static void simplify() { + public static void simplify(EquationScreen equationScreen) { } diff --git a/src/org/warp/picalculator/math/MathematicalSymbols.java b/src/org/warp/picalculator/math/MathematicalSymbols.java index bcb7c43b..9a9cacd5 100644 --- a/src/org/warp/picalculator/math/MathematicalSymbols.java +++ b/src/org/warp/picalculator/math/MathematicalSymbols.java @@ -7,7 +7,8 @@ import org.warp.picalculator.Utils; public class MathematicalSymbols { public static final String SUM = "+"; public static final String SUM_SUBTRACTION = "±"; - public static final String SUBTRACTION = "-"; + public static final String SUBTRACTION = "−"; + public static final String MINUS = "-"; public static final String MULTIPLICATION = "*"; public static final String PRIORITARY_MULTIPLICATION = "▪"; public static final String DIVISION = "/"; @@ -28,11 +29,11 @@ public class MathematicalSymbols { } public static final String[] functionsSN() { - return new String[] { SQUARE_ROOT }; + return new String[] { SQUARE_ROOT, MINUS }; } public static final String[] signums(boolean withMultiplication, boolean withPrioritaryMultiplication) { - String[] ret = new String[] { SUM, SUM_SUBTRACTION, DIVISION }; + String[] ret = new String[] { SUM, SUM_SUBTRACTION, SUBTRACTION, DIVISION }; if (withMultiplication) { ret = Utils.add(ret, MULTIPLICATION); } @@ -47,7 +48,7 @@ public class MathematicalSymbols { } public static String[] variables() { - return new String[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "♓"}; + return new String[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; } public static String[] genericSyntax() { diff --git a/src/org/warp/picalculator/math/functions/AnteriorFunction.java b/src/org/warp/picalculator/math/functions/AnteriorFunction.java index c78f8136..e7478587 100644 --- a/src/org/warp/picalculator/math/functions/AnteriorFunction.java +++ b/src/org/warp/picalculator/math/functions/AnteriorFunction.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.functions; -import static org.warp.picalculator.device.graphicengine.Display.Render.getStringWidth; +import static org.warp.picalculator.device.graphicengine.Display.Render.glGetStringWidth; import static org.warp.picalculator.device.graphicengine.Display.Render.glDrawStringLeft; import java.util.List; @@ -14,16 +14,18 @@ import org.warp.picalculator.device.graphicengine.Display; import com.rits.cloning.Cloner; public abstract class AnteriorFunction implements Function { - public AnteriorFunction(Function value) { + public AnteriorFunction(Function parent, Function value) { + setParent(parent); setVariable(value); } - protected Function variable = new Number(NumeroAvanzatoVec.ZERO); + protected Function parent; + protected Function variable = new Number(null, NumeroAvanzatoVec.ZERO); protected int width; protected int height; protected int line; protected boolean small; - + public Function getVariable() { return variable; } @@ -32,6 +34,15 @@ public abstract class AnteriorFunction implements Function { variable = value; } + public Function getParent() { + return parent; + } + + public Function setParent(Function value) { + parent = value; + return this; + } + @Override public abstract String getSymbol(); @@ -50,7 +61,7 @@ public abstract class AnteriorFunction implements Function { variable.setSmall(small); variable.generateGraphics(); - width = getStringWidth(getSymbol()) + 1 + getVariable().getWidth(); + width = glGetStringWidth(getSymbol()) + 1 + getVariable().getWidth(); height = variable.getHeight(); line = variable.getLine(); } @@ -58,10 +69,10 @@ public abstract class AnteriorFunction implements Function { @Override public void draw(int x, int y) { float h1 = getVariable().getHeight(); - int wsegno = getStringWidth(getSymbol()); + int wsegno = glGetStringWidth(getSymbol()); float hsegno = Utils.getFontHeight(small); float maxh = getHeight(); - Display.Render.setFont(Utils.getFont(small)); + Display.Render.glSetFont(Utils.getFont(small)); glDrawStringLeft(x, (int) Math.floor(y + (maxh - hsegno) / 2), getSymbol()); getVariable().draw(x + wsegno + 1, (int) Math.floor(y + (maxh - h1) / 2)); diff --git a/src/org/warp/picalculator/math/functions/Division.java b/src/org/warp/picalculator/math/functions/Division.java index be581a51..3336fb5b 100644 --- a/src/org/warp/picalculator/math/functions/Division.java +++ b/src/org/warp/picalculator/math/functions/Division.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.functions; -import static org.warp.picalculator.device.graphicengine.Display.Render.getStringWidth; +import static org.warp.picalculator.device.graphicengine.Display.Render.glGetStringWidth; import static org.warp.picalculator.device.graphicengine.Display.Render.glColor3f; import static org.warp.picalculator.device.graphicengine.Display.Render.glDrawStringLeft; import static org.warp.picalculator.device.graphicengine.Display.Render.glFillRect; @@ -16,11 +16,17 @@ import org.warp.picalculator.device.PIDisplay; import org.warp.picalculator.device.graphicengine.Display; import org.warp.picalculator.device.graphicengine.Display.Render; import org.warp.picalculator.math.MathematicalSymbols; +import org.warp.picalculator.math.rules.FractionsRule1; +import org.warp.picalculator.math.rules.FractionsRule2; +import org.warp.picalculator.math.rules.FractionsRule3; +import org.warp.picalculator.math.rules.NumberRule1; +import org.warp.picalculator.math.rules.NumberRule2; +import org.warp.picalculator.math.rules.UndefinedRule2; public class Division extends FunctionTwoValues { - public Division(Function value1, Function value2) { - super(value1, value2); + public Division(Function parent, Function value1, Function value2) { + super(parent, value1, value2); } @Override @@ -31,21 +37,24 @@ public class Division extends FunctionTwoValues { @Override protected boolean isSolvable() throws Error { - if (variable1 instanceof Number & variable2 instanceof Number) { - if (((Number)variable2).getTerm().compareTo(NumeroAvanzatoVec.ZERO) == 0) { - throw new Error(Errors.DIVISION_BY_ZERO); - } else { - return true; - } - } + if (FractionsRule1.compare(this)) return true; + if (FractionsRule2.compare(this)) return true; + if (FractionsRule3.compare(this)) return true; + if (UndefinedRule2.compare(this)) return true; return false; } @Override public List solveOneStep() throws Error { ArrayList result = new ArrayList<>(); - if (variable1.isSolved() & variable2.isSolved()) { - result.add(((Number) variable1).divide((Number)variable2)); + if (FractionsRule1.compare(this)) { + result = FractionsRule1.execute(this); + } else if (FractionsRule2.compare(this)) { + result = FractionsRule2.execute(this); + } else if (FractionsRule3.compare(this)) { + result = FractionsRule3.execute(this); + } else if (UndefinedRule2.compare(this)) { + result = UndefinedRule2.execute(this); } else { List l1 = new ArrayList(); List l2 = new ArrayList(); @@ -63,7 +72,7 @@ public class Division extends FunctionTwoValues { Function[][] results = Utils.joinFunctionsResults(l1, l2); for (Function[] f : results) { - result.add(new Division((Function)f[0], (Function)f[1])); + result.add(new Division(this.parent, (Function)f[0], (Function)f[1])); } } return result; @@ -117,10 +126,10 @@ public class Division extends FunctionTwoValues { } int w1 = 0; int h1 = 0; - Display.Render.setFont(Utils.getFont(small)); + Display.Render.glSetFont(Utils.getFont(small)); if (minus) { - w1 = getStringWidth(numerator); - h1 = Render.getFontHeight(); + w1 = glGetStringWidth(numerator); + h1 = Render.glGetCurrentFontHeight(); } else { w1 = ((Function) var1).getWidth(); h1 = ((Function) var1).getHeight(); @@ -133,8 +142,8 @@ public class Division extends FunctionTwoValues { maxw = 1 + w2; } if (minus && drawMinus) { - minusw = getStringWidth("-") + 1; - minush = Render.getFontHeight(); + minusw = glGetStringWidth("-") + 1; + minush = Render.glGetCurrentFontHeight(); glDrawStringLeft(x+1, y + h1 + 1 + 1 - (minush / 2), "-"); glDrawStringLeft((int) (x+1 + minusw + 1 + (maxw - w1) / 2d), y, numerator); } else { @@ -189,7 +198,7 @@ public class Division extends FunctionTwoValues { } int w1 = 0; if (minus) { - w1 = getStringWidth(numerator); + w1 = glGetStringWidth(numerator); } else { w1 = variable1.getWidth(); } @@ -201,7 +210,7 @@ public class Division extends FunctionTwoValues { maxw = w2+1; } if (minus && drawMinus) { - return 1 + getStringWidth("-") + 1 + maxw; + return 1 + glGetStringWidth("-") + 1 + maxw; } else { return 1 + maxw; } diff --git a/src/org/warp/picalculator/math/functions/EmptyNumber.java b/src/org/warp/picalculator/math/functions/EmptyNumber.java new file mode 100644 index 00000000..438b2527 --- /dev/null +++ b/src/org/warp/picalculator/math/functions/EmptyNumber.java @@ -0,0 +1,66 @@ +package org.warp.picalculator.math.functions; + +import java.util.List; + +import org.warp.picalculator.Error; +import org.warp.picalculator.device.graphicengine.Display; + +public class EmptyNumber implements Function { + + @Override + public String getSymbol() { + return " "; + } + + @Override + public List solveOneStep() throws Error { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean isSolved() throws Error { + return false; + } + + @Override + public void generateGraphics() { + + } + + @Override + public void draw(int x, int y) { + Display.Render.glDrawStringLeft(x, y, " "); + } + + @Override + public int getWidth() { + return Display.Render.glGetStringWidth(" "); + } + + @Override + public int getHeight() { + return Display.Render.glGetCurrentFontHeight(); + } + + @Override + public int getLine() { + return Display.Render.glGetCurrentFontHeight()/2; + } + + @Override + public Function setParent(Function parent) { + return null; + } + + @Override + public Function getParent() { + return null; + } + + @Override + public void setSmall(boolean small) { + + } + +} diff --git a/src/org/warp/picalculator/math/functions/Expression.java b/src/org/warp/picalculator/math/functions/Expression.java index 4b41c1b1..5a60c9a8 100644 --- a/src/org/warp/picalculator/math/functions/Expression.java +++ b/src/org/warp/picalculator/math/functions/Expression.java @@ -20,31 +20,35 @@ import org.warp.picalculator.math.MathematicalSymbols; import org.warp.picalculator.math.Variable; import org.warp.picalculator.math.Variables; +import sun.reflect.generics.reflectiveObjects.NotImplementedException; + public class Expression extends FunctionMultipleValues { - public Expression() { - super(); + public Expression(Function parent) { + super(parent); } - - public Expression(Function[] values) { - super(values); + + public Expression(Function parent, Function[] values) { + super(parent, values); } private boolean initialParenthesis = false; + private Function parent = null; - public Expression(String string) throws Error { - this(string, "", true); + public Expression(Function parent, String string) throws Error { + this(parent, string, "", true); } - public Expression(String string, String debugSpaces, boolean initialParenthesis) throws Error { - super(); + public Expression(Function parent, String string, String debugSpaces, boolean initialParenthesis) throws Error { + super(parent); + this.parent = parent; this.initialParenthesis = initialParenthesis; boolean isNumber = false; // Determine if the expression is already a number: // Determina se l'espressione è già un numero: try { - new Number(string); + new Number(this, string); isNumber = true; } catch (NumberFormatException ex) { isNumber = false; @@ -53,10 +57,12 @@ public class Expression extends FunctionMultipleValues { String processExpression = string; Utils.debug.println(debugSpaces + "•Analyzing expression:" + processExpression); + isNumber = false; //TODO: rimuovere isNumber, alcune semplificazione come la divisione per zero altrimenti verrebbero saltate. + if (isNumber){ // If the expression is already a number: // Se l'espressione è già un numero: - Number t = new Number(string); + Number t = new Number(this, string); setVariables(new Function[] { t }); Utils.debug.println(debugSpaces + "•Result:" + t.toString()); } else { @@ -95,7 +101,7 @@ public class Expression extends FunctionMultipleValues { while (matcher.find()) { symbolsChanged = true; String correzione = "+"; - processExpression = processExpression.substring(0, matcher.start(0) + 1) + correzione + processExpression.substring(matcher.start(0) + matcher.group(0).length(), processExpression.length()); + processExpression = processExpression.substring(0, matcher.start(0)) + correzione + processExpression.substring(matcher.start(0) + matcher.group(0).length(), processExpression.length()); matcher = pattern.matcher(processExpression); } @@ -116,11 +122,12 @@ public class Expression extends FunctionMultipleValues { processExpression = processExpression.replace("(+", "("); } - // Cambia i segni appena prima le parentesi - if (processExpression.contains("-(")) { - symbolsChanged = true; - processExpression = processExpression.replace("-(", "-1*("); - } +// // Cambia i segni appena prima le parentesi +// if (processExpression.contains("-(")) { +// symbolsChanged = true; +// processExpression = processExpression.replace("-(", "-1*("); +// } + // Rimuovi i segni appena dopo l'inizio if (processExpression.startsWith("+")) { symbolsChanged = true; @@ -137,25 +144,46 @@ public class Expression extends FunctionMultipleValues { processExpression = processExpression.substring(0, matcher.start(0) + 1) + correzione + processExpression.substring(matcher.start(0) + matcher.group(0).length(), processExpression.length()); matcher = pattern.matcher(processExpression); } + + // Correggi i segni - in − + processExpression = processExpression.replace("-", MathematicalSymbols.SUBTRACTION); - // Correggi i segni - in +- - pattern = Pattern.compile("[^" + Utils.ArrayToRegex(concat(concat(MathematicalSymbols.functions(), new String[] { MathematicalSymbols.PARENTHESIS_OPEN }), MathematicalSymbols.signums(true, true))) + "]-"); + // Correggi i segni − dopo di espressioni o funzioni SN in - + pattern = Pattern.compile("[" + Utils.ArrayToRegex(concat(concat(concat(MathematicalSymbols.functions(), MathematicalSymbols.variables()), new String[] { MathematicalSymbols.PARENTHESIS_OPEN }), MathematicalSymbols.signums(true, true))) + "]" + MathematicalSymbols.SUBTRACTION); matcher = pattern.matcher(processExpression); while (matcher.find()) { symbolsChanged = true; - String correzione = "+-"; - processExpression = processExpression.substring(0, matcher.start(0) + 1) + correzione + processExpression.substring(matcher.start(0) + matcher.group(0).length(), processExpression.length()); + String correzione = MathematicalSymbols.MINUS; + processExpression = processExpression.substring(0, matcher.start(0) + 1) + correzione + processExpression.substring(matcher.start(0) + 2, processExpression.length()); matcher = pattern.matcher(processExpression); } + + // Cambia il segno iniziale − in - + if (processExpression.startsWith("−")) { + symbolsChanged = true; + processExpression = "-" + processExpression.substring(1, processExpression.length()); + } if (symbolsChanged) { Utils.debug.println(debugSpaces + "•Resolved signs:" + processExpression); } + // Aggiungi le parentesi implicite per le potenze con una incognita + pattern = Pattern.compile("(? 0) { - imputRawParenthesis.addVariableToEnd(new Number(tmp)); + imputRawParenthesis.addVariableToEnd(new Number(this, tmp)); Utils.debug.println(debugSpaces + "•Added value to expression:" + tmp); - imputRawParenthesis.addVariableToEnd(new PrioritaryMultiplication(null, null)); - Utils.debug.println(debugSpaces + "•Added variable to expression:" + new PrioritaryMultiplication(null, null).getSymbol()); + imputRawParenthesis.addVariableToEnd(new PrioritaryMultiplication(this, null, null)); + Utils.debug.println(debugSpaces + "•Added variable to expression:" + new PrioritaryMultiplication(this, null, null).getSymbol()); } } else { if (tmp.length() > 0) { if (imputRawParenthesis.getVariable(imputRawParenthesis.getVariablesLength() - 1) instanceof Number) { - imputRawParenthesis.addVariableToEnd(new PrioritaryMultiplication(null, null)); - Utils.debug.println(debugSpaces + "•Added variable to expression:" + new PrioritaryMultiplication(null, null).getSymbol()); + imputRawParenthesis.addVariableToEnd(new PrioritaryMultiplication(this, null, null)); + Utils.debug.println(debugSpaces + "•Added variable to expression:" + new PrioritaryMultiplication(this, null, null).getSymbol()); } if (tmp.equals("-")) { - tmp = "-1"; + imputRawParenthesis.addVariableToEnd(new Subtraction(this, null, null)); + } else { + imputRawParenthesis.addVariableToEnd(new Number(this, tmp)); + Utils.debug.println(debugSpaces + "•Added value to expression:" + tmp); } - imputRawParenthesis.addVariableToEnd(new Number(tmp)); - Utils.debug.println(debugSpaces + "•Added value to expression:" + tmp); } if (tmp.length() > 0 || imputRawParenthesis.getVariable(imputRawParenthesis.getVariablesLength() - 1) instanceof Number) { - imputRawParenthesis.addVariableToEnd(new PrioritaryMultiplication(null, null)); - Utils.debug.println(debugSpaces + "•Added variable to expression:" + new PrioritaryMultiplication(null, null).getSymbol()); + imputRawParenthesis.addVariableToEnd(new PrioritaryMultiplication(this, null, null)); + Utils.debug.println(debugSpaces + "•Added variable to expression:" + new PrioritaryMultiplication(this, null, null).getSymbol()); } } } else { if (tmp.length() != 0) { - imputRawParenthesis.addVariableToEnd(new Number(tmp)); + if (tmp.equals("-")) { + if (tmp.equals("-")) { + tmp = "-1"; + } + } + imputRawParenthesis.addVariableToEnd(new Number(this, tmp)); Utils.debug.println(debugSpaces + "•Added variable to expression:" + tmp); } } @@ -322,7 +362,7 @@ public class Expression extends FunctionMultipleValues { if (tmp.length() > 0) { Utils.debug.println(debugSpaces + "•Added variable to expression:" + tmp); try { - imputRawParenthesis.addVariableToEnd(new Number(tmp)); + imputRawParenthesis.addVariableToEnd(new Number(this, tmp)); } catch (NumberFormatException ex) { throw new Error(Errors.SYNTAX_ERROR); } @@ -366,7 +406,7 @@ public class Expression extends FunctionMultipleValues { oldFunctionsArray[i-1] = null; oldFunctionsList.remove(oldFunctionsList.size()-1); i -= 1; - funzione = new RootSquare(null); + funzione = new RootSquare(this, null); } } //Aggiunta della funzione alla lista grezza @@ -399,7 +439,7 @@ public class Expression extends FunctionMultipleValues { System.out.println("WARN: ---> POSSIBILE ERRORE????? <---");// BOH // throw new Errore(Errori.SYNTAX_ERROR); while (oldFunctionsList.size() > 1) { - oldFunctionsList.set(0, new Multiplication(oldFunctionsList.get(0), oldFunctionsList.remove(1))); + oldFunctionsList.set(0, new Multiplication(this, oldFunctionsList.get(0), oldFunctionsList.remove(1))); } } Utils.debug.println(debugSpaces + " •Phase: "+step); @@ -408,7 +448,7 @@ public class Expression extends FunctionMultipleValues { if (funzioneTMP instanceof FunctionTwoValues) { if (step != "SN Functions") { if ( - (step == "sums" && (funzioneTMP instanceof Sum || funzioneTMP instanceof SumSubtraction) == true && ((funzioneTMP instanceof AnteriorFunction && ((AnteriorFunction) funzioneTMP).variable == null) || (funzioneTMP instanceof FunctionTwoValues && ((FunctionTwoValues) funzioneTMP).variable1 == null && ((FunctionTwoValues) funzioneTMP).variable2 == null) || (!(funzioneTMP instanceof AnteriorFunction) && !(funzioneTMP instanceof FunctionTwoValues)))) + (step == "sums" && (funzioneTMP instanceof Sum || funzioneTMP instanceof SumSubtraction || funzioneTMP instanceof Subtraction) == true && ((funzioneTMP instanceof AnteriorFunction && ((AnteriorFunction) funzioneTMP).variable == null) || (funzioneTMP instanceof FunctionTwoValues && ((FunctionTwoValues) funzioneTMP).variable1 == null && ((FunctionTwoValues) funzioneTMP).variable2 == null) || (!(funzioneTMP instanceof AnteriorFunction) && !(funzioneTMP instanceof FunctionTwoValues)))) || ( step.equals("prioritary multiplications") @@ -441,6 +481,8 @@ public class Expression extends FunctionMultipleValues { && (funzioneTMP instanceof SumSubtraction) == false && + (funzioneTMP instanceof Subtraction) == false + && (funzioneTMP instanceof Multiplication) == false && (funzioneTMP instanceof PrioritaryMultiplication) == false @@ -580,7 +622,7 @@ public class Expression extends FunctionMultipleValues { if (f instanceof Number) { ret.add(f); } else { - ret.add(new Expression(new Function[]{(Function) f})); + ret.add(new Expression(this, new Function[]{(Function) f})); } } return ret; @@ -590,7 +632,7 @@ public class Expression extends FunctionMultipleValues { if (f.isSolved() == false) { List partial = f.solveOneStep(); for (Function fnc : partial) { - ret.add(new Expression(new Function[]{(Function) fnc})); + ret.add(new Expression(this, new Function[]{(Function) fnc})); } } } @@ -612,12 +654,18 @@ public class Expression extends FunctionMultipleValues { public boolean parenthesesNeeded() { boolean parenthesesneeded = true; - if (initialParenthesis) { + if (initialParenthesis | parent instanceof Division) { parenthesesneeded = false; } else { if (variables.length == 1) { if (variables[0] instanceof Division) { parenthesesneeded = false; + } else if (variables[0] instanceof Power) { + if (((Power)variables[0]).getVariable1() instanceof Number && ((Number)((Power)variables[0]).getVariable1()).soloIncognitaSemplice()) { + parenthesesneeded = false; + } else { + parenthesesneeded = true; + } } else { parenthesesneeded = true; } diff --git a/src/org/warp/picalculator/math/functions/Function.java b/src/org/warp/picalculator/math/functions/Function.java index 3158bf57..a03164c7 100644 --- a/src/org/warp/picalculator/math/functions/Function.java +++ b/src/org/warp/picalculator/math/functions/Function.java @@ -21,6 +21,10 @@ public interface Function { public int getLine(); + public Function setParent(Function parent); + + public Function getParent(); + public void setSmall(boolean small); @Override diff --git a/src/org/warp/picalculator/math/functions/FunctionMultipleValues.java b/src/org/warp/picalculator/math/functions/FunctionMultipleValues.java index 6fc5da21..1ceff97f 100644 --- a/src/org/warp/picalculator/math/functions/FunctionMultipleValues.java +++ b/src/org/warp/picalculator/math/functions/FunctionMultipleValues.java @@ -8,14 +8,17 @@ import org.warp.picalculator.Error; import com.rits.cloning.Cloner; public abstract class FunctionMultipleValues implements Function { - public FunctionMultipleValues() { + public FunctionMultipleValues(Function parent) { + setParent(parent); setVariables(new Function[] {}); } - public FunctionMultipleValues(Function[] values) { + public FunctionMultipleValues(Function parent, Function[] values) { + setParent(parent); setVariables(values); } + protected Function parent; protected Function[] variables; protected int width; protected int height; @@ -27,6 +30,9 @@ public abstract class FunctionMultipleValues implements Function { } public void setVariables(Function[] value) { + for (Function f : value) { + f.setParent(this); + } variables = value; } @@ -35,6 +41,7 @@ public abstract class FunctionMultipleValues implements Function { Function[] tmp = new Function[vsize]; for (int i = 0; i < vsize; i++) { tmp[i] = value.get(i); + tmp[i].setParent(this); } variables = tmp; } @@ -44,10 +51,12 @@ public abstract class FunctionMultipleValues implements Function { } public void setVariable(int index, Function value) { + value.setParent(this); variables[index] = value; } public void addVariableToEnd(Function value) { + value.setParent(this); int index = variables.length; setVariablesLength(index + 1); variables[index] = value; @@ -76,6 +85,16 @@ public abstract class FunctionMultipleValues implements Function { protected abstract boolean isSolvable() throws Error; + + public Function setParent(Function value) { + parent = value; + return this; + } + + public Function getParent() { + return parent; + } + @Override public abstract void generateGraphics(); diff --git a/src/org/warp/picalculator/math/functions/FunctionTwoValues.java b/src/org/warp/picalculator/math/functions/FunctionTwoValues.java index e8fbb093..6a4652c9 100644 --- a/src/org/warp/picalculator/math/functions/FunctionTwoValues.java +++ b/src/org/warp/picalculator/math/functions/FunctionTwoValues.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.functions; -import static org.warp.picalculator.device.graphicengine.Display.Render.getStringWidth; +import static org.warp.picalculator.device.graphicengine.Display.Render.glGetStringWidth; import static org.warp.picalculator.device.graphicengine.Display.Render.glDrawStringLeft; import java.util.List; @@ -14,12 +14,16 @@ import org.warp.picalculator.device.graphicengine.Display; import com.rits.cloning.Cloner; public abstract class FunctionTwoValues implements Function { - public FunctionTwoValues(Function value1, Function value2) { - setVariable1(value1); - setVariable2(value2); + public FunctionTwoValues(Function parent, Function value1, Function value2) { + this.parent = parent; + variable1 = value1; + variable2 = value2; } - protected Function variable1 = (Function) new Number(Rational.ZERO); + protected Function parent; + + protected Function variable1 = (Function) new Number(null, Rational.ZERO); + protected Function variable2 = (Function) new Number(null, Rational.ZERO); protected int width; protected int height; protected int line; @@ -30,16 +34,25 @@ public abstract class FunctionTwoValues implements Function { } public void setVariable1(Function value) { + value.setParent(this); variable1 = value; } - protected Function variable2 = (Function) new Number(Rational.ZERO); + public Function getParent() { + return parent; + } + + public Function setParent(Function value) { + parent = value; + return this; + } public Function getVariable2() { return variable2; } public void setVariable2(Function value) { + value.setParent(this); variable2 = value; } @@ -76,9 +89,9 @@ public abstract class FunctionTwoValues implements Function { variable1.draw(dx + x, ln - variable1.getLine() + y); dx += 1+variable1.getWidth(); if (drawSignum()) { - Display.Render.setFont(Utils.getFont(small)); + Display.Render.glSetFont(Utils.getFont(small)); glDrawStringLeft(dx + x, ln - Utils.getFontHeight(small) / 2 + y, getSymbol()); - dx += getStringWidth(getSymbol()); + dx += glGetStringWidth(getSymbol()); } variable2.draw(dx + x, ln - variable2.getLine() + y); } @@ -124,7 +137,7 @@ public abstract class FunctionTwoValues implements Function { } protected int calcWidth() { - return variable1.getWidth() + 1 + (drawSignum() ? getStringWidth(getSymbol()) : 0) + variable2.getWidth(); + return variable1.getWidth() + 1 + (drawSignum() ? glGetStringWidth(getSymbol()) : 0) + variable2.getWidth(); } protected int calcHeight() { diff --git a/src/org/warp/picalculator/math/functions/Joke.java b/src/org/warp/picalculator/math/functions/Joke.java new file mode 100644 index 00000000..01921f9c --- /dev/null +++ b/src/org/warp/picalculator/math/functions/Joke.java @@ -0,0 +1,93 @@ +package org.warp.picalculator.math.functions; + +import java.util.List; + +import org.warp.picalculator.Error; +import org.warp.picalculator.Utils; +import org.warp.picalculator.device.PIDisplay; +import org.warp.picalculator.device.graphicengine.Display; +import org.warp.picalculator.device.graphicengine.RAWFont; + +public class Joke implements Function { + + public static final byte FISH = 0; + public static final byte TORNADO = 1; + public static final byte SHARKNADO = 2; + private static final String[] jokes = new String[]{"♓", "TORNADO", "SHARKNADO"}; + private static final int[] jokesFont = new int[]{4, -1, -1}; + private final byte joke; + + public Joke(byte joke) { + this.joke = joke; + } + + @Override + public String getSymbol() { + return "joke"; + } + + @Override + public List solveOneStep() throws Error { + return null; + } + + @Override + public boolean isSolved() throws Error { + return true; + } + + @Override + public void generateGraphics() { + + } + + @Override + public void draw(int x, int y) { + RAWFont rf = Display.Render.currentFont; + if (jokesFont[joke] >= 0) { + Display.Render.glSetFont(PIDisplay.fonts[jokesFont[joke]]); + } + Display.Render.glDrawStringLeft(x, y, jokes[joke]); + if (jokesFont[joke] >= 0) { + Display.Render.glSetFont(rf); + } + } + + @Override + public int getWidth() { + if (jokesFont[joke] >= 0) { + return Display.Render.glGetStringWidth(PIDisplay.fonts[jokesFont[joke]], jokes[joke]); + } else { + return Display.Render.glGetStringWidth(jokes[joke]); + } + } + + @Override + public int getHeight() { + if (jokesFont[joke] >= 0) { + return PIDisplay.fonts[jokesFont[joke]].charH; + } else { + return Display.Render.glGetCurrentFontHeight(); + } + } + + @Override + public int getLine() { + return getHeight()/2; + } + + @Override + public Function setParent(Function value) { + return this; + } + + @Override + public Function getParent() { + return null; + } + + @Override + public void setSmall(boolean small) { + } + +} diff --git a/src/org/warp/picalculator/math/functions/Multiplication.java b/src/org/warp/picalculator/math/functions/Multiplication.java index 395ac98b..25f9ae4a 100644 --- a/src/org/warp/picalculator/math/functions/Multiplication.java +++ b/src/org/warp/picalculator/math/functions/Multiplication.java @@ -9,11 +9,13 @@ import org.warp.picalculator.Errors; import org.warp.picalculator.Utils; import org.warp.picalculator.math.MathematicalSymbols; import org.warp.picalculator.math.Variable; +import org.warp.picalculator.math.rules.NumberRule1; +import org.warp.picalculator.math.rules.NumberRule2; public class Multiplication extends FunctionTwoValues { - public Multiplication(Function value1, Function value2) { - super(value1, value2); + public Multiplication(Function parent, Function value1, Function value2) { + super(parent, value1, value2); } @Override @@ -26,13 +28,19 @@ public class Multiplication extends FunctionTwoValues { if (variable1 instanceof Number & variable2 instanceof Number) { return true; } + if (NumberRule1.compare(this)) return true; + if (NumberRule2.compare(this)) return true; return false; } @Override public List solveOneStep() throws Error { - ArrayList result = new ArrayList<>(); - if (variable1.isSolved() & variable2.isSolved()) { + List 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()) { result.add(((Number)variable1).multiply((Number)variable2)); } else { List l1 = new ArrayList(); @@ -51,7 +59,7 @@ public class Multiplication extends FunctionTwoValues { Function[][] results = Utils.joinFunctionsResults(l1, l2); for (Function[] f : results) { - result.add(new Multiplication(f[0], f[1])); + result.add(new Multiplication(this.parent, f[0], f[1])); } } return result; @@ -93,9 +101,19 @@ public class Multiplication extends FunctionTwoValues { } else if (tmpVar[val] instanceof Power) { tmpVar[val] = ((Power) tmpVar[val]).variable1; } else if (tmpVar[val] instanceof Root) { + if (val == 0) { + break; + } ok[val] = true; } else if (tmpVar[val] instanceof RootSquare) { + if (val == 0) { + break; + } ok[val] = true; + } else if (tmpVar[val] instanceof Undefined) { + break; + } else if (tmpVar[val] instanceof Joke) { + break; } else if (tmpVar[val] instanceof Expression) { ok[0] = true; ok[1] = true; diff --git a/src/org/warp/picalculator/math/functions/Negative.java b/src/org/warp/picalculator/math/functions/Negative.java new file mode 100644 index 00000000..a4484782 --- /dev/null +++ b/src/org/warp/picalculator/math/functions/Negative.java @@ -0,0 +1,96 @@ +package org.warp.picalculator.math.functions; + +import java.util.ArrayList; +import java.util.List; + +import org.nevec.rjm.NumeroAvanzatoVec; +import org.nevec.rjm.Rational; +import org.warp.picalculator.Error; +import org.warp.picalculator.Errors; +import org.warp.picalculator.Utils; +import org.warp.picalculator.device.graphicengine.Display; +import org.warp.picalculator.math.MathematicalSymbols; +import org.warp.picalculator.math.rules.NumberRule1; +import org.warp.picalculator.math.rules.ExpandRule1; +import org.warp.picalculator.math.rules.ExpandRule5; + +public class Negative extends AnteriorFunction { + + public Negative(Function parent, Function value) { + super(parent, value); + } + + @Override + public String getSymbol() { + return MathematicalSymbols.MINUS; + } + + @Override + public void generateGraphics() { + variable.setSmall(small); + variable.generateGraphics(); + + height = getVariable().getHeight(); + width = Display.Render.glGetStringWidth("-") + getVariable().getWidth(); + line = getVariable().getLine(); + } + + @Override + protected boolean isSolvable() throws Error { + if (variable instanceof Number) return true; + if (ExpandRule1.compare(this)) return true; + if (ExpandRule5.compare(this)) return true; + return false; + } + + @Override + public List solveOneStep() throws Error { + if (variable == null) { + throw new Error(Errors.SYNTAX_ERROR); + } + ArrayList result = new ArrayList<>(); + if (ExpandRule1.compare(this)) { + result = ExpandRule1.execute(this); + } else if (ExpandRule5.compare(this)) { + result = ExpandRule5.execute(this); + } else if (variable.isSolved()) { + try { + Number var = (Number) getVariable(); + result.add(var.multiply(new Number(null, "-1"))); + } 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 l1 = new ArrayList(); + if (variable.isSolved()) { + l1.add(variable); + } else { + l1.addAll(variable.solveOneStep()); + } + + for (Function f : l1) { + result.add(new Negative(this.parent, (Function)f)); + } + } + return result; + } + + @Override + public int getWidth() { + return width; + } + + @Override + public int getHeight() { + return height; + } + + @Override + public int getLine() { + return line; + } +} diff --git a/src/org/warp/picalculator/math/functions/Number.java b/src/org/warp/picalculator/math/functions/Number.java index 19caa1fd..df8402b0 100644 --- a/src/org/warp/picalculator/math/functions/Number.java +++ b/src/org/warp/picalculator/math/functions/Number.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.functions; -import static org.warp.picalculator.device.graphicengine.Display.Render.getStringWidth; +import static org.warp.picalculator.device.graphicengine.Display.Render.glGetStringWidth; import static org.warp.picalculator.device.graphicengine.Display.Render.glColor3f; import static org.warp.picalculator.device.graphicengine.Display.Render.glDrawStringLeft; import static org.warp.picalculator.device.graphicengine.Display.Render.glFillRect; @@ -25,33 +25,40 @@ import com.rits.cloning.Cloner; public class Number implements Function { + private Function parent; protected NumeroAvanzatoVec term = NumeroAvanzatoVec.ZERO; protected int width; protected int height; protected int line; protected boolean small; - public Number(NumeroAvanzatoVec val) { + public Number(Function parent, NumeroAvanzatoVec val) { + this.parent = parent; term = val; } - public Number(String s) throws Error { + public Number(Function parent, String s) throws Error { + this.parent = parent; term = new NumeroAvanzatoVec(new NumeroAvanzato(Utils.getRational(s), Rational.ONE)); } - public Number(Rational r) { + public Number(Function parent, Rational r) { + this.parent = parent; term = new NumeroAvanzatoVec(new NumeroAvanzato(r, Rational.ONE)); } - public Number(BigInteger r) { + public Number(Function parent, BigInteger r) { + this.parent = parent; term = new NumeroAvanzatoVec(new NumeroAvanzato(new Rational(r, BigInteger.ONE), Rational.ONE)); } - public Number(BigDecimal r) { + public Number(Function parent, BigDecimal r) { + this.parent = parent; term = new NumeroAvanzatoVec(new NumeroAvanzato(Utils.getRational(r), Rational.ONE)); } - public Number(NumeroAvanzato numeroAvanzato) { + public Number(Function parent, NumeroAvanzato numeroAvanzato) { + this.parent = parent; term = new NumeroAvanzatoVec(numeroAvanzato); } @@ -76,25 +83,25 @@ public class Number implements Function { } public Number add(Number f) throws Error { - Number ret = new Number(getTerm().add(f.getTerm())); + Number ret = new Number(this.parent, getTerm().add(f.getTerm())); return ret; } public Number multiply(Number f) throws Error { - Number ret = new Number(getTerm().multiply(f.getTerm())); + Number ret = new Number(this.parent, getTerm().multiply(f.getTerm())); return ret; } public Number divide(Number f) throws Error { - Number ret = new Number(getTerm().divide(f.getTerm())); + Number ret = new Number(this.parent, getTerm().divide(f.getTerm())); return ret; } public Number pow(Number f) throws Error { - Number ret = new Number(NumeroAvanzatoVec.ONE); + Number ret = new Number(this.parent, NumeroAvanzatoVec.ONE); if (f.getTerm().isBigInteger(true)) { for (BigInteger i = BigInteger.ZERO; i.compareTo(f.getTerm().toBigInteger(true)) < 0; i = i.add(BigInteger.ONE)) { - ret = ret.multiply(new Number(getTerm())); + ret = ret.multiply(new Number(this.parent, getTerm())); } } else if (getTerm().isRational(true) && f.getTerm().isRational(false) && f.getTerm().toRational(false).compareTo(Rational.HALF) == 0) { // Rational originalExponent = f.getTerm().toRational(); @@ -105,9 +112,9 @@ public class Number implements Function { na = na.setVariableX(getTerm().toNumeroAvanzato().getVariableY().multiply(getTerm().toNumeroAvanzato().getVariableZ())); na = na.setVariableY(new Variables()); na = na.setVariableZ(getTerm().toNumeroAvanzato().getVariableZ()); - ret = new Number(na); + ret = new Number(this.parent, na); } else { - ret = new Number(BigDecimalMath.pow(getTerm().BigDecimalValue(new MathContext(Utils.scale, Utils.scaleMode2)), f.getTerm().BigDecimalValue(new MathContext(Utils.scale, Utils.scaleMode2)))); + ret = new Number(this.parent, BigDecimalMath.pow(getTerm().BigDecimalValue(new MathContext(Utils.scale, Utils.scaleMode2)), f.getTerm().BigDecimalValue(new MathContext(Utils.scale, Utils.scaleMode2)))); } return ret; } @@ -130,7 +137,7 @@ public class Number implements Function { public void draw(int x, int y) { if (getTerm().isBigInteger(false)) { - Display.Render.setFont(Utils.getFont(small)); + Display.Render.glSetFont(Utils.getFont(small)); String t = toString(); if (t.startsWith("-")) { @@ -143,7 +150,7 @@ public class Number implements Function { glDrawStringLeft(x+1, y, t); } else if (getTerm().isRational(false)) { small = true; - Display.Render.setFont(Utils.getFont(true)); + Display.Render.glSetFont(Utils.getFont(true)); Rational r = getTerm().toRational(false); boolean minus = false; int minusw = 0; @@ -153,9 +160,9 @@ public class Number implements Function { minus = true; numerator = numerator.substring(1); } - int w1 = getStringWidth(numerator); + int w1 = glGetStringWidth(numerator); int h1 = Utils.getFontHeight(small); - int w2 = getStringWidth(r.denom().toString()); + int w2 = glGetStringWidth(r.denom().toString()); int maxw; if (w1 > w2) { maxw = 1 + w1 + 1; @@ -164,7 +171,7 @@ public class Number implements Function { } if (minus) { if (drawMinus) { - minusw = getStringWidth("-"); + minusw = glGetStringWidth("-"); minush = Utils.getFontHeight(small); maxw += minusw; glDrawStringLeft(x, y + h1 + 1 + 1 - (minush / 2), "-"); @@ -176,7 +183,7 @@ public class Number implements Function { glFillRect(x + minusw + 1, y + h1 + 1, maxw, 1); } else if (getTerm().toFancyString().contains("/")) { small = true; - Display.Render.setFont(Utils.getFont(true)); + Display.Render.glSetFont(Utils.getFont(true)); String r = getTerm().toFancyString(); String numer = r.substring(0, r.lastIndexOf("/")); String denom = r.substring(numer.length() + 1, r.length()); @@ -188,9 +195,9 @@ public class Number implements Function { minus = true; numer = numer.substring(1); } - int w1 = getStringWidth(numer.toString()); + int w1 = glGetStringWidth(numer.toString()); int h1 = Utils.getFontHeight(small); - int w2 = getStringWidth(denom.toString()); + int w2 = glGetStringWidth(denom.toString()); int maxw; if (w1 > w2) { maxw = w1 + 2; @@ -201,7 +208,7 @@ public class Number implements Function { int minush = 0; if (minus) { if (drawMinus) { - minusw = getStringWidth("-") + 1; + minusw = glGetStringWidth("-") + 1; minush = Utils.getFontHeight(small); maxw += minusw; glDrawStringLeft(x, y + h1 + 1 + 1 - (minush / 2), "-"); @@ -212,7 +219,7 @@ public class Number implements Function { glColor3f(0, 0, 0); glFillRect(x + minusw + 1, y + h1 + 1, maxw, 1); } else { - Display.Render.setFont(Utils.getFont(small)); + Display.Render.glSetFont(Utils.getFont(small)); String r = getTerm().toFancyString(); if (r.startsWith("-")) { @@ -274,7 +281,7 @@ public class Number implements Function { t = t.substring(1); } } - return getStringWidth(t)+1; + return glGetStringWidth(t)+1; } else if (getTerm().isRational(false)) { Rational r = getTerm().toRational(false); boolean minus = false; @@ -283,8 +290,8 @@ public class Number implements Function { minus = true; numerator = numerator.substring(1); } - int w1 = getStringWidth(numerator); - int w2 = getStringWidth(r.denom().toString()); + int w1 = glGetStringWidth(numerator); + int w2 = glGetStringWidth(r.denom().toString()); int maxw; if (w1 > w2) { maxw = 1 + w1 + 1; @@ -293,7 +300,7 @@ public class Number implements Function { } if (minus) { if (drawMinus) { - maxw += getStringWidth("-"); + maxw += glGetStringWidth("-"); } } return maxw + 1; @@ -309,8 +316,8 @@ public class Number implements Function { minus = true; numer = numer.substring(1); } - int w1 = getStringWidth(numer.toString()); - int w2 = getStringWidth(denom.toString()); + int w1 = glGetStringWidth(numer.toString()); + int w2 = glGetStringWidth(denom.toString()); int maxw; if (w1 > w2) { maxw = w1 + 1; @@ -319,7 +326,7 @@ public class Number implements Function { } if (minus) { if (drawMinus) { - maxw += getStringWidth("-"); + maxw += glGetStringWidth("-"); } } return maxw + 2; @@ -332,7 +339,7 @@ public class Number implements Function { r = r.substring(1); } } - return getStringWidth(r.toString())+1; + return glGetStringWidth(r.toString())+1; } } @@ -344,7 +351,7 @@ public class Number implements Function { } return false; } - + @Override public int getLine() { return line; @@ -397,7 +404,34 @@ public class Number implements Function { @Override public boolean equals(Object o) { - return o != null && o.hashCode() == this.hashCode(); + if (o != null & term != null) { + if (o instanceof Number) { + NumeroAvanzatoVec nav = ((Number) o).getTerm(); + if (term.isBigInteger(true) & nav.isBigInteger(true)) { + boolean na1 = term.toBigInteger(true).compareTo(BigInteger.ZERO) == 0; + boolean na2 = nav.toBigInteger(true).compareTo(BigInteger.ZERO) == 0; + if (na1 == na2) { + if (na1 == true) { + return true; + } + } else { + return false; + } + } + return nav.compareTo(term) == 0; + } + } + return false; + } + + public Function setParent(Function value) { + parent = value; + return this; + } + + @Override + public Function getParent() { + return parent; } /* diff --git a/src/org/warp/picalculator/math/functions/Power.java b/src/org/warp/picalculator/math/functions/Power.java index 3be690ce..b5e5974b 100644 --- a/src/org/warp/picalculator/math/functions/Power.java +++ b/src/org/warp/picalculator/math/functions/Power.java @@ -7,11 +7,13 @@ 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.rules.FractionsRule4; +import org.warp.picalculator.math.rules.FractionsRule5; public class Power extends FunctionTwoValues { - public Power(Function value1, Function value2) { - super(value1, value2); + public Power(Function parent, Function value1, Function value2) { + super(parent, value1, value2); } @Override @@ -22,8 +24,13 @@ public class Power extends FunctionTwoValues { @Override protected boolean isSolvable() throws Error { if (variable1 instanceof Number & variable2 instanceof Number) { + if (((Number)variable2).getTerm().hasVariables()) { + return false; + } return true; } + if (FractionsRule4.compare(this)) return true; + if (FractionsRule5.compare(this)) return true; return false; } @@ -43,8 +50,12 @@ public class Power extends FunctionTwoValues { @Override public List solveOneStep() throws Error { ArrayList result = new ArrayList<>(); - if (variable1.isSolved() & variable2.isSolved()) { + if (variable1 instanceof Number & variable2 instanceof Number) { result.add((Function) ((Number)variable1).pow((Number)variable2)); + } else if (FractionsRule4.compare(this)) { + result.addAll(FractionsRule4.execute(this)); + } else if (FractionsRule5.compare(this)) { + result.addAll(FractionsRule5.execute(this)); } else { List l1 = new ArrayList(); List l2 = new ArrayList(); @@ -62,7 +73,7 @@ public class Power extends FunctionTwoValues { Function[][] results = Utils.joinFunctionsResults(l1, l2); for (Function[] f : results) { - result.add(new Power((Function)f[0], (Function)f[1])); + result.add(new Power(this.parent, (Function)f[0], (Function)f[1])); } } return result; diff --git a/src/org/warp/picalculator/math/functions/PrioritaryMultiplication.java b/src/org/warp/picalculator/math/functions/PrioritaryMultiplication.java index 15cf3d65..c83f976f 100644 --- a/src/org/warp/picalculator/math/functions/PrioritaryMultiplication.java +++ b/src/org/warp/picalculator/math/functions/PrioritaryMultiplication.java @@ -10,8 +10,8 @@ import org.warp.picalculator.math.MathematicalSymbols; public class PrioritaryMultiplication extends FunctionTwoValues { - public PrioritaryMultiplication(Function value1, Function value2) { - super(value1, value2); + public PrioritaryMultiplication(Function parent, Function value1, Function value2) { + super(parent, value1, value2); } @Override @@ -52,7 +52,7 @@ public class PrioritaryMultiplication extends FunctionTwoValues { Function[][] results = Utils.joinFunctionsResults(l1, l2); for (Function[] f : results) { - result.add(new PrioritaryMultiplication((Function)f[0], (Function)f[1])); + result.add(new PrioritaryMultiplication(this.parent, (Function)f[0], (Function)f[1])); } } return result; diff --git a/src/org/warp/picalculator/math/functions/Root.java b/src/org/warp/picalculator/math/functions/Root.java index 728ecf85..efaa8041 100644 --- a/src/org/warp/picalculator/math/functions/Root.java +++ b/src/org/warp/picalculator/math/functions/Root.java @@ -13,8 +13,8 @@ import org.warp.picalculator.math.MathematicalSymbols; public class Root extends FunctionTwoValues { - public Root(Function value1, Function value2) { - super(value1, value2); + public Root(Function parent, Function value1, Function value2) { + super(parent, value1, value2); } @Override @@ -38,7 +38,7 @@ public class Root extends FunctionTwoValues { @Override protected boolean isSolvable() throws Error { if (variable1 instanceof Number & variable2 instanceof Number) { - if ((((Number)variable2).pow(new Number(NumeroAvanzatoVec.ONE).divide((Number) variable1)).getTerm().isBigInteger(true))) { + if ((((Number)variable2).pow(new Number(this.parent, NumeroAvanzatoVec.ONE).divide((Number) variable1)).getTerm().isBigInteger(true))) { return true; } } @@ -49,7 +49,7 @@ public class Root extends FunctionTwoValues { public List solveOneStep() throws Error { ArrayList result = new ArrayList<>(); if (variable1.isSolved() & variable2.isSolved()) { - Number exponent = new Number(NumeroAvanzatoVec.ONE); + Number exponent = new Number(this.parent, NumeroAvanzatoVec.ONE); exponent = exponent.divide((Number) variable1); result.add(((Number)variable2).pow(exponent)); } else { @@ -69,7 +69,11 @@ public class Root extends FunctionTwoValues { Function[][] results = Utils.joinFunctionsResults(l1, l2); for (Function[] f : results) { - result.add(new Root((Function)f[0], (Function)f[1])); + 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; diff --git a/src/org/warp/picalculator/math/functions/RootSquare.java b/src/org/warp/picalculator/math/functions/RootSquare.java index 50e3b67d..21f51840 100644 --- a/src/org/warp/picalculator/math/functions/RootSquare.java +++ b/src/org/warp/picalculator/math/functions/RootSquare.java @@ -12,8 +12,8 @@ import org.warp.picalculator.math.MathematicalSymbols; public class RootSquare extends AnteriorFunction { - public RootSquare(Function value) { - super(value); + public RootSquare(Function parent, Function value) { + super(parent, value); } @Override @@ -34,7 +34,7 @@ public class RootSquare extends AnteriorFunction { @Override protected boolean isSolvable() throws Error { if (variable instanceof Number) { - if ((((Number)variable).pow(new Number(new Rational(1, 2))).getTerm().isBigInteger(true))) { + if ((((Number)variable).pow(new Number(this.parent, new Rational(1, 2))).getTerm().isBigInteger(true))) { return true; } } @@ -50,7 +50,7 @@ public class RootSquare extends AnteriorFunction { if (variable.isSolved()) { try { Number var = (Number) getVariable(); - result.add(var.pow(new Number(new Rational(1, 2)))); + result.add(var.pow(new Number(this.parent, new Rational(1, 2)))); } catch(NullPointerException ex) { throw new Error(Errors.ERROR); } catch(NumberFormatException ex) { @@ -67,7 +67,7 @@ public class RootSquare extends AnteriorFunction { } for (Function f : l1) { - result.add(new RootSquare((Function)f)); + result.add(new RootSquare(this.parent, (Function)f)); } } return result; diff --git a/src/org/warp/picalculator/math/functions/Subtraction.java b/src/org/warp/picalculator/math/functions/Subtraction.java index 806fc21e..a219f852 100644 --- a/src/org/warp/picalculator/math/functions/Subtraction.java +++ b/src/org/warp/picalculator/math/functions/Subtraction.java @@ -14,8 +14,8 @@ import org.warp.picalculator.math.Variables; public class Subtraction extends FunctionTwoValues { - public Subtraction(Function value1, Function value2) { - super(value1, value2); + public Subtraction(Function parent, Function value1, Function value2) { + super(parent, value1, value2); } @Override @@ -38,19 +38,7 @@ public class Subtraction extends FunctionTwoValues { } ArrayList result = new ArrayList<>(); if (variable1.isSolved() & variable2.isSolved()) { - if (((Number)variable1).term.isBigInteger(false) & ((Number)variable2).term.isBigInteger(false)) { - if (((Number)variable1).term.toBigInteger(false).compareTo(new BigInteger("2")) == 0 && ((Number)variable2).term.toBigInteger(false).compareTo(new BigInteger("2")) == 0) { - NumeroAvanzato na = NumeroAvanzato.ONE; - Variables iy = na.getVariableY(); - List newVariableList = iy.getVariablesList(); - newVariableList.add(new Variable('♓', 1, 1)); - iy = new Variables(newVariableList.toArray(new Variable[newVariableList.size()])); - na = na.setVariableY(iy); - result.add(new Number(na)); - return result; - } - } - result.add(((Number)variable1).add((Number)variable2).multiply(new Number("-1"))); + result.add(((Number)variable1).add(((Number)variable2).multiply(new Number(this.parent, "-1")))); } else { List l1 = new ArrayList(); List l2 = new ArrayList(); @@ -68,7 +56,7 @@ public class Subtraction extends FunctionTwoValues { Function[][] results = Utils.joinFunctionsResults(l1, l2); for (Function[] f : results) { - result.add(new Sum((Function)f[0], (Function)f[1])); + result.add(new Subtraction(this.parent, (Function)f[0], (Function)f[1])); } } return result; diff --git a/src/org/warp/picalculator/math/functions/Sum.java b/src/org/warp/picalculator/math/functions/Sum.java index 547c45e3..db858e1e 100644 --- a/src/org/warp/picalculator/math/functions/Sum.java +++ b/src/org/warp/picalculator/math/functions/Sum.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.functions; -import static org.warp.picalculator.device.graphicengine.Display.Render.getStringWidth; +import static org.warp.picalculator.device.graphicengine.Display.Render.glGetStringWidth; import static org.warp.picalculator.device.graphicengine.Display.Render.glDrawStringLeft; import java.math.BigInteger; @@ -19,8 +19,8 @@ import org.warp.picalculator.math.Variables; public class Sum extends FunctionTwoValues { - public Sum(Function value1, Function value2) { - super(value1, value2); + public Sum(Function parent, Function value1, Function value2) { + super(parent, value1, value2); } @Override @@ -43,15 +43,15 @@ public class Sum extends FunctionTwoValues { } ArrayList result = new ArrayList<>(); if (variable1.isSolved() & variable2.isSolved()) { - if (((Number)variable1).term.isBigInteger(false) & ((Number)variable2).term.isBigInteger(false)) { + if ((parent == null || parent.getParent() == null) && ((Number)variable1).term.isBigInteger(false) & ((Number)variable2).term.isBigInteger(false)) { if (((Number)variable1).term.toBigInteger(false).compareTo(new BigInteger("2")) == 0 && ((Number)variable2).term.toBigInteger(false).compareTo(new BigInteger("2")) == 0) { - NumeroAvanzato na = NumeroAvanzato.ONE; - Variables iy = na.getVariableY(); - List newVariableList = iy.getVariablesList(); - newVariableList.add(new Variable('♓', 1, 1)); - iy = new Variables(newVariableList.toArray(new Variable[newVariableList.size()])); - na = na.setVariableY(iy); - result.add(new Number(na)); + result.add(new Joke(Joke.FISH)); + return result; + } else if (((Number)variable1).term.toBigInteger(false).compareTo(new BigInteger("20")) == 0 && ((Number)variable2).term.toBigInteger(false).compareTo(new BigInteger("20")) == 0) { + result.add(new Joke(Joke.TORNADO)); + return result; + } else if (((Number)variable1).term.toBigInteger(false).compareTo(new BigInteger("29")) == 0 && ((Number)variable2).term.toBigInteger(false).compareTo(new BigInteger("29")) == 0) { + result.add(new Joke(Joke.SHARKNADO)); return result; } } @@ -73,7 +73,7 @@ public class Sum extends FunctionTwoValues { Function[][] results = Utils.joinFunctionsResults(l1, l2); for (Function[] f : results) { - result.add(new Sum((Function)f[0], (Function)f[1])); + result.add(new Sum(this.parent, (Function)f[0], (Function)f[1])); } } return result; @@ -102,10 +102,10 @@ public class Sum extends FunctionTwoValues { int dx = 0; variable1.draw(dx + x, ln - variable1.getLine() + y); dx += variable1.getWidth(); - Display.Render.setFont(Utils.getFont(small)); + Display.Render.glSetFont(Utils.getFont(small)); dx += 1; glDrawStringLeft(dx + x, ln - Utils.getFontHeight(small) / 2 + y, getSymbol()); - dx += getStringWidth(getSymbol()); + dx += glGetStringWidth(getSymbol()); variable2.draw(dx + x, ln - variable2.getLine() + y); } @@ -119,7 +119,7 @@ public class Sum extends FunctionTwoValues { int dx = 0; dx += variable1.getWidth(); dx += 1; - dx += getStringWidth(getSymbol()); + dx += glGetStringWidth(getSymbol()); return dx += variable2.getWidth(); } } diff --git a/src/org/warp/picalculator/math/functions/SumSubtraction.java b/src/org/warp/picalculator/math/functions/SumSubtraction.java index b824a548..64581a5e 100644 --- a/src/org/warp/picalculator/math/functions/SumSubtraction.java +++ b/src/org/warp/picalculator/math/functions/SumSubtraction.java @@ -1,6 +1,6 @@ package org.warp.picalculator.math.functions; -import static org.warp.picalculator.device.graphicengine.Display.Render.getStringWidth; +import static org.warp.picalculator.device.graphicengine.Display.Render.glGetStringWidth; import static org.warp.picalculator.device.graphicengine.Display.Render.glDrawStringLeft; import java.util.ArrayList; @@ -15,8 +15,8 @@ import org.warp.picalculator.math.MathematicalSymbols; public class SumSubtraction extends FunctionTwoValues { - public SumSubtraction(Function value1, Function value2) { - super(value1, value2); + public SumSubtraction(Function parent, Function value1, Function value2) { + super(parent, value1, value2); } @Override @@ -40,7 +40,7 @@ public class SumSubtraction extends FunctionTwoValues { ArrayList result = new ArrayList<>(); if (variable1.isSolved() & variable2.isSolved()) { result.add(((Number)variable1).add((Number)variable2)); - result.add(((Number)variable1).add(((Number)variable2).multiply(new Number("-1")))); + result.add(((Number)variable1).add(((Number)variable2).multiply(new Number(this.parent, "-1")))); } else { List l1 = new ArrayList(); List l2 = new ArrayList(); @@ -58,7 +58,7 @@ public class SumSubtraction extends FunctionTwoValues { Function[][] results = Utils.joinFunctionsResults(l1, l2); for (Function[] f : results) { - result.add(new SumSubtraction(f[0], f[1])); + result.add(new SumSubtraction(this.parent, f[0], f[1])); } } return result; @@ -87,10 +87,10 @@ public class SumSubtraction extends FunctionTwoValues { int dx = 0; variable1.draw(dx + x, ln - variable1.getLine() + y); dx += variable1.getWidth(); - Display.Render.setFont(Utils.getFont(small)); + Display.Render.glSetFont(Utils.getFont(small)); dx += 1; glDrawStringLeft(dx + x, ln - Utils.getFontHeight(small) / 2 + y, getSymbol()); - dx += getStringWidth(getSymbol()); + dx += glGetStringWidth(getSymbol()); variable2.draw(dx + x, ln - variable2.getLine() + y); } @@ -104,7 +104,7 @@ public class SumSubtraction extends FunctionTwoValues { int dx = 0; dx += variable1.getWidth(); dx += 1; - dx += getStringWidth(getSymbol()); + dx += glGetStringWidth(getSymbol()); return dx += variable2.getWidth(); } } diff --git a/src/org/warp/picalculator/math/functions/Undefined.java b/src/org/warp/picalculator/math/functions/Undefined.java new file mode 100644 index 00000000..5d434f9a --- /dev/null +++ b/src/org/warp/picalculator/math/functions/Undefined.java @@ -0,0 +1,78 @@ +package org.warp.picalculator.math.functions; + +import java.util.List; + +import org.warp.picalculator.Error; +import org.warp.picalculator.Utils; +import org.warp.picalculator.device.graphicengine.Display; + +public class Undefined implements Function { + + public Undefined(Function parent) { + setParent(parent); + } + + @Override + public String getSymbol() { + return "undefined"; + } + + @Override + public List solveOneStep() throws Error { + return null; + } + + @Override + public boolean isSolved() throws Error { + return true; + } + + private int width, height, line; + private Function parent; + private boolean small; + + @Override + public void generateGraphics() { + width = Display.Render.glGetStringWidth("UNDEFINED"); + height = Utils.getFontHeight(small); + line = height/2; + } + + @Override + public void draw(int x, int y) { + Display.Render.glSetFont(Utils.getFont(small)); + Display.Render.glDrawStringLeft(x, y, "UNDEFINED"); + } + + @Override + public int getWidth() { + return width; + } + + @Override + public int getHeight() { + return height; + } + + @Override + public int getLine() { + return line; + } + + @Override + public Function setParent(Function parent) { + this.parent = parent; + return this; + } + + @Override + public Function getParent() { + return parent; + } + + @Override + public void setSmall(boolean small) { + this.small = small; + } + +} diff --git a/src/org/warp/picalculator/math/functions/equations/Equation.java b/src/org/warp/picalculator/math/functions/equations/Equation.java index 6ffcd26a..fb83ad8c 100644 --- a/src/org/warp/picalculator/math/functions/equations/Equation.java +++ b/src/org/warp/picalculator/math/functions/equations/Equation.java @@ -19,8 +19,8 @@ import com.rits.cloning.Cloner; public class Equation extends FunctionTwoValues { - public Equation(Function value1, Function value2) { - super(value1,value2); + public Equation(Function parent, Function value1, Function value2) { + super(parent, value1,value2); } @Override @@ -46,7 +46,10 @@ public class Equation extends FunctionTwoValues { if (((Number)variable2).getTerm().isBigInteger(false) && ((Number)variable2).getTerm().toBigInteger(false).compareTo(new BigInteger("0")) == 0) { result.add(this); } else { - result.add(new Equation(new Subtraction(variable1, variable2), new Number("0"))); + Equation e = new Equation(this.parent, null, null); + e.setVariable1(new Subtraction(e, variable1, variable2)); + e.setVariable2(new Number(e, "0")); + result.add(e); } } else { List l1 = new ArrayList(); @@ -79,7 +82,7 @@ public class Equation extends FunctionTwoValues { if (cur2 >= size1) cur2 = 0; } for (Function[] f : results) { - result.add(new Equation(f[0], f[1])); + result.add(new Equation(this.parent, f[0], f[1])); } } return result; diff --git a/src/org/warp/picalculator/math/functions/equations/EquationResult.java b/src/org/warp/picalculator/math/functions/equations/EquationResult.java index bd84a507..0581f5d9 100644 --- a/src/org/warp/picalculator/math/functions/equations/EquationResult.java +++ b/src/org/warp/picalculator/math/functions/equations/EquationResult.java @@ -6,7 +6,7 @@ import org.warp.picalculator.math.functions.Number; public class EquationResult { public boolean isAnEquation = false; - public Number LR = new Number(new BigInteger("0")); + public Number LR = new Number(null, new BigInteger("0")); public EquationResult(Number LR, boolean isAnEquation) { this.LR = LR; diff --git a/src/org/warp/picalculator/math/functions/equations/EquationsSystem.java b/src/org/warp/picalculator/math/functions/equations/EquationsSystem.java index ff1d7655..e8d3ed74 100644 --- a/src/org/warp/picalculator/math/functions/equations/EquationsSystem.java +++ b/src/org/warp/picalculator/math/functions/equations/EquationsSystem.java @@ -14,16 +14,16 @@ import org.warp.picalculator.math.functions.Number; public class EquationsSystem extends FunctionMultipleValues { static final int spacing = 2; - public EquationsSystem() { - super(); + public EquationsSystem(Function parent) { + super(parent); } - public EquationsSystem(Function value) { - super(new Function[]{value}); + public EquationsSystem(Function parent, Function value) { + super(parent, new Function[]{value}); } - public EquationsSystem(Function[] value) { - super(value); + public EquationsSystem(Function parent, Function[] value) { + super(parent, value); } @Override @@ -52,7 +52,7 @@ public class EquationsSystem extends FunctionMultipleValues { if (f instanceof Number) { ret.add(f); } else { - ret.add(new Expression(new Function[]{(Function) f})); + ret.add(new Expression(this.parent, new Function[]{(Function) f})); } } return ret; @@ -62,7 +62,7 @@ public class EquationsSystem extends FunctionMultipleValues { if (f.isSolved() == false) { List partial = f.solveOneStep(); for (Function fnc : partial) { - ret.add(new Expression(new Function[]{(Function) fnc})); + ret.add(new Expression(this.parent, new Function[]{(Function) fnc})); } } } diff --git a/src/org/warp/picalculator/math/functions/equations/EquationsSystemPart.java b/src/org/warp/picalculator/math/functions/equations/EquationsSystemPart.java index c7eabdd7..5faadf5a 100644 --- a/src/org/warp/picalculator/math/functions/equations/EquationsSystemPart.java +++ b/src/org/warp/picalculator/math/functions/equations/EquationsSystemPart.java @@ -13,8 +13,8 @@ import org.warp.picalculator.math.functions.Function; public class EquationsSystemPart extends AnteriorFunction { - public EquationsSystemPart(Equation equazione) { - super(equazione); + public EquationsSystemPart(Function parent, Equation equazione) { + super(parent, equazione); } @Override diff --git a/src/org/warp/picalculator/math/rules/ExpandRule1.java b/src/org/warp/picalculator/math/rules/ExpandRule1.java new file mode 100644 index 00000000..3aa03c5f --- /dev/null +++ b/src/org/warp/picalculator/math/rules/ExpandRule1.java @@ -0,0 +1,127 @@ +package org.warp.picalculator.math.rules; + +import java.util.ArrayList; + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.functions.Expression; +import org.warp.picalculator.math.functions.Function; +import org.warp.picalculator.math.functions.FunctionTwoValues; +import org.warp.picalculator.math.functions.Negative; +import org.warp.picalculator.math.functions.Subtraction; +import org.warp.picalculator.math.functions.Sum; +import org.warp.picalculator.math.functions.SumSubtraction; + +/** + * Expand rule
+ * -(+a+b) = -a-b
+ * -(+a-b) = -a+b + * @author Andrea Cavalli + * + */ +public class ExpandRule1 { + + public static boolean compare(Function f) { + if (f instanceof Negative) { + Negative fnc = (Negative) f; + if (fnc.getVariable() instanceof Expression) { + Expression expr = (Expression) fnc.getVariable(); + if (expr.getVariablesLength() == 1) { + if (expr.getVariable(0) instanceof Sum) { + return true; + } else if (expr.getVariable(0) instanceof Subtraction) { + return true; + } else if (expr.getVariable(0) instanceof SumSubtraction) { + return true; + } + } + } + } else if (f instanceof Subtraction || f instanceof SumSubtraction) { + FunctionTwoValues fnc = (FunctionTwoValues) f; + if (fnc.getVariable2() instanceof Expression) { + Expression expr = (Expression) fnc.getVariable2(); + if (expr.getVariablesLength() == 1) { + if (expr.getVariable(0) instanceof Sum) { + return true; + } else if (expr.getVariable(0) instanceof Subtraction) { + return true; + } else if (expr.getVariable(0) instanceof SumSubtraction) { + return true; + } + } + } + } + return false; + } + + public static ArrayList execute(Function f) throws Error { + ArrayList result = new ArrayList<>(); + + Expression expr = null; + int fromSubtraction = 0; + FunctionTwoValues subtraction = null; + if (f instanceof Negative) { + expr = ((Expression)((Negative) f).getVariable()); + } else if (f instanceof Subtraction || f instanceof SumSubtraction) { + expr = ((Expression)((FunctionTwoValues) f).getVariable2()); + if (f instanceof Subtraction) { + fromSubtraction = 1; + } else { + fromSubtraction = 2; + } + } + + if (f instanceof SumSubtraction) { + + } + + Function fnc = expr.getVariable(0); + if (fnc instanceof Sum) { + Function a = ((Sum) fnc).getVariable1(); + Function b = ((Sum) fnc).getVariable2(); + Subtraction fnc2 = new Subtraction(((Negative) f).getParent(), null, b); + fnc2.setVariable1(new Negative(fnc2, a)); + if (fromSubtraction > 0) { + subtraction = new Subtraction(f.getParent(), null, null); + subtraction.setVariable1(((FunctionTwoValues)f).getVariable1()); + subtraction.setVariable2(fnc2); + result.add(subtraction); + } else { + result.add(fnc2); + } + } else if (fnc instanceof Subtraction) { + Function a = ((Subtraction) fnc).getVariable1(); + Function b = ((Subtraction) fnc).getVariable2(); + Sum fnc2 = new Sum(((Negative) f).getParent(), null, b); + fnc2.setVariable1(new Negative(fnc2, a)); + if (fromSubtraction > 0) { + subtraction = new Subtraction(f.getParent(), null, null); + subtraction.setVariable1(((FunctionTwoValues)f).getVariable1()); + subtraction.setVariable2(fnc2); + result.add(subtraction); + } else { + result.add(fnc2); + } + } else if (fnc instanceof SumSubtraction) { + Function a = ((Subtraction) fnc).getVariable1(); + Function b = ((Subtraction) fnc).getVariable2(); + Sum fnc2 = new Sum(((Negative) f).getParent(), null, b); + fnc2.setVariable1(new Negative(fnc2, a)); + Subtraction fnc3 = new Subtraction(((Negative) f).getParent(), null, b); + fnc3.setVariable1(new Negative(fnc2, a)); + if (fromSubtraction > 0) { + subtraction = new SumSubtraction(f.getParent(), null, null); + subtraction.setVariable1(((FunctionTwoValues)f).getVariable1()); + subtraction.setVariable2(fnc2); + result.add(subtraction); + subtraction = new SumSubtraction(f.getParent(), null, null); + subtraction.setVariable1(((FunctionTwoValues)f).getVariable1()); + subtraction.setVariable2(fnc3); + result.add(subtraction); + } else { + result.add(fnc2); + } + } + return result; + } + +} diff --git a/src/org/warp/picalculator/math/rules/ExpandRule5.java b/src/org/warp/picalculator/math/rules/ExpandRule5.java new file mode 100644 index 00000000..38ffd8b3 --- /dev/null +++ b/src/org/warp/picalculator/math/rules/ExpandRule5.java @@ -0,0 +1,47 @@ +package org.warp.picalculator.math.rules; + +import java.util.ArrayList; + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.functions.Function; +import org.warp.picalculator.math.functions.Negative; +import org.warp.picalculator.math.functions.Subtraction; + +/** + * Expand rule
+ * -(-a) = a + * @author Andrea Cavalli + * + */ +public class ExpandRule5 { + + public static boolean compare(Function f) { + if (f instanceof Negative) { + Negative fnc = (Negative) f; + if (fnc.getVariable() instanceof Negative) { + return true; + } + } else if (f instanceof Subtraction) { + Subtraction fnc = (Subtraction) f; + if (fnc.getVariable2() instanceof Negative) { + return true; + } + } + return false; + } + + public static ArrayList execute(Function f) throws Error { + ArrayList result = new ArrayList<>(); + Function a = null; + + if (f instanceof Negative) { + Negative fnc = (Negative) f; + result.add(((Negative)fnc.getVariable()).getVariable().setParent(f.getParent())); + } else if (f instanceof Subtraction) { + Subtraction fnc = (Subtraction) f; + result.add(((Negative)fnc.getVariable2()).getVariable().setParent(f.getParent())); + } + return result; + } + +} diff --git a/src/org/warp/picalculator/math/rules/FractionsRule1.java b/src/org/warp/picalculator/math/rules/FractionsRule1.java new file mode 100644 index 00000000..eed1b1f0 --- /dev/null +++ b/src/org/warp/picalculator/math/rules/FractionsRule1.java @@ -0,0 +1,50 @@ +package org.warp.picalculator.math.rules; + +import org.warp.picalculator.math.functions.Division; +import org.warp.picalculator.math.functions.Function; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Number; + +import java.util.ArrayList; +import java.util.List; + +import org.warp.picalculator.Error; + +/** + * Fractions rule
+ * 0 / a = 0 + * @author Andrea Cavalli + * + */ +public class FractionsRule1 { + + public static boolean compare(Function f) { + try { + if (f instanceof Division) { + Division fnc = (Division) f; + if (fnc.getVariable1() instanceof Number) { + Number numb1 = (Number) fnc.getVariable1(); + if (numb1.equals(new Number(null, "0"))) { + if (fnc.getVariable2() instanceof Number) { + Number numb2 = (Number) fnc.getVariable2(); + if (numb2.equals(new Number(null, "0"))) { + return false; + } + } + return true; + } + } + } + } catch (Error e) { + e.printStackTrace(); + } + return false; + } + + public static ArrayList execute(Function f) throws Error { + ArrayList result = new ArrayList<>(); + result.add(new Number(f.getParent(), "0")); + return result; + } + +} diff --git a/src/org/warp/picalculator/math/rules/FractionsRule2.java b/src/org/warp/picalculator/math/rules/FractionsRule2.java new file mode 100644 index 00000000..80a757d0 --- /dev/null +++ b/src/org/warp/picalculator/math/rules/FractionsRule2.java @@ -0,0 +1,45 @@ +package org.warp.picalculator.math.rules; + +import org.warp.picalculator.math.functions.Division; +import org.warp.picalculator.math.functions.Function; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Number; + +import java.util.ArrayList; +import java.util.List; + +import org.warp.picalculator.Error; + +/** + * Fractions rule
+ * a / 1 = a + * @author Andrea Cavalli + * + */ +public class FractionsRule2 { + + public static boolean compare(Function f) { + try { + if (f instanceof Division) { + Division fnc = (Division) f; + if (fnc.getVariable2() instanceof Number) { + Number numb = (Number) fnc.getVariable2(); + if (numb.equals(new Number(null, "1"))) { + return true; + } + } + } + } catch (Error e) { + e.printStackTrace(); + } + return false; + } + + public static ArrayList execute(Function f) throws Error { + ArrayList result = new ArrayList<>(); + Division fnc = (Division) f; + result.add(fnc.getVariable1().setParent(f.getParent())); + return result; + } + +} diff --git a/src/org/warp/picalculator/math/rules/FractionsRule3.java b/src/org/warp/picalculator/math/rules/FractionsRule3.java new file mode 100644 index 00000000..65b0ffa0 --- /dev/null +++ b/src/org/warp/picalculator/math/rules/FractionsRule3.java @@ -0,0 +1,38 @@ +package org.warp.picalculator.math.rules; + +import org.warp.picalculator.math.functions.Division; +import org.warp.picalculator.math.functions.Function; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Number; + +import java.util.ArrayList; +import java.util.List; + +import org.warp.picalculator.Error; + +/** + * Fractions rule
+ * a / 1 = a + * @author Andrea Cavalli + * + */ +public class FractionsRule3 { + + public static boolean compare(Function f) { + if (f instanceof Division) { + Division fnc = (Division) f; + if (fnc.getVariable1().equals(fnc.getVariable2())) { + return true; + } + } + return false; + } + + public static ArrayList execute(Function f) throws Error { + ArrayList result = new ArrayList<>(); + Division fnc = (Division) f; + result.add(fnc.getVariable1().setParent(f.getParent())); + return result; + } + +} diff --git a/src/org/warp/picalculator/math/rules/FractionsRule4.java b/src/org/warp/picalculator/math/rules/FractionsRule4.java new file mode 100644 index 00000000..8a285650 --- /dev/null +++ b/src/org/warp/picalculator/math/rules/FractionsRule4.java @@ -0,0 +1,45 @@ +package org.warp.picalculator.math.rules; + +import org.warp.picalculator.math.functions.Division; +import org.warp.picalculator.math.functions.Number; +import org.warp.picalculator.math.functions.Function; +import org.warp.picalculator.math.functions.Power; + +import java.util.ArrayList; + +import org.nevec.rjm.NumeroAvanzato; +import org.nevec.rjm.NumeroAvanzatoVec; +import org.warp.picalculator.Error; + +/** + * Fractions rule
+ * (a / b) ^ -1 = b / a + * @author Andrea Cavalli + * + */ +public class FractionsRule4 { + + public static boolean compare(Function f) throws Error { + if (f instanceof Power) { + Power fnc = (Power) f; + if (fnc.getVariable1() instanceof Division && fnc.getVariable2() instanceof Number) { + Number n2 = (Number) fnc.getVariable2(); + if (n2.equals(new Number(null, "-1"))) { + return true; + } + } + } + return false; + } + + public static ArrayList execute(Function f) throws Error { + ArrayList result = new ArrayList<>(); + Power fnc = (Power) f; + Function a = ((Division)fnc.getVariable1()).getVariable1(); + Function b = ((Division)fnc.getVariable1()).getVariable2(); + Division res = new Division(f.getParent(), b, a); + result.add(res); + return result; + } + +} diff --git a/src/org/warp/picalculator/math/rules/FractionsRule5.java b/src/org/warp/picalculator/math/rules/FractionsRule5.java new file mode 100644 index 00000000..662d1982 --- /dev/null +++ b/src/org/warp/picalculator/math/rules/FractionsRule5.java @@ -0,0 +1,49 @@ +package org.warp.picalculator.math.rules; + +import org.warp.picalculator.math.functions.Division; +import org.warp.picalculator.math.functions.Number; +import org.warp.picalculator.math.functions.Function; +import org.warp.picalculator.math.functions.Power; + +import java.math.BigInteger; +import java.util.ArrayList; + +import org.nevec.rjm.NumeroAvanzato; +import org.nevec.rjm.NumeroAvanzatoVec; +import org.warp.picalculator.Error; + +/** + * Fractions rule
+ * (a / b) ^ -c = (b / a) ^ c + * @author Andrea Cavalli + * + */ +public class FractionsRule5 { + + public static boolean compare(Function f) throws Error { + if (f instanceof Power) { + Power fnc = (Power) f; + if (fnc.getVariable1() instanceof Division && fnc.getVariable2() instanceof Number) { + Number n2 = (Number) fnc.getVariable2(); + if (n2.getTerm().compareTo(new NumeroAvanzatoVec(NumeroAvanzato.ZERO)) < 0) { + return true; + } + } + } + return false; + } + + public static ArrayList execute(Function f) throws Error { + ArrayList 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(null, "-1")); + Division dv = new Division(null, b, a); + Power pow = new Power(f.getParent(), dv, c); + dv.setParent(pow); + result.add(pow); + return result; + } + +} diff --git a/src/org/warp/picalculator/math/rules/NumberRule1.java b/src/org/warp/picalculator/math/rules/NumberRule1.java new file mode 100644 index 00000000..6e38250c --- /dev/null +++ b/src/org/warp/picalculator/math/rules/NumberRule1.java @@ -0,0 +1,49 @@ +package org.warp.picalculator.math.rules; + +import org.warp.picalculator.math.functions.Function; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Number; + +import java.util.ArrayList; +import java.util.List; + +import org.warp.picalculator.Error; + +/** + * Number rule
+ * a * 0 = 0 + * @author Andrea Cavalli + * + */ +public class NumberRule1 { + + public static boolean compare(Function f) { + try { + if (f instanceof Multiplication) { + Multiplication mult = (Multiplication) f; + if (mult.getVariable1() instanceof Number) { + Number numb = (Number) mult.getVariable1(); + if (numb.equals(new Number(null, "0"))) { + return true; + } + } + if (mult.getVariable2() instanceof Number) { + Number numb = (Number) mult.getVariable2(); + if (numb.equals(new Number(null, "0"))) { + return true; + } + } + } + } catch (Error e) { + e.printStackTrace(); + } + return false; + } + + public static ArrayList execute(Function f) throws Error { + ArrayList result = new ArrayList<>(); + result.add(new Number(f.getParent(), "0")); + return result; + } + +} diff --git a/src/org/warp/picalculator/math/rules/NumberRule2.java b/src/org/warp/picalculator/math/rules/NumberRule2.java new file mode 100644 index 00000000..639d7ab1 --- /dev/null +++ b/src/org/warp/picalculator/math/rules/NumberRule2.java @@ -0,0 +1,67 @@ +package org.warp.picalculator.math.rules; + +import org.warp.picalculator.math.functions.Function; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Number; + +import java.util.ArrayList; +import java.util.List; + +import org.warp.picalculator.Error; + +/** + * Number rule
+ * a * 1 = a + * @author Andrea Cavalli + * + */ +public class NumberRule2 { + + public static boolean compare(Function f) { + try { + if (f instanceof Multiplication) { + Multiplication mult = (Multiplication) f; + if (mult.getVariable1() instanceof Number) { + Number numb = (Number) mult.getVariable1(); + if (numb.equals(new Number(null, "1"))) { + return true; + } + } + if (mult.getVariable2() instanceof Number) { + Number numb = (Number) mult.getVariable2(); + if (numb.equals(new Number(null, "1"))) { + return true; + } + } + } + } catch (Error e) { + e.printStackTrace(); + } + return false; + } + + public static ArrayList execute(Function f) throws Error { + ArrayList result = new ArrayList<>(); + Function a = null; + boolean aFound = false; + Multiplication mult = (Multiplication) f; + if (aFound == false & mult.getVariable1() instanceof Number) { + Number numb = (Number) mult.getVariable1(); + if (numb.equals(new Number(null, "1"))) { + a = mult.getVariable2(); + aFound = true; + } + } + if (aFound == false && mult.getVariable2() instanceof Number) { + Number numb = (Number) mult.getVariable2(); + if (numb.equals(new Number(null, "1"))) { + a = mult.getVariable1(); + aFound = true; + } + } + + result.add(a.setParent(f.getParent())); + return result; + } + +} diff --git a/src/org/warp/picalculator/math/rules/UndefinedRule2.java b/src/org/warp/picalculator/math/rules/UndefinedRule2.java new file mode 100644 index 00000000..e363e008 --- /dev/null +++ b/src/org/warp/picalculator/math/rules/UndefinedRule2.java @@ -0,0 +1,43 @@ +package org.warp.picalculator.math.rules; + +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.Undefined; + +import java.util.ArrayList; + +import org.warp.picalculator.Error; + +/** + * Undefined rule
+ * a / 0 = undefined + * @author Andrea Cavalli + * + */ +public class UndefinedRule2 { + + public static boolean compare(Function f) { + try { + if (f instanceof Division) { + Division fnc = (Division) f; + if (fnc.getVariable2() instanceof Number) { + Number numb = (Number) fnc.getVariable2(); + if (numb.equals(new Number(null, "0"))) { + return true; + } + } + } + } catch (Error e) { + e.printStackTrace(); + } + return false; + } + + public static ArrayList execute(Function f) throws Error { + ArrayList result = new ArrayList<>(); + result.add(new Undefined(f.getParent())); + return result; + } + +} diff --git a/src/org/warp/picalculator/screens/EquationScreen.java b/src/org/warp/picalculator/screens/EquationScreen.java index 46b00aba..bed9c9fd 100644 --- a/src/org/warp/picalculator/screens/EquationScreen.java +++ b/src/org/warp/picalculator/screens/EquationScreen.java @@ -18,6 +18,9 @@ import org.warp.picalculator.device.graphicengine.Screen; import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Function; +import com.rits.cloning.Cloner; +import com.rits.cloning.FastClonerArrayList; + public class EquationScreen extends Screen { public float endLoading; @@ -28,6 +31,7 @@ public class EquationScreen extends Screen { public volatile float showCaretDelta = 0f; public List f; public List f2; + public int resultsCount; public int ew1; public int ew2; public int eh2; @@ -84,8 +88,11 @@ public class EquationScreen extends Screen { // Funzione f = new Parentesi("5Y+XY=2", "") // calcola("((5^2+3√(100/0.1))+Ⓐ(7)+9/15*2√(26/2))/21"); - f = new ArrayList(); - f2 = new ArrayList(); + if (f == null & f2 == null) { + f = new ArrayList(); + f2 = new ArrayList(); + resultsCount = 0; + } // interpreta("{(5X*(15X/3X))+(25X/(5X*(15X/3X)))=15{X=5"); //TODO RIMUOVERE // long start = System.nanoTime(); @@ -122,7 +129,7 @@ public class EquationScreen extends Screen { } public void solve() throws Error { - Calculator.solve(); + Calculator.solve(this); } @Override @@ -137,14 +144,14 @@ public class EquationScreen extends Screen { @Override public void render() { - Display.Render.setFont(Utils.getFont(false)); + Display.Render.glSetFont(Utils.getFont(false)); glClearColor(0xFFCCE7D4); glColor3f(0, 0, 0); glDrawStringLeft(2, 22, nuovaEquazione.substring(0, caretPos)+(showCaret?"|":"")+nuovaEquazione.substring(((showCaret==false||nuovaEquazione.length()<=caretPos)?caretPos:caretPos+1), nuovaEquazione.length())); if (f != null) { int topSpacing = 0; for (Function f : f) { - f.draw(2, 22+1+getFontHeight()+1+topSpacing); + f.draw(2, 22+1+glGetCurrentFontHeight()+1+topSpacing); topSpacing += f.getHeight() + 2; } } @@ -154,6 +161,13 @@ public class EquationScreen extends Screen { bottomSpacing += f.getHeight()+2; f.draw(Display.getWidth() - 2 - f.getWidth(), Display.getHeight() - bottomSpacing); } + if (resultsCount > 1 && resultsCount != f2.size()) { + String resultsCountText = resultsCount+" total results".toUpperCase(); + glColor(0xFF9AAEA0); + glSetFont(Utils.getFont(true)); + bottomSpacing += glGetCurrentFontHeight()+2; + glDrawStringRight(Display.getWidth() - 2, Display.getHeight() - bottomSpacing, resultsCountText); + } } } @@ -172,7 +186,7 @@ public class EquationScreen extends Screen { switch (k) { case SIMPLIFY: if (nuovaEquazione.length() > 0) { - Calculator.simplify(); + Calculator.simplify(this); } return true; case SOLVE: @@ -184,6 +198,7 @@ public class EquationScreen extends Screen { if (nuovaEquazione != equazioneCorrente && nuovaEquazione.length() > 0) { try { try { + changeEquationScreen(); interpreta(nuovaEquazione); solve(); } catch (Exception ex) { @@ -322,12 +337,24 @@ public class EquationScreen extends Screen { } } + private void changeEquationScreen() { + if (equazioneCorrente != null && equazioneCorrente.length() > 0) { + EquationScreen cloned = clone(); + cloned.caretPos = cloned.equazioneCorrente.length(); + cloned.nuovaEquazione = cloned.equazioneCorrente; + PIDisplay.INSTANCE.replaceScreen(cloned); + this.initialized = false; + PIDisplay.INSTANCE.setScreen(this); + + } + } + public void typeChar(String chr) { nuovaEquazione=nuovaEquazione.substring(0, caretPos)+chr+nuovaEquazione.substring(caretPos, nuovaEquazione.length()); caretPos+=1; showCaret = true; showCaretDelta = 0L; - f.clear(); +// f.clear(); //TODO: I removed this line to prevent input blanking when pressing EQUALS button and cloning this screen, but I must see why I created this part of code. } @Override @@ -335,5 +362,34 @@ public class EquationScreen extends Screen { return false; } + + @Override + public EquationScreen clone() { + EquationScreen es = this; + EquationScreen es2 = new EquationScreen(); + es2.endLoading = es.endLoading; + es2.nuovaEquazione = es.nuovaEquazione; + es2.equazioneCorrente = es.equazioneCorrente; + es2.showCaret = es.showCaret; + es2.showCaretDelta = es.showCaretDelta; + es2.caretPos = es.caretPos; + es2.f = Utils.cloner.deepClone(es.f); + es2.f2 = Utils.cloner.deepClone(es.f2); + es2.resultsCount = es.resultsCount; + es2.ew1 = es.ew1; + es2.ew2 = es.ew2; + es2.eh2 = es.eh2; + es2.x1 = es.x1; + es2.x2 = es.x2; + es2.requiresleep1 = es.requiresleep1; + es2.requiresleep2 = es.requiresleep2; + es2.autoscroll = es.autoscroll; + es2.errorLevel = es.errorLevel; + es2.err1 = es.err1; + es2.err2 = es.err2; + es2.mustRefresh = es.mustRefresh; + es2.aftersleep = es.aftersleep; + return es2; + } } diff --git a/src/org/warp/picalculator/screens/LoadingScreen.java b/src/org/warp/picalculator/screens/LoadingScreen.java index ab820891..f3fba3ad 100644 --- a/src/org/warp/picalculator/screens/LoadingScreen.java +++ b/src/org/warp/picalculator/screens/LoadingScreen.java @@ -69,7 +69,7 @@ public class LoadingScreen extends Screen { @Override public void render() { - setFont(fonts[2]); + glSetFont(fonts[2]); colore(1.0f, 1.0f, 1.0f, 1.0f); glDrawStringCenter((Main.screenSize[0] / 2) - 1,(int) ((Main.screenSize[1]/ 2) - 25 + loadingTextTranslation), "ANDREA CAVALLI'S CALCULATOR"); colore(1.0f, 1.0f, 1.0f, 1.0f); @@ -81,9 +81,9 @@ public class LoadingScreen extends Screen { colore(1.0f, 0.5f, 0.0f, 1.0f); glDrawStringCenter((Main.screenSize[0] / 2), (int) ((Main.screenSize[1]/ 2) - 25 + loadingTextTranslation), "ANDREA CAVALLI'S CALCULATOR"); colore(0.0f, 0.0f, 0.0f, 0.75f); - setFont(fonts[0]); + glSetFont(fonts[0]); glDrawStringCenter((Main.screenSize[0] / 2), (Main.screenSize[1]/ 2) + 11, "LOADING"); - setFont(fonts[1]); + glSetFont(fonts[1]); colore(0.0f, 0.0f, 0.0f, 0.5f); glDrawStringCenter((Main.screenSize[0] / 2), (Main.screenSize[1]/ 2) + 22, "PLEASE WAIT..."); diff --git a/src/org/warp/picalculator/screens/MarioScreen.java b/src/org/warp/picalculator/screens/MarioScreen.java index b8029a5a..cf102bd8 100644 --- a/src/org/warp/picalculator/screens/MarioScreen.java +++ b/src/org/warp/picalculator/screens/MarioScreen.java @@ -1,10 +1,6 @@ package org.warp.picalculator.screens; -import static org.warp.picalculator.device.graphicengine.Display.Render.getMatrixOfImage; -import static org.warp.picalculator.device.graphicengine.Display.Render.glClearColor; -import static org.warp.picalculator.device.graphicengine.Display.Render.glDrawSkin; -import static org.warp.picalculator.device.graphicengine.Display.Render.glDrawStringLeft; -import static org.warp.picalculator.device.graphicengine.Display.Render.setFont; +import static org.warp.picalculator.device.graphicengine.Display.Render.*; import java.awt.image.BufferedImage; import java.io.IOException; @@ -142,7 +138,7 @@ public class MarioScreen extends Screen { if (errored) { glDrawStringLeft(0, 20, "ERROR"); } else { - setFont(PIDisplay.fonts[0]); + glSetFont(PIDisplay.fonts[0]); glClearColor(0xff9290ff); glDrawSkin(groundSize[0], ground, 0, 25+25, 0, 0, 16, 16, false); glDrawSkin(groundSize[0], ground, 16, 25+25, 0, 0, 16, 16, false); @@ -163,6 +159,23 @@ public class MarioScreen extends Screen { glDrawSkin(groundSize[0], ground, 16*7, 25+25+16, 0, 0, 16, 16, false); glDrawSkin(groundSize[0], ground, 16*8, 25+25+16, 0, 0, 16, 16, false); +// EASTER EGG +// glSetFont(PIDisplay.fonts[4]); +// glColor(0xFF000000); +// glDrawStringRight(0, Main.screenSize[1]-glGetCurrentFontHeight(), "A"); +// glColor(0xFF800000); +// glDrawStringRight(0, Main.screenSize[1]-glGetCurrentFontHeight(), "B"); +// glColor(0xFFeea28e); +// glDrawStringRight(0, Main.screenSize[1]-glGetCurrentFontHeight(), "C"); +// glColor(0xFFee7255); +// glDrawStringRight(0, Main.screenSize[1]-glGetCurrentFontHeight(), "D"); +// glColor(0xFFeac0b0); +// glDrawStringRight(0, Main.screenSize[1]-glGetCurrentFontHeight(), "E"); +// glColor(0xFFf3d8ce); +// glDrawStringRight(0, Main.screenSize[1]-glGetCurrentFontHeight(), "F"); +// glColor(0xffede7); +// glDrawStringRight(0, Main.screenSize[1]-glGetCurrentFontHeight(), "G"); + //DRAW MARIO glDrawSkin(skinSize[0], skin, getPosX()-18, 25+getPosY(), 35*(marioSkinPos[0]+(flipped?2:1)), 27*marioSkinPos[1], 35*(marioSkinPos[0]+(flipped?1:2)), 27*(marioSkinPos[1]+1), true); }