From e3912397d77159692520fe771463dfd87988e008 Mon Sep 17 00:00:00 2001 From: XDrake99 Date: Mon, 5 Dec 2016 22:57:11 +0100 Subject: [PATCH 1/5] Added timeout and Multiplication Method 1 --- Algebra Cheat Sheet.rtf | Bin 5307 -> 5401 bytes README.md | 5 +- res/decimal.png | Bin 0 -> 7582 bytes res/font_ex.rft | Bin 1633 -> 1667 bytes src/org/warp/picalculator/Errors.java | 2 +- .../warp/picalculator/math/Calculator.java | 5 ++ .../math/functions/Multiplication.java | 4 + .../picalculator/math/functions/Number.java | 46 ++++++++++ .../math/functions/Subtraction.java | 4 + .../rules/methods/MultiplicationMethod1.java | 84 ++++++++++++++++++ 10 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 res/decimal.png create mode 100644 src/org/warp/picalculator/math/rules/methods/MultiplicationMethod1.java diff --git a/Algebra Cheat Sheet.rtf b/Algebra Cheat Sheet.rtf index 9897a8379355fa5385c2cfcd3d4d54356c7afdb0..51627ed15690bdbea9727a27bd58adbcbb657817 100644 GIT binary patch delta 66 zcmdn3Ia6x`m#{=qOqxMVTCs_Nf^TU~NoGM#W^!UlW`5q}8$xot5n9Gt5n4tHwhCri W5tA8tBsX&kuVrN8<*Mc7VgLZlz7oFx delta 18 ZcmbQKwOexom+U+zkN$#5#W; zzDzYS**&Hp)ZFTt;17`lk~)X~9#B640LZJEUp2K0A7fFY6Xbjmipy^1W8k#7ra0jN zg`d-}`YNSZm+OXOr*gg|{Ct&s+O%e5%p*h3<;c)|&6LOC2hSX;zWcI`WpYaF)x);K zUFQ0KS1RAI_eha9tMOKMMc0G0h*b{!21nE_0*==nL+fhd0%Wt-D-K!glMnhi#aLZT9nTRvN4MFr4m(2|d zE=WA8LG1t=&lR8C(+={-_9lC3b3!~ux~P(e4BwvdTi^-$vpZ00^)Wnz(=B?F+p$jn z>TlIoUK0hV@BFL(?(2Gvx{dxiu<_dE1;+8_>*7sRVAu18z7U3bC-D}2>Kx@^JQ{51O;O0JTpMGCHD*i?ZnuQ~ zF*DzaBZ4w2ZvQ2@V_A%&MXDj90*&=j_LP@5G1gNI!mknaV>c1nj0T^1}8Ta(qQL2yXHycNF9wr%}l5*Q(Gv< zs&_hT$-@S`bcq%cK`Hp#-JzEW6W_b)jYD)@o%}JRt^UmVIwMRp{3g1mk31*;;%=r< zl_rseOzcF2u)8qWtNumN5T@-UwES1=9etbNDQ%Ky}+HZRF;*R zoGXHdP;@pHsmS__XqACZ?`E8^iQS!E#$;djbhro7;(0J9pwHRnRFh5GhiOOSnU3Lc zOV_)J{VSNbTnuTTyRTt7`)iSwI?W}KJnH`^sGbrz^|uFB$J3yn$mvK;@Z#PLX%v|zV(TYcnP=7$5ERT z@h^?*rMRuTaQ5;<$L8i#`qWgqNt&Rz!ne3hL{~BMUgdy#cOpl@7otL7jaK@NHFfxj zlaLsU@I{AjqT#q7K~AWi3~yL41Yuzjver|>D@tx?NK;?UUmP*sm<^7YvTU}i(e4e( z$d~uJEei=nKf<+k-#dT0$K#Uj!qUeOx6|CtPOjt z3!OqN8OB;4qFIo1=lp>e+*(Jm%vcDFbE_|~MJnmRixb@)A`@Rtx94`?5XnVB#i+xI zh)wtHVVm8u?=f>~?78v59y7)0elSg%JN+bBI)No<-Wx+d3Zlk%wUsDkC|`5?t$6{}OIHGZEfrL#f))jH7z$MamK zBp7)%V+s9#W+_=|5Y=+Kc_fdW@f=4y_O$`RF;)9H_^t;pyoGhLWN6`SwmK`P-!}vD zhmm}i)Y44d?XIK{ioaZP^4yv}VRjuKUP%kXRGdTMm^17KtZq)#&d z18XH{Sc1~ZX325W%6)L-`^ChmR(2l$~ z+Z@vEv6Q>pJ-Cb7q{Qsp*zVuT*#yQ+b#2a}x4SkI6@kZMwluc4HfKxHC+GONrxMF) zmp~Y~EPGuiNngt4tfp!$N^T&?YRWcW{e%}@atU%Ab^yy`S?*k#H_80kdH@{7RlevfQECVTNt%%|d>-bNQUV3UNbPg$Piz$!U4;cY%+AVLF7 zjG0{S}rN`Z~>w2w!VC9>R-N!S89B{6b?%1+}?K?w)Dqu_a- zuvZj10V&tCP}U_96bH2cco77qMUin8*9`n`4a+odnM~ zNexIjWL;Z&Kf+mx0YXsp9DB6YD5RErkxohYx zIV2cJ*u$hb;PAp|ZXrl(3d-G04=4RLiy7TS)agy}^Orm+XB8 zx_+HZ?@V{8EDGw+-!PC5o^-r=3}eo)evIG66ktv5Pz64$s3<7ZN!PV?_EK7xg-=Z! z#t%Yr9+>N`4#!;?r9)goeMy-2bNYEZlz*aArYDbifk4g8PyZ8UG4Rsi;oum_qm&t! zl!VRd+sH~cn>luMc#6ejUiJ1|9m9-fQ;A14CC?uBp$wppMg?9EUhK|I z%6Bm=;lt5tX=JtGimiWvLv(+8?51@r3T0;IAtFTq)a*`*={tUdVoDB=aj(X&|`otTSYM?WjVarqMZ!3cF*xJ zvcx7dpSlNxO-xm3mf@T8uHsWJaM;3w3hH-B)le&bK-|Xrl_h)G$^!tA_>%$vQ<2{# z@8N&y-`_+N|)}ywt%+FHF*b*c2=K{tO%u zc}tW(LC7|VeEbvgm(lP4%`~G!UigejwD_7~I0b1>F65nI*&4vCN!ja5SY^GAEQ{;76DSnZfILn7YlF8K!V!my;vdwkSGr5AB^R`c-Gr{tE(+ zyIGHIEka8L;sRYE@TkBEJLQ+%s5}Rcy{Afgvn^2bhLYad7ZcO6@{9yvTsfN;@I(h; zqiqra4)GZ+lB&{>Lm-?S9SpT@GSTDHEhGI^X*w&aF0Ln+E-R~)b9(n}veE*#u7BUq zQhY-73!LzLXMV&NFBszVMC4X(9VeKO5GhoYvXkZjL-yaEs+O8xNqQ9lacjHx=8QRa*rxUX!Cr{o7<_3atuRH$O3A#0 z8115j_JcrkT6fl$!cfHCNPrAP;3Ek|uVm%rHLXTItb?6iIvGWE@&&A^5t~mHyOLNphCd8*rnoxZ<9vU0Dv`uosU!cF8NB z;+dJjV}A;lnU@Yh)Web1MT2sgmGt^t16V#(bg21JRpeho5pRac-i>8^THKks;9T}i z&}RX{>3q+M9}OjEo!-(#2JS#4e{HvasSjPb8GXZ{Xz8@}(pF#gyPCrTqLc5g2*F+i zXZp1~yzJnHmspb+CeLGY(7v|9bIM~f#TR{VXP1K1;j0%F0a%A_bFGx!*}8@)8>KIv zm#5%5ESF*5k=aJ;5>l>y-X#o;tTR$qlGo^Y`1{jy<=(Or8G5%=(8-s+Y=KxptUx|{ z%)Dh@#}Lp}`@^70nPrg9WB3VtbWZKtys+jhsmR&aivh@Y@5%u8HF^=4x?zyB-8K82 ziaYldkJ`y*y2xH|k!^DV!t#{OLq0lsyn{|HnWvuydu%bJtTF4q%u#~ai$ax^s>ZjD zDU(B*uYuigKP+Eh$3`G4blt0X*AqAmi;YtSbw2=a{jwepuYmgiRMp;$ehESu51p{V0O8HfJ$>P8)`+H6A3eGjE?sDgK|NCM4j+nKGU6H zL)&;%X%(ryDe^+PM9s_S&`7!;xW3Xp}Gbo%>6$LO< z>$M-vCOJhjyEEszEJrHr{29fB8-%*MR%d->n0i&;cHI;`>JTe_t zojx7)Uk{iy;^U#^paX!WtKMdY^z}<`>dF{C$oa7wtTCav>r1oS!yHm}E`O8tnzMvY z6Op8CS_Jwy>-!CwdD#kxFznDr_cn%%6$dWe{3Dadp;20s{`PJp5U?801~u|4V;nA| z>Zq8QIUnmb@AC4u%##|UtDLwkdReFAcp+*jn;1f_th;Nawe}- zcu{M}5o@*=XnwARl?W%KGVluMi*tk;nHUOa+kcbk@adjDIV`l(zAH-WRl--;h(EU_ z_?372If&~pG89Y;UG3{8eoduj06P~hym4^3z+EF|wtJ1X68lafZKo;&A%-<{>$TP|{LFe= z#IJL|O)?lK{=kQ{su`w?&xg-%f8gZ1s-_hbPK}v6WrB6Uk?N7rt|pZ09tB`f(U=Wa ztVFeHObm>zEpE*Mp^M@&O(Ei5j8s8(L)b}yQU>bk(nn8 zRUz65@@}~0G4n^@6Ly)9p60Lh^mXdoW3dN777Up8tuP0H+w&^34G$lY56-Yc~?H|-$w0tHQx;?(DfVNcw@JFNZb;N>}U(&P>KCGHx7 zEP%O_^X@5lN@&r&AV@we=u5_sSO<+5KP(nk^}>v8ARrms*=F{6-PYae0hkd2+o^!8vpe`tSM56S_J^jnABlQES2Smwbd$oqjoN51!_SuF z36fiE9srG0@cY_kPY*V8tT~)vm>}m9+R(Nk3$O(H_UmL5iF21`>SF!KR)gh;F_dGx zH$MAbol;XQzE?Herrpb3d#njpE z^v01JUW0S?p-VcO6=KqU(2H4knst>F==0%0WV>0g<|qVYc!RIJ6ul&|!3B{Eph4oQ z&uU>ds!pY!2#e3T)?B_ z;BNd2CmbV1=~?^U3%vg95GJnxO3^d9xvZbJL#wJZpgDckUT$$;}{f9f{ zfn-x~1vP9uY_>#n$yLCAtww=ww!bCN)NnqKGC-2;6l*OP=9r6R1WNTwZ*Za3C*I`# z%yFQbyP0qTZGfXS$lGg2YQU|w+Oxe{Vji@qia1S#ge6i zv-VW}kHFZ?HtRTdT*Q&sKpaX|P^!(fRH<hIoeG8vXs#AuU>hhvE!~Ne&O!HA~Q9ja7@ZLpd+W3yNDO4qd z!mZCR4_w~E?0@M;{`bWGi~rvY|KB8p9su^10RP0#MZkYX?DbN60`?lK-_rg~;(u@a ze>HIb{hNQ&6aTdi*%R=edE#FL{F|Qm7gzpw6aQ!9|K~UVnOm#`b@gD0lzt`Ro0=!h|F5lQh|jq?8kcgP&zs7xC;Y<9acc>3SWq_3(b!bv@_S7M zC}8?P6vKmhC3wh`%8Zz*=ykeI(rHr=CN|Gkl{58auaE<$5QF{p9}vAW1)y`%otNxB_&Ej>3$`o8!VARU-= zUvYIK8pzY_+Y&?e+jRoqMt?Pl-kA6K1|6;|cih|=E&4-s_rGFee z*o#o>s99-BZ3Uk=NbN13bGHHp;<0zp&XY#~-JPe^`R?~Yt@v;GmBG%*thp3i-=Yar zpKmPXo0YM{i&V%kTnb*WX^jV}n>6?|_j dt = itm.solveOneStep(); + long t2 = System.currentTimeMillis(); + if (t2-t1 >= 3000) { + throw new Error(Errors.TIMEOUT); + } partialResults.addAll(dt); } else { partialResults.add(itm); diff --git a/src/org/warp/picalculator/math/functions/Multiplication.java b/src/org/warp/picalculator/math/functions/Multiplication.java index 2f991afe..91b6067e 100644 --- a/src/org/warp/picalculator/math/functions/Multiplication.java +++ b/src/org/warp/picalculator/math/functions/Multiplication.java @@ -9,6 +9,7 @@ import org.warp.picalculator.math.rules.NumberRule1; import org.warp.picalculator.math.rules.NumberRule2; import org.warp.picalculator.math.rules.NumberRule6; import org.warp.picalculator.math.rules.SyntaxRule1; +import org.warp.picalculator.math.rules.methods.MultiplicationMethod1; public class Multiplication extends FunctionTwoValues { @@ -40,6 +41,7 @@ public class Multiplication extends FunctionTwoValues { if (NumberRule2.compare(this)) return true; if (NumberRule6.compare(this)) return true; if (ExponentRule15.compare(this)) return true; + if (MultiplicationMethod1.compare(this)) return true; return false; } @@ -56,6 +58,8 @@ public class Multiplication extends FunctionTwoValues { result = NumberRule6.execute(this); } else if (ExponentRule15.compare(this)) { result = ExponentRule15.execute(this); + } else if (MultiplicationMethod1.compare(this)) { + result = MultiplicationMethod1.execute(this); } else if (variable1.isSolved() & variable2.isSolved()) { result.add(((Number)variable1).multiply((Number)variable2)); } diff --git a/src/org/warp/picalculator/math/functions/Number.java b/src/org/warp/picalculator/math/functions/Number.java index 999c55f2..d4a1f565 100644 --- a/src/org/warp/picalculator/math/functions/Number.java +++ b/src/org/warp/picalculator/math/functions/Number.java @@ -6,6 +6,7 @@ import static org.warp.picalculator.device.graphicengine.Display.Render.glGetStr import java.math.BigDecimal; import java.math.BigInteger; import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; import org.nevec.rjm.BigDecimalMath; @@ -289,4 +290,49 @@ public class Number implements Function { * return 6*toString().length()-1; * } */ + + public boolean canBeFactorized() { + if (Utils.isIntegerValue(getTerm())) { + return getTerm().toBigIntegerExact().compareTo(BigInteger.valueOf(1)) > 1; + } + return false; + } + + public LinkedList getFactors() + { + BigInteger n = getTerm().toBigIntegerExact(); + BigInteger two = BigInteger.valueOf(2); + LinkedList fs = new LinkedList(); + + if (n.compareTo(two) < 0) + { + throw new IllegalArgumentException("must be greater than one"); + } + + while (n.mod(two).equals(BigInteger.ZERO)) + { + fs.add(two); + n = n.divide(two); + } + + if (n.compareTo(BigInteger.ONE) > 0) + { + BigInteger f = BigInteger.valueOf(3); + while (f.multiply(f).compareTo(n) <= 0) + { + if (n.mod(f).equals(BigInteger.ZERO)) + { + fs.add(f); + n = n.divide(f); + } + else + { + f = f.add(two); + } + } + fs.add(n); + } + + return fs; + } } diff --git a/src/org/warp/picalculator/math/functions/Subtraction.java b/src/org/warp/picalculator/math/functions/Subtraction.java index 63857b95..5525a2d0 100644 --- a/src/org/warp/picalculator/math/functions/Subtraction.java +++ b/src/org/warp/picalculator/math/functions/Subtraction.java @@ -11,6 +11,7 @@ import org.warp.picalculator.math.rules.NumberRule5; import org.warp.picalculator.math.rules.VariableRule1; import org.warp.picalculator.math.rules.VariableRule2; import org.warp.picalculator.math.rules.VariableRule3; +import org.warp.picalculator.math.rules.methods.SumMethod1; public class Subtraction extends FunctionTwoValues { @@ -40,6 +41,7 @@ public class Subtraction extends FunctionTwoValues { if (ExpandRule1.compare(this)) return true; if (ExpandRule5.compare(this)) return true; if (NumberRule5.compare(this)) return true; + if (SumMethod1.compare(this)) return true; return false; } @@ -60,6 +62,8 @@ public class Subtraction extends FunctionTwoValues { result = ExpandRule5.execute(this); } else if (NumberRule5.compare(this)) { result = NumberRule5.execute(this); + } else if (SumMethod1.compare(this)) { + result = SumMethod1.execute(this); } else if (variable1.isSolved() & variable2.isSolved()) { result.add(((Number)variable1).add(((Number)variable2).multiply(new Number(this.parent, "-1")))); } diff --git a/src/org/warp/picalculator/math/rules/methods/MultiplicationMethod1.java b/src/org/warp/picalculator/math/rules/methods/MultiplicationMethod1.java new file mode 100644 index 00000000..842913a6 --- /dev/null +++ b/src/org/warp/picalculator/math/rules/methods/MultiplicationMethod1.java @@ -0,0 +1,84 @@ +package org.warp.picalculator.math.rules.methods; + +import java.util.ArrayList; + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.functions.Function; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Number; + +/** + * Multiplication method
+ * Example: X*3*X*2 = 6*X^2 + * @author Andrea Cavalli + * + */ +public class MultiplicationMethod1 { + + public static boolean compare(Function f) { + return ((Multiplication)f).getVariable1().isSolved() && ((Multiplication)f).getVariable2().isSolved() && !(((Multiplication)f).getVariable1() instanceof Number && ((Multiplication)f).getVariable2() instanceof Number) && getFirstWorkingMultiplicationCouple(getMultiplicationElements(f)) != null; + } + + public static ArrayList execute(Function f) throws Error { + Function result; + ArrayList elements = getMultiplicationElements(f); + int[] workingElementCouple = getFirstWorkingMultiplicationCouple(elements); + Function elem1 = elements.get(workingElementCouple[0]); + Function elem2 = elements.get(workingElementCouple[1]); + + final int size = elements.size(); + Function prec = new Multiplication(null, elem1, elem2); + elem1.setParent(prec); + elem2.setParent(prec); + for (int i = size-1; i >= 0; i--) { + if (i != workingElementCouple[0] & i != workingElementCouple[1]) { + Function a = prec; + Function b = elements.get(i); + prec = new Multiplication(null, a, b); + a.setParent(prec); + b.setParent(prec); + } + } + prec.setParent(f.getParent()); + + result = prec; + + ArrayList results = new ArrayList<>(); + results.add(result); + return results; + } + + private static ArrayList getMultiplicationElements(Function mult) { + ArrayList elements = new ArrayList<>(); + while (mult instanceof Multiplication) { + elements.add(((Multiplication) mult).getVariable1()); + mult = ((Multiplication) mult).getVariable2(); + } + elements.add(mult); + return elements; + } + + private static int[] getFirstWorkingMultiplicationCouple(ArrayList elements) { + final int size = elements.size(); + Function a; + Function b; + if (elements.size() == 2) { + return null; + } + for (int i = 0; i < size; i++) { + a = elements.get(i); + for (int j = 0; j < size; j++) { + b = elements.get(j); + if (i != j) { + Function testFunc; + testFunc = new Multiplication(null, a, b); + if (!testFunc.isSolved()) { + return new int[]{i, j}; + } + } + } + } + return null; + } + +} From e8f74c63e639b3d680b1cdfc774d8d503934a5f0 Mon Sep 17 00:00:00 2001 From: XDrake99 Date: Mon, 5 Dec 2016 22:58:21 +0100 Subject: [PATCH 2/5] Edited README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d1e25dbe..87e211aa 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ # PICalculator Scientific calculator in java. + ![alt text](https://github.com/XDrake99/PICalculator/blob/master/res/decimal.png "Example expression") \ No newline at end of file From d799c2cf7d948eb19f555c43465e6d2be5a15a92 Mon Sep 17 00:00:00 2001 From: XDrake99 Date: Mon, 16 Jan 2017 17:57:09 +0100 Subject: [PATCH 3/5] Start changing tree structure --- .classpath | 20 +- Algebra Cheat Sheet.rtf | Bin 5401 -> 5473 bytes libs/lwjgl/LICENSE | 29 + libs/lwjgl/build.txt | 1 + libs/lwjgl/dyncall_license.txt | 14 + libs/lwjgl/glfw_license.txt | 21 + libs/lwjgl/jemalloc_license.txt | 23 + libs/lwjgl/khronos_license.txt | 22 + libs/objenesis-2.4/LICENSE | 202 +++++++ libs/objenesis-2.4/NOTICE | 9 + libs/pi4j-1.1/LICENSE.txt | 165 ++++++ libs/pi4j-1.1/NOTICE.txt | 33 ++ res/font_ex.rft | Bin 1667 -> 1667 bytes src/org/warp/picalculator/Main.java | 6 +- src/org/warp/picalculator/Utils.java | 18 + .../warp/picalculator/device/Keyboard.java | 392 ++++++++++++-- .../warp/picalculator/device/PIDisplay.java | 66 +-- .../device/graphicengine/Display.java | 4 +- .../device/graphicengine/Frame.java | 494 ------------------ .../device/graphicengine/SwingWindow.java | 241 +++++++++ src/org/warp/picalculator/math/AngleMode.java | 7 + .../warp/picalculator/math/Calculator.java | 84 +-- .../math/MathematicalSymbols.java | 3 +- .../picalculator/math/functions/Number.java | 2 +- .../picalculator/math/functions/Variable.java | 10 + .../math/functions/trigonometry/Sine.java | 32 +- .../math/rules/methods/DivisionRule1.java | 96 ++++ .../screens/ChooseVariableValueScreen.java | 85 +++ .../screens/KeyboardDebugScreen.java | 229 ++++++++ .../picalculator/screens/MathInputScreen.java | 319 +++++++++-- 30 files changed, 1906 insertions(+), 721 deletions(-) create mode 100644 libs/lwjgl/LICENSE create mode 100644 libs/lwjgl/build.txt create mode 100644 libs/lwjgl/dyncall_license.txt create mode 100644 libs/lwjgl/glfw_license.txt create mode 100644 libs/lwjgl/jemalloc_license.txt create mode 100644 libs/lwjgl/khronos_license.txt create mode 100644 libs/objenesis-2.4/LICENSE create mode 100644 libs/objenesis-2.4/NOTICE create mode 100644 libs/pi4j-1.1/LICENSE.txt create mode 100644 libs/pi4j-1.1/NOTICE.txt delete mode 100644 src/org/warp/picalculator/device/graphicengine/Frame.java create mode 100644 src/org/warp/picalculator/device/graphicengine/SwingWindow.java create mode 100644 src/org/warp/picalculator/math/AngleMode.java create mode 100644 src/org/warp/picalculator/math/rules/methods/DivisionRule1.java create mode 100644 src/org/warp/picalculator/screens/ChooseVariableValueScreen.java create mode 100644 src/org/warp/picalculator/screens/KeyboardDebugScreen.java diff --git a/.classpath b/.classpath index 22aa364e..37991c32 100644 --- a/.classpath +++ b/.classpath @@ -3,29 +3,29 @@ - + - + - + - + - + - + - + - + - + - + diff --git a/Algebra Cheat Sheet.rtf b/Algebra Cheat Sheet.rtf index 51627ed15690bdbea9727a27bd58adbcbb657817..030baa544aa7dce4bdc7ae45694fa80d68b9f3fd 100644 GIT binary patch delta 78 zcmbQK^-yaA3!`XtOqzL2T3%*OOj>eAVo`BwiJ_U{#$T)=8U=|(yj&pJoW#6zLjz-@ a81*!Rm^4!;-^3uMBr!=Lz;p6H5orJ?C>l5b delta 39 ucmaE;HB)N?3*%;G#^1tHNik^#F=@pn1~EB_dFh5`F$IZ5yj-=sTnqpWh72D7 diff --git a/libs/lwjgl/LICENSE b/libs/lwjgl/LICENSE new file mode 100644 index 00000000..a983c18a --- /dev/null +++ b/libs/lwjgl/LICENSE @@ -0,0 +1,29 @@ +Copyright (c) 2012-present Lightweight Java Game Library +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +- Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +- Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +- Neither the name Lightweight Java Game Library nor the names of + its contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/libs/lwjgl/build.txt b/libs/lwjgl/build.txt new file mode 100644 index 00000000..e454bfa6 --- /dev/null +++ b/libs/lwjgl/build.txt @@ -0,0 +1 @@ +LWJGL 3.1.1 build 16 \ No newline at end of file diff --git a/libs/lwjgl/dyncall_license.txt b/libs/lwjgl/dyncall_license.txt new file mode 100644 index 00000000..f119bb7c --- /dev/null +++ b/libs/lwjgl/dyncall_license.txt @@ -0,0 +1,14 @@ +Copyright (c) 2007-2015 Daniel Adler , + Tassilo Philipp + +Permission to use, copy, modify, and distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. \ No newline at end of file diff --git a/libs/lwjgl/glfw_license.txt b/libs/lwjgl/glfw_license.txt new file mode 100644 index 00000000..e7c49d62 --- /dev/null +++ b/libs/lwjgl/glfw_license.txt @@ -0,0 +1,21 @@ +Copyright (c) 2002-2006 Marcus Geelnard +Copyright (c) 2006-2010 Camilla Berglund + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would + be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and must not + be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source + distribution. diff --git a/libs/lwjgl/jemalloc_license.txt b/libs/lwjgl/jemalloc_license.txt new file mode 100644 index 00000000..c95c2baa --- /dev/null +++ b/libs/lwjgl/jemalloc_license.txt @@ -0,0 +1,23 @@ +Copyright (C) 2002-2014 Jason Evans . +All rights reserved. +Copyright (C) 2007-2012 Mozilla Foundation. All rights reserved. +Copyright (C) 2009-2014 Facebook, Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: +1. Redistributions of source code must retain the above copyright notice(s), + this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice(s), + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS +OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/libs/lwjgl/khronos_license.txt b/libs/lwjgl/khronos_license.txt new file mode 100644 index 00000000..96c8f0cd --- /dev/null +++ b/libs/lwjgl/khronos_license.txt @@ -0,0 +1,22 @@ +/* +** Copyright (c) 2013-2014 The Khronos Group Inc. +** +** Permission is hereby granted, free of charge, to any person obtaining a +** copy of this software and/or associated documentation files (the +** "Materials"), to deal in the Materials without restriction, including +** without limitation the rights to use, copy, modify, merge, publish, +** distribute, sublicense, and/or sell copies of the Materials, and to +** permit persons to whom the Materials are furnished to do so, subject to +** the following conditions: +** +** The above copyright notice and this permission notice shall be included +** in all copies or substantial portions of the Materials. +** +** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. +*/ \ No newline at end of file diff --git a/libs/objenesis-2.4/LICENSE b/libs/objenesis-2.4/LICENSE new file mode 100644 index 00000000..d6456956 --- /dev/null +++ b/libs/objenesis-2.4/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/libs/objenesis-2.4/NOTICE b/libs/objenesis-2.4/NOTICE new file mode 100644 index 00000000..c4857a0b --- /dev/null +++ b/libs/objenesis-2.4/NOTICE @@ -0,0 +1,9 @@ +// ------------------------------------------------------------------ +// NOTICE file corresponding to the section 4d of The Apache License, +// Version 2.0, in this case for Objenesis +// ------------------------------------------------------------------ + +Objenesis +Copyright 2006-2016 Joe Walnes, Henri Tremblay, Leonardo Mesquita + + diff --git a/libs/pi4j-1.1/LICENSE.txt b/libs/pi4j-1.1/LICENSE.txt new file mode 100644 index 00000000..cca7fc27 --- /dev/null +++ b/libs/pi4j-1.1/LICENSE.txt @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/libs/pi4j-1.1/NOTICE.txt b/libs/pi4j-1.1/NOTICE.txt new file mode 100644 index 00000000..1da9e7b0 --- /dev/null +++ b/libs/pi4j-1.1/NOTICE.txt @@ -0,0 +1,33 @@ + ========================================================================= + Pi4J :: Java library for Raspberry Pi + ========================================================================= + + Pi4J Library + Copyright (C) 2012-2016 Pi4J.com + + This product includes software developed at: + The Pi4J Project (http://www.pi4j.com/). + + This product was originally authored by: Robert Savage + + This software is licensed under the GNU LGPLv3 license. + More information about licensing terms can be found at: + http://pi4j.com/license.html + + Additional information about this project and its authors can be found at: + - http://www.pi4j.com/ + - http://www.raspicentral.com + - http://www.savagehomeautomation.com + + This software compiles against libraries from the WiringPi project. + The Wiring Pi project is provided by Gordon Henderson and is available + from + http://wiringpi.com/ + + The WiringPi project is licensed under the GNU LGPLv3 license + http://www.gnu.org/licenses/lgpl.html + + ========================================================================= + Subject to the terms of the software license, this NOTICE file must be + included in any Derivative Works that You distribute as a readable copy. + (Section 4d) diff --git a/res/font_ex.rft b/res/font_ex.rft index 70e553c23a51a9c59e41561c717972db7c70e6fe..1f79a6e9117c1f5c530d5dfa5120f10a0dfd0c58 100644 GIT binary patch delta 132 zcmV-~0DJ#~4TBAkdJaAqL;(Om5C!oBK^Vi}k%%QAd^6x62m%0rAP5E#55T|xOTjRV zV=&k}4+C(=Fl-Nik27urY2nGWIfB;$m9pTsn0|9`*U@#s4@CUQZ0k;GurYkT2 delta 132 zcmZqXZRVX&#m9gGCU&SQpa=jt-e>|7&+c_X6Jm(?ctcs>h!k_ String arrayToString(T... data) { + String sdata = ""; + for (T o : data) { + sdata += ","+o.toString(); + } + return sdata.substring(1); + } + + public static String arrayToString(boolean... data) { + String sdata = ""; + for (boolean o : data) { + sdata += (o)?1:0; + } + return sdata; + } } diff --git a/src/org/warp/picalculator/device/Keyboard.java b/src/org/warp/picalculator/device/Keyboard.java index 3169cd44..1d95c91c 100644 --- a/src/org/warp/picalculator/device/Keyboard.java +++ b/src/org/warp/picalculator/device/Keyboard.java @@ -1,11 +1,17 @@ package org.warp.picalculator.device; +import java.awt.event.KeyEvent; +import java.util.Arrays; + import org.warp.picalculator.Utils; +import org.warp.picalculator.device.Keyboard.Key; import org.warp.picalculator.device.chip.ParallelToSerial; import org.warp.picalculator.device.chip.SerialToParallel; import org.warp.picalculator.device.graphicengine.Display; import org.warp.picalculator.device.graphicengine.Screen; +import org.warp.picalculator.math.AngleMode; import org.warp.picalculator.math.Calculator; +import org.warp.picalculator.screens.KeyboardDebugScreen; import org.warp.picalculator.screens.MarioScreen; import com.pi4j.wiringpi.Gpio; @@ -26,25 +32,40 @@ public class Keyboard { private static volatile boolean[][] precedentStates = new boolean[8][8]; public static volatile boolean[][] debugKeysDown = new boolean[8][8]; + public static volatile KeyEvent debugKeyEvent; public static void startKeyboard() { - if (Utils.debugOn == false) { - Gpio.pinMode(CLK_INH_pin, Gpio.OUTPUT); - Gpio.pinMode(RCK_pin, Gpio.OUTPUT); - Gpio.pinMode(SER_pin, Gpio.OUTPUT); - Gpio.pinMode(SH_LD_pin, Gpio.OUTPUT); - Gpio.pinMode(SCK_and_CLK_pin, Gpio.OUTPUT); - Gpio.pinMode(QH_pin, Gpio.INPUT); - - Gpio.digitalWrite(CLK_INH_pin, false); - Gpio.digitalWrite(RCK_pin, false); - Gpio.digitalWrite(SER_pin, false); - Gpio.digitalWrite(SH_LD_pin, false); - Gpio.digitalWrite(SCK_and_CLK_pin, false); - Gpio.digitalWrite(QH_pin, false); - Thread kt = new Thread(()->{ + Thread kt = new Thread(()->{ + if (Utils.debugOn) { + try { + while(true) { + if (debugKeyEvent != null) { + debugKeyPressed(debugKeyEvent); + debugKeyEvent = null; + } + Thread.sleep(50); + } + } catch (InterruptedException e) { + } + } else { + Gpio.pinMode(CLK_INH_pin, Gpio.OUTPUT); + Gpio.pinMode(RCK_pin, Gpio.OUTPUT); + Gpio.pinMode(SER_pin, Gpio.OUTPUT); + Gpio.pinMode(SH_LD_pin, Gpio.OUTPUT); + Gpio.pinMode(SCK_and_CLK_pin, Gpio.OUTPUT); + Gpio.pinMode(QH_pin, Gpio.INPUT); + + Gpio.digitalWrite(CLK_INH_pin, false); + Gpio.digitalWrite(RCK_pin, false); + Gpio.digitalWrite(SER_pin, false); + Gpio.digitalWrite(SH_LD_pin, false); + Gpio.digitalWrite(SCK_and_CLK_pin, false); + Gpio.digitalWrite(QH_pin, false); SerialToParallel chip1 = new SerialToParallel(RCK_pin, SCK_and_CLK_pin /*SCK*/, SER_pin); ParallelToSerial chip2 = new ParallelToSerial(SH_LD_pin, CLK_INH_pin, QH_pin, SCK_and_CLK_pin/*CLK*/); + + KeyboardDebugScreen.log("Started keyboard system"); + while(true) { boolean[] data; for (int col = 0; col < 8; col++) { @@ -52,25 +73,306 @@ public class Keyboard { data[col] = true; chip1.write(data); - data = new boolean[8]; data = chip2.read(); + KeyboardDebugScreen.ks[col] = data; for (int row = 0; row < 8; row++) { if (data[row] == true && precedentStates[row][col] == false) { System.out.println("Pressed button at "+(row+1) +", "+(col+1)); + KeyboardDebugScreen.log("Pressed button at "+(row+1) +", "+(col+1)); keyPressedRaw(row+1, col+1); } else if (data[row] == false && precedentStates[row][col] == true) { keyReleasedRaw(row+1, col+1); + KeyboardDebugScreen.log("Released button at "+(row+1) +", "+(col+1)); } precedentStates[row][col] = data[row]; } } } - }); - kt.setName("Keyboard thread"); - kt.setPriority(Thread.MIN_PRIORITY); - kt.setDaemon(true); - kt.start(); + } + }); + kt.setName("Keyboard thread"); + kt.setPriority(Thread.MIN_PRIORITY); + kt.setDaemon(true); + kt.start(); + } + + private static void debugKeyPressed(KeyEvent arg0) { + switch (arg0.getKeyCode()) { + case KeyEvent.VK_ESCAPE: + Keyboard.keyPressed(Key.POWER); + break; + case KeyEvent.VK_S: + if (Keyboard.shift) { + Keyboard.keyPressed(Key.ARCSINE); + } else if (Keyboard.alpha) { + Keyboard.keyPressed(Key.NONE); + } else { + Keyboard.keyPressed(Key.SINE); + } + break; + case KeyEvent.VK_C: + if (Keyboard.shift) { + Keyboard.keyPressed(Key.ARCCOSINE); + } else if (Keyboard.alpha) { + Keyboard.keyPressed(Key.NONE); + } else { + Keyboard.keyPressed(Key.COSINE); + } + break; + case KeyEvent.VK_T: + if (Keyboard.shift) { + Keyboard.keyPressed(Key.ARCTANGENT); + } else if (Keyboard.alpha) { + Keyboard.keyPressed(Key.NONE); + } else { + Keyboard.keyPressed(Key.TANGENT); + } + break; + case KeyEvent.VK_D: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.debug_DEG); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_R: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.debug_RAD); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_G: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.debug_GRA); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_X: + if (Keyboard.alpha) { + Keyboard.keyPressed(Key.LETTER_X); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_P: + if (Keyboard.alpha) { + Keyboard.keyPressed(Key.PI); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_Y: + if (Keyboard.alpha) { + Keyboard.keyPressed(Key.LETTER_Y); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_B: + if (Keyboard.shift) { + Keyboard.keyPressed(Key.BRIGHTNESS_CYCLE_REVERSE); + } else if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.BRIGHTNESS_CYCLE); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_ENTER: + if (Keyboard.shift) { + Keyboard.keyPressed(Key.SIMPLIFY); + } else if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.SOLVE); + } else { + Keyboard.keyPressed(Key.NONE); + } + int row = 2; + int col = 1; + Keyboard.debugKeysDown[row-1][col-1] = true; + break; + case KeyEvent.VK_1: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.NUM1); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_2: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.NUM2); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_3: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.NUM3); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_4: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.NUM4); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_5: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.NUM5); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_6: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.NUM6); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_7: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.NUM7); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_8: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.NUM8); + } else if (Keyboard.shift) { + Keyboard.keyPressed(Key.PARENTHESIS_OPEN); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_9: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.NUM9); + } else if (Keyboard.shift) { + Keyboard.keyPressed(Key.PARENTHESIS_CLOSE); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_0: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.NUM0); + } else if (Keyboard.shift) { + Keyboard.keyPressed(Key.EQUAL); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_ADD: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.PLUS); + } else if (Keyboard.shift) { + Keyboard.keyPressed(Key.PLUS_MINUS); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_SUBTRACT: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.MINUS); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_MULTIPLY: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.MULTIPLY); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_DIVIDE: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.DIVIDE); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_BACK_SPACE: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.DELETE); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_DELETE: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.RESET); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_LEFT: + //LEFT + row = 2; + col = 3; + Keyboard.debugKeysDown[row-1][col-1] = true; + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.LEFT); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_RIGHT: + //RIGHT + row = 2; + col = 5; + Keyboard.debugKeysDown[row-1][col-1] = true; + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.RIGHT); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_NUMPAD4: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.HISTORY_BACK); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_NUMPAD6: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.HISTORY_FORWARD); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_PERIOD: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyPressed(Key.DOT); + } else { + Keyboard.keyPressed(Key.NONE); + } + break; + case KeyEvent.VK_SHIFT: + Keyboard.keyPressed(Key.SHIFT); + break; + case KeyEvent.VK_A: + Keyboard.keyPressed(Key.ALPHA); + break; + case KeyEvent.VK_NUMPAD1: + Keyboard.keyPressed(Key.SQRT); + break; + case KeyEvent.VK_NUMPAD2: + Keyboard.keyPressed(Key.ROOT); + break; + case KeyEvent.VK_NUMPAD3: + Keyboard.keyPressed(Key.POWER_OF_2); + break; + case KeyEvent.VK_NUMPAD5: + Keyboard.keyPressed(Key.POWER_OF_x); + break; } } @@ -83,12 +385,16 @@ public class Keyboard { } private static void keyReleasedRaw(int row, int col) { + KeyboardDebugScreen.keyX = row; + KeyboardDebugScreen.keyY = col; if (row == 1 && col == 1) { - keyReleased(Key.BRIGHTNESS_CYCLE); + //keyReleased(Key.BRIGHTNESS_CYCLE); } } static void keyPressedRaw(int row, int col) { + KeyboardDebugScreen.keyX = row; + KeyboardDebugScreen.keyY = col; if (row == 1 && col == 1) { keyPressed(Key.SHIFT); } else if (row == 1 && col == 2) { @@ -111,7 +417,7 @@ public class Keyboard { } } else if (row == 2 && col == 8) { if (shift) { - keyPressed(Key.NONE); + keyPressed(Key.PI); } else if (alpha) { keyPressed(Key.DRG_CYCLE); } else { @@ -389,34 +695,6 @@ public class Keyboard { break; case NONE: break; - case debug_DEG: - if (Calculator.angleMode.equals("deg") == false) { - refresh = true; - } - Calculator.angleMode = "deg"; - break; - case debug_RAD: - if (Calculator.angleMode.equals("rad") == false) { - refresh = true; - } - Calculator.angleMode = "rad"; - break; - case debug_GRA: - if (Calculator.angleMode.equals("gra") == false) { - refresh = true; - } - Calculator.angleMode = "gra"; - break; - case DRG_CYCLE: - if (Calculator.angleMode.equals("deg") == true) { - Calculator.angleMode = "rad"; - } else if (Calculator.angleMode.equals("rad") == true) { - Calculator.angleMode = "gra"; - } else { - Calculator.angleMode = "deg"; - } - refresh = true; - break; case LETTER_X: letterPressed('X'); break; @@ -498,14 +776,22 @@ public class Keyboard { PARENTHESIS_OPEN, PARENTHESIS_CLOSE, PLUS, MINUS, PLUS_MINUS, MULTIPLY, DIVIDE, EQUAL, DELETE, RESET, LEFT, RIGHT, UP, DOWN, OK, debug1, debug2, debug3, debug4, debug5, SQRT, ROOT, POWER_OF_2, POWER_OF_x, - SINE, COSINE, TANGENT, ARCSINE, ARCCOSINE, ARCTANGENT + SINE, COSINE, TANGENT, ARCSINE, ARCCOSINE, ARCTANGENT, PI } + } /* + +-coord- + NORMAL + SHIFT + ALPHA +------- + |1,1---|1,2---|------|1,4---|------|------|1,7---| |SHIFT |ALPHA |------| ^ |------|------|+BRIGH| |SHIFT |ALPHA |------| |------|------|-BRIGH| @@ -540,7 +826,7 @@ public class Keyboard { | | | | | | |5,8---|4,8---|3,8---|2,8---|1,8-----------------| | 0 | . | | | SOLVE | -| | | | | SIMPLIFY | +| | | |PI | SIMPLIFY | | X | Y | Z |DRGCYCL| | |------|------|------|------|--------------------| diff --git a/src/org/warp/picalculator/device/PIDisplay.java b/src/org/warp/picalculator/device/PIDisplay.java index eae1a10d..a9fe724a 100644 --- a/src/org/warp/picalculator/device/PIDisplay.java +++ b/src/org/warp/picalculator/device/PIDisplay.java @@ -23,6 +23,7 @@ import org.warp.picalculator.Utils; import org.warp.picalculator.device.graphicengine.Display; import org.warp.picalculator.device.graphicengine.RAWFont; import org.warp.picalculator.device.graphicengine.Screen; +import org.warp.picalculator.math.AngleMode; import org.warp.picalculator.math.Calculator; import com.pi4j.wiringpi.Gpio; @@ -89,13 +90,13 @@ public final class PIDisplay { public void setScreen(Screen screen) { if (screen.initialized == false) { if (screen.canBeInHistory) { - Calculator.currentSession = 0; - for (int i = Calculator.sessions.length - 1; i >= 1; i--) { - Calculator.sessions[i] = Calculator.sessions[i - 1]; + PIDisplay.currentSession = 0; + for (int i = PIDisplay.sessions.length - 1; i >= 1; i--) { + PIDisplay.sessions[i] = PIDisplay.sessions[i - 1]; } - Calculator.sessions[0] = screen; + PIDisplay.sessions[0] = screen; } else { - Calculator.currentSession = -1; + PIDisplay.currentSession = -1; } } screen.d = this; @@ -114,11 +115,11 @@ public final class PIDisplay { public void replaceScreen(Screen screen) { if (screen.initialized == false) { if (screen.canBeInHistory) { - Calculator.sessions[Calculator.currentSession] = screen; + PIDisplay.sessions[PIDisplay.currentSession] = screen; } else { - Calculator.currentSession = -1; - for (int i = 0; i < Calculator.sessions.length - 2; i++) { - Calculator.sessions[i] = Calculator.sessions[i + 1]; + PIDisplay.currentSession = -1; + for (int i = 0; i < PIDisplay.sessions.length - 2; i++) { + PIDisplay.sessions[i] = PIDisplay.sessions[i + 1]; } } } @@ -136,13 +137,13 @@ public final class PIDisplay { } public boolean canGoBack() { - if (Calculator.currentSession == -1) { - return Calculator.sessions[0] != null; + if (PIDisplay.currentSession == -1) { + return PIDisplay.sessions[0] != null; } - if (PIDisplay.screen != Calculator.sessions[Calculator.currentSession]) { + if (PIDisplay.screen != PIDisplay.sessions[PIDisplay.currentSession]) { - } else if (Calculator.currentSession + 1 < Calculator.sessions.length) { - if (Calculator.sessions[Calculator.currentSession + 1] != null) { + } else if (PIDisplay.currentSession + 1 < PIDisplay.sessions.length) { + if (PIDisplay.sessions[PIDisplay.currentSession + 1] != null) { } else { return false; @@ -150,7 +151,7 @@ public final class PIDisplay { } else { return false; } - if (Calculator.sessions[Calculator.currentSession] != null) { + if (PIDisplay.sessions[PIDisplay.currentSession] != null) { return true; } return false; @@ -158,22 +159,22 @@ public final class PIDisplay { public void goBack() { if (canGoBack()) { - if (Calculator.currentSession >= 0 && PIDisplay.screen != Calculator.sessions[Calculator.currentSession]) { + if (PIDisplay.currentSession >= 0 && PIDisplay.screen != PIDisplay.sessions[PIDisplay.currentSession]) { } else { - Calculator.currentSession += 1; + PIDisplay.currentSession += 1; } - PIDisplay.screen = Calculator.sessions[Calculator.currentSession]; + PIDisplay.screen = PIDisplay.sessions[PIDisplay.currentSession]; } } public boolean canGoForward() { - if (Calculator.currentSession <= 0) { // -1 e 0 + if (PIDisplay.currentSession <= 0) { // -1 e 0 return false; } - if (PIDisplay.screen != Calculator.sessions[Calculator.currentSession]) { + if (PIDisplay.screen != PIDisplay.sessions[PIDisplay.currentSession]) { - } else if (Calculator.currentSession > 0) { - if (Calculator.sessions[Calculator.currentSession - 1] != null) { + } else if (PIDisplay.currentSession > 0) { + if (PIDisplay.sessions[PIDisplay.currentSession - 1] != null) { } else { return false; @@ -181,7 +182,7 @@ public final class PIDisplay { } else { return false; } - if (Calculator.sessions[Calculator.currentSession] != null) { + if (PIDisplay.sessions[PIDisplay.currentSession] != null) { return true; } return false; @@ -189,12 +190,12 @@ public final class PIDisplay { public void goForward() { if (canGoForward()) { - if (PIDisplay.screen != Calculator.sessions[Calculator.currentSession]) { + if (PIDisplay.screen != PIDisplay.sessions[PIDisplay.currentSession]) { } else { - Calculator.currentSession -= 1; + PIDisplay.currentSession -= 1; } - PIDisplay.screen = Calculator.sessions[Calculator.currentSession]; + PIDisplay.screen = PIDisplay.sessions[PIDisplay.currentSession]; } } @@ -249,15 +250,16 @@ public final class PIDisplay { } else { drawSkinPart(2 + 18 * 1, 2, 16 * 1, 16 * 0, 16 + 16 * 1, 16 + 16 * 0); } - if (Calculator.angleMode == "deg") { + /* + if (Calculator.angleMode == AngleMode.DEG) { drawSkinPart(8 + 18 * 2, 2, 16 * 4, 16 * 0, 16 + 16 * 4, 16 + 16 * 0); drawSkinPart(8 + 18 * 3, 2, 16 * 7, 16 * 0, 16 + 16 * 7, 16 + 16 * 0); drawSkinPart(8 + 18 * 4, 2, 16 * 9, 16 * 0, 16 + 16 * 9, 16 + 16 * 0); - } else if (Calculator.angleMode == "rad") { + } else if (Calculator.angleMode == AngleMode.RAD) { drawSkinPart(8 + 18 * 2, 2, 16 * 5, 16 * 0, 16 + 16 * 5, 16 + 16 * 0); drawSkinPart(8 + 18 * 3, 2, 16 * 6, 16 * 0, 16 + 16 * 6, 16 + 16 * 0); drawSkinPart(8 + 18 * 4, 2, 16 * 9, 16 * 0, 16 + 16 * 9, 16 + 16 * 0); - } else if (Calculator.angleMode == "gra") { + } else if (Calculator.angleMode == AngleMode.GRA) { drawSkinPart(8 + 18 * 2, 2, 16 * 5, 16 * 0, 16 + 16 * 5, 16 + 16 * 0); drawSkinPart(8 + 18 * 3, 2, 16 * 7, 16 * 0, 16 + 16 * 7, 16 + 16 * 0); drawSkinPart(8 + 18 * 4, 2, 16 * 8, 16 * 0, 16 + 16 * 8, 16 + 16 * 0); @@ -265,7 +267,7 @@ public final class PIDisplay { drawSkinPart(8 + 18 * 2, 2, 16 * 5, 16 * 0, 16 + 16 * 5, 16 + 16 * 0); drawSkinPart(8 + 18 * 3, 2, 16 * 7, 16 * 0, 16 + 16 * 7, 16 + 16 * 0); drawSkinPart(8 + 18 * 4, 2, 16 * 9, 16 * 0, 16 + 16 * 9, 16 + 16 * 0); - } + }*/ int padding = 2; @@ -287,7 +289,7 @@ public final class PIDisplay { boolean canGoBack = canGoBack(); boolean canGoForward = canGoForward(); - if (Calculator.haxMode) { + if (Main.haxMode) { drawSkinPart(Main.screenSize[0] - (padding + 16), 2, 16 * 18, 16 * 0, 16 + 16 * 18, 16 + 16 * 0); padding += 18 + 6; } @@ -443,6 +445,8 @@ public final class PIDisplay { } public float[] colore = new float[] { 0.0f, 0.0f, 0.0f, 1.0f }; + public static int currentSession = 0; + public static Screen[] sessions = new Screen[5]; public static void colore(float f1, float f2, float f3, float f4) { PIDisplay.INSTANCE.colore = new float[] { f1, f2, f3, f4 }; diff --git a/src/org/warp/picalculator/device/graphicengine/Display.java b/src/org/warp/picalculator/device/graphicengine/Display.java index f74c91f0..87f081ac 100644 --- a/src/org/warp/picalculator/device/graphicengine/Display.java +++ b/src/org/warp/picalculator/device/graphicengine/Display.java @@ -9,7 +9,7 @@ import org.warp.picalculator.device.PIDisplay; public class Display { - private static Frame INSTANCE = new Frame(); + private static SwingWindow INSTANCE = new SwingWindow(); public static int[] size = new int[] { 1, 1 }; public static BufferedImage g = new BufferedImage(size[0], size[1], BufferedImage.TYPE_INT_RGB); static int[] canvas2d = new int[1]; @@ -35,7 +35,7 @@ public class Display { } public static void create() { - Display.setResizable(Utils.debugOn); + Display.setResizable(Utils.debugOn&!Utils.debugThirdScreen); Display.setDisplayMode(Main.screenSize[0], Main.screenSize[1]); INSTANCE.setVisible(true); initialized = true; diff --git a/src/org/warp/picalculator/device/graphicengine/Frame.java b/src/org/warp/picalculator/device/graphicengine/Frame.java deleted file mode 100644 index 0fc2b3c6..00000000 --- a/src/org/warp/picalculator/device/graphicengine/Frame.java +++ /dev/null @@ -1,494 +0,0 @@ -package org.warp.picalculator.device.graphicengine; - -import java.awt.Cursor; -import java.awt.Dimension; -import java.awt.Graphics; -import java.awt.Point; -import java.awt.Toolkit; -import java.awt.event.ComponentEvent; -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 javax.swing.JFrame; -import javax.swing.JPanel; - -import org.warp.picalculator.Utils; -import org.warp.picalculator.device.Keyboard; -import org.warp.picalculator.device.Keyboard.Key; -import org.warp.picalculator.device.PIDisplay; - -public class Frame extends JFrame { - private static final long serialVersionUID = 2945898937634075491L; - public CustomCanvas c; - public boolean wasResized = false; - - public Frame() { - c = new CustomCanvas(); - c.setDoubleBuffered(false); - this.add(c); -// this.setExtendedState(Frame.MAXIMIZED_BOTH); - Toolkit.getDefaultToolkit().setDynamicLayout(false); - // Transparent 16 x 16 pixel cursor image. - BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB); - - if (!Utils.debugOn) { - // Create a new blank cursor. - Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "blank cursor"); - - // Set the blank cursor to the JFrame. - getContentPane().setCursor(blankCursor); - } - - this.setTitle("Raspberry PI Calculator by XDrake99 (Andrea Cavalli)"); - this.setResizable(Utils.debugOn); - - this.addComponentListener(new ComponentListener() { - @Override - public void componentHidden(ComponentEvent e) { - Display.destroy(); - } - - @Override - public void componentMoved(ComponentEvent e) {} - - @Override - public void componentResized(ComponentEvent e) { - wasResized = true; - } - - @Override - public void componentShown(ComponentEvent e) {} - }); - this.addKeyListener(new KeyListener() { - @Override - public void keyPressed(KeyEvent arg0) { - switch (arg0.getKeyCode()) { - case KeyEvent.VK_ESCAPE: - Keyboard.keyPressed(Key.POWER); - break; - case KeyEvent.VK_S: - if (Keyboard.shift) { - Keyboard.keyPressed(Key.ARCSINE); - } else if (Keyboard.alpha) { - Keyboard.keyPressed(Key.NONE); - } else { - Keyboard.keyPressed(Key.SINE); - } - break; - case KeyEvent.VK_C: - if (Keyboard.shift) { - Keyboard.keyPressed(Key.ARCCOSINE); - } else if (Keyboard.alpha) { - Keyboard.keyPressed(Key.NONE); - } else { - Keyboard.keyPressed(Key.COSINE); - } - break; - case KeyEvent.VK_T: - if (Keyboard.shift) { - Keyboard.keyPressed(Key.ARCTANGENT); - } else if (Keyboard.alpha) { - Keyboard.keyPressed(Key.NONE); - } else { - Keyboard.keyPressed(Key.TANGENT); - } - break; - case KeyEvent.VK_D: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.debug_DEG); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_R: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.debug_RAD); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_G: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.debug_GRA); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_X: - if (Keyboard.alpha) { - Keyboard.keyPressed(Key.LETTER_X); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_Y: - if (Keyboard.alpha) { - Keyboard.keyPressed(Key.LETTER_Y); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_B: - if (Keyboard.shift) { - Keyboard.keyPressed(Key.BRIGHTNESS_CYCLE_REVERSE); - } else if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.BRIGHTNESS_CYCLE); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_ENTER: - if (Keyboard.shift) { - Keyboard.keyPressed(Key.SIMPLIFY); - } else if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.SOLVE); - } else { - Keyboard.keyPressed(Key.NONE); - } - int row = 2; - int col = 1; - Keyboard.debugKeysDown[row-1][col-1] = true; - break; - case KeyEvent.VK_1: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.NUM1); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_2: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.NUM2); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_3: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.NUM3); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_4: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.NUM4); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_5: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.NUM5); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_6: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.NUM6); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_7: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.NUM7); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_8: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.NUM8); - } else if (Keyboard.shift) { - Keyboard.keyPressed(Key.PARENTHESIS_OPEN); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_9: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.NUM9); - } else if (Keyboard.shift) { - Keyboard.keyPressed(Key.PARENTHESIS_CLOSE); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_0: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.NUM0); - } else if (Keyboard.shift) { - Keyboard.keyPressed(Key.EQUAL); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_ADD: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.PLUS); - } else if (Keyboard.shift) { - Keyboard.keyPressed(Key.PLUS_MINUS); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_SUBTRACT: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.MINUS); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_MULTIPLY: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.MULTIPLY); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_DIVIDE: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.DIVIDE); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_BACK_SPACE: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.DELETE); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_DELETE: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.RESET); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_LEFT: - //LEFT - row = 2; - col = 3; - Keyboard.debugKeysDown[row-1][col-1] = true; - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.LEFT); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_RIGHT: - //RIGHT - row = 2; - col = 5; - Keyboard.debugKeysDown[row-1][col-1] = true; - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.RIGHT); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_NUMPAD4: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.HISTORY_BACK); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_NUMPAD6: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.HISTORY_FORWARD); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_PERIOD: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyPressed(Key.DOT); - } else { - Keyboard.keyPressed(Key.NONE); - } - break; - case KeyEvent.VK_SHIFT: - Keyboard.keyPressed(Key.SHIFT); - break; - case KeyEvent.VK_A: - Keyboard.keyPressed(Key.ALPHA); - break; - case KeyEvent.VK_NUMPAD1: - Keyboard.keyPressed(Key.SQRT); - break; - case KeyEvent.VK_NUMPAD2: - Keyboard.keyPressed(Key.ROOT); - break; - case KeyEvent.VK_NUMPAD3: - Keyboard.keyPressed(Key.POWER_OF_2); - break; - case KeyEvent.VK_NUMPAD5: - Keyboard.keyPressed(Key.POWER_OF_x); - break; - } - } - - @Override - public void keyReleased(KeyEvent arg0) { - switch (arg0.getKeyCode()) { - case KeyEvent.VK_ESCAPE: - Keyboard.keyReleased(Key.POWER); - break; - case KeyEvent.VK_D: - Keyboard.keyReleased(Key.debug_DEG); - break; - case KeyEvent.VK_R: - Keyboard.keyReleased(Key.debug_RAD); - break; - case KeyEvent.VK_G: - Keyboard.keyReleased(Key.debug_GRA); - break; - case KeyEvent.VK_X: - if (Keyboard.alpha) { - Keyboard.keyReleased(Key.LETTER_X); - } else { - Keyboard.keyReleased(Key.NONE); - } - break; - case KeyEvent.VK_B: - if (Keyboard.shift) { - Keyboard.keyReleased(Key.BRIGHTNESS_CYCLE_REVERSE); - } else if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyReleased(Key.BRIGHTNESS_CYCLE); - } else { - Keyboard.keyReleased(Key.NONE); - } - break; - case KeyEvent.VK_ENTER: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyReleased(Key.SOLVE); - } else { - Keyboard.keyReleased(Key.NONE); - } - int row = 2; - int col = 1; - Keyboard.debugKeysDown[row-1][col-1] = false; - break; - case KeyEvent.VK_1: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyReleased(Key.debug1); - } else { - Keyboard.keyReleased(Key.NONE); - } - break; - case KeyEvent.VK_2: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyReleased(Key.debug2); - } else { - Keyboard.keyReleased(Key.NONE); - } - break; - case KeyEvent.VK_3: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyReleased(Key.debug3); - } else { - Keyboard.keyReleased(Key.NONE); - } - break; - case KeyEvent.VK_4: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyReleased(Key.debug4); - } else { - Keyboard.keyReleased(Key.NONE); - } - break; - case KeyEvent.VK_5: - if (!Keyboard.shift && !Keyboard.alpha) { - Keyboard.keyReleased(Key.debug5); - } else { - Keyboard.keyReleased(Key.NONE); - } - break; - case KeyEvent.VK_SHIFT: - Keyboard.keyReleased(Key.SHIFT); - break; - case KeyEvent.VK_A: - Keyboard.keyReleased(Key.ALPHA); - break; - case KeyEvent.VK_LEFT: - //LEFT - row = 2; - col = 3; - Keyboard.debugKeysDown[row-1][col-1] = false; - case KeyEvent.VK_RIGHT: - //RIGHT - row = 2; - col = 5; - Keyboard.debugKeysDown[row-1][col-1] = false; - } - } - - @Override - public void keyTyped(KeyEvent arg0) { - // TODO Auto-generated method stub - - } - }); - } - - @Override - public void setSize(int width, int height) { - c.setSize(width, height); - super.getContentPane().setPreferredSize(new Dimension(width, height)); - super.pack(); - } - - @Override - public Dimension getSize() { - return c.getSize(); - } - - @Override - public int getWidth() { - return c.getWidth(); - } - - @Override - public int getHeight() { - return c.getHeight(); - } - -// private static ArrayList mediaValori = new ArrayList(); - - public static class CustomCanvas extends JPanel { - - /** - * - */ - private static final long serialVersionUID = 605243927485370885L; - - @Override - public void paintComponent(Graphics g) { -// long time1 = System.nanoTime(); - PIDisplay.INSTANCE.refresh(); - - 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())); - } - } -} diff --git a/src/org/warp/picalculator/device/graphicengine/SwingWindow.java b/src/org/warp/picalculator/device/graphicengine/SwingWindow.java new file mode 100644 index 00000000..400cf362 --- /dev/null +++ b/src/org/warp/picalculator/device/graphicengine/SwingWindow.java @@ -0,0 +1,241 @@ +package org.warp.picalculator.device.graphicengine; + +import java.awt.Cursor; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Point; +import java.awt.Toolkit; +import java.awt.event.ComponentEvent; +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 javax.swing.JFrame; +import javax.swing.JPanel; + +import org.warp.picalculator.Utils; +import org.warp.picalculator.device.Keyboard; +import org.warp.picalculator.device.Keyboard.Key; +import org.warp.picalculator.device.PIDisplay; + +public class SwingWindow extends JFrame { + private static final long serialVersionUID = 2945898937634075491L; + public CustomCanvas c; + public boolean wasResized = false; + + public SwingWindow() { + c = new CustomCanvas(); + c.setDoubleBuffered(false); + this.add(c); +// this.setExtendedState(Frame.MAXIMIZED_BOTH); + Toolkit.getDefaultToolkit().setDynamicLayout(false); + // Transparent 16 x 16 pixel cursor image. + BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB); + + if (Utils.debugOn) { + if (Utils.debugThirdScreen) { + this.setLocation(2880, 900); + this.setResizable(false); + this.setAlwaysOnTop(true); + } + } else { + // Create a new blank cursor. + Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "blank cursor"); + + // Set the blank cursor to the JFrame. + getContentPane().setCursor(blankCursor); + + this.setResizable(false); + } + + this.setTitle("Raspberry PI Calculator by XDrake99 (Andrea Cavalli)"); + + + this.addComponentListener(new ComponentListener() { + @Override + public void componentHidden(ComponentEvent e) { + Display.destroy(); + } + + @Override + public void componentMoved(ComponentEvent e) {} + + @Override + public void componentResized(ComponentEvent e) { + wasResized = true; + } + + @Override + public void componentShown(ComponentEvent e) {} + }); + this.addKeyListener(new KeyListener() { + @Override + public void keyPressed(KeyEvent arg0) { + Keyboard.debugKeyEvent = arg0; + } + + @Override + public void keyReleased(KeyEvent arg0) { + switch (arg0.getKeyCode()) { + case KeyEvent.VK_ESCAPE: + Keyboard.keyReleased(Key.POWER); + break; + case KeyEvent.VK_D: + Keyboard.keyReleased(Key.debug_DEG); + break; + case KeyEvent.VK_R: + Keyboard.keyReleased(Key.debug_RAD); + break; + case KeyEvent.VK_G: + Keyboard.keyReleased(Key.debug_GRA); + break; + case KeyEvent.VK_X: + if (Keyboard.alpha) { + Keyboard.keyReleased(Key.LETTER_X); + } else { + Keyboard.keyReleased(Key.NONE); + } + break; + case KeyEvent.VK_P: + if (Keyboard.alpha) { + Keyboard.keyReleased(Key.PI); + } else { + Keyboard.keyReleased(Key.NONE); + } + break; + case KeyEvent.VK_B: + if (Keyboard.shift) { + Keyboard.keyReleased(Key.BRIGHTNESS_CYCLE_REVERSE); + } else if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyReleased(Key.BRIGHTNESS_CYCLE); + } else { + Keyboard.keyReleased(Key.NONE); + } + break; + case KeyEvent.VK_ENTER: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyReleased(Key.SOLVE); + } else { + Keyboard.keyReleased(Key.NONE); + } + int row = 2; + int col = 1; + Keyboard.debugKeysDown[row-1][col-1] = false; + break; + case KeyEvent.VK_1: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyReleased(Key.debug1); + } else { + Keyboard.keyReleased(Key.NONE); + } + break; + case KeyEvent.VK_2: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyReleased(Key.debug2); + } else { + Keyboard.keyReleased(Key.NONE); + } + break; + case KeyEvent.VK_3: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyReleased(Key.debug3); + } else { + Keyboard.keyReleased(Key.NONE); + } + break; + case KeyEvent.VK_4: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyReleased(Key.debug4); + } else { + Keyboard.keyReleased(Key.NONE); + } + break; + case KeyEvent.VK_5: + if (!Keyboard.shift && !Keyboard.alpha) { + Keyboard.keyReleased(Key.debug5); + } else { + Keyboard.keyReleased(Key.NONE); + } + break; + case KeyEvent.VK_SHIFT: + Keyboard.keyReleased(Key.SHIFT); + break; + case KeyEvent.VK_A: + Keyboard.keyReleased(Key.ALPHA); + break; + case KeyEvent.VK_LEFT: + //LEFT + row = 2; + col = 3; + Keyboard.debugKeysDown[row-1][col-1] = false; + case KeyEvent.VK_RIGHT: + //RIGHT + row = 2; + col = 5; + Keyboard.debugKeysDown[row-1][col-1] = false; + } + } + + @Override + public void keyTyped(KeyEvent arg0) { + // TODO Auto-generated method stub + + } + }); + } + + @Override + public void setSize(int width, int height) { + c.setSize(width, height); + super.getContentPane().setPreferredSize(new Dimension(width, height)); + super.pack(); + } + + @Override + public Dimension getSize() { + return c.getSize(); + } + + @Override + public int getWidth() { + return c.getWidth(); + } + + @Override + public int getHeight() { + return c.getHeight(); + } + +// private static ArrayList mediaValori = new ArrayList(); + + public static class CustomCanvas extends JPanel { + + /** + * + */ + private static final long serialVersionUID = 605243927485370885L; + + @Override + public void paintComponent(Graphics g) { +// long time1 = System.nanoTime(); + PIDisplay.INSTANCE.refresh(); + + 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())); + } + } +} diff --git a/src/org/warp/picalculator/math/AngleMode.java b/src/org/warp/picalculator/math/AngleMode.java new file mode 100644 index 00000000..2780b927 --- /dev/null +++ b/src/org/warp/picalculator/math/AngleMode.java @@ -0,0 +1,7 @@ +package org.warp.picalculator.math; + +public enum AngleMode { + DEG, + RAD, + GRA +} diff --git a/src/org/warp/picalculator/math/Calculator.java b/src/org/warp/picalculator/math/Calculator.java index 881bb339..9ba1eb49 100644 --- a/src/org/warp/picalculator/math/Calculator.java +++ b/src/org/warp/picalculator/math/Calculator.java @@ -11,10 +11,10 @@ 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.Screen; import org.warp.picalculator.math.functions.Expression; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.Number; +import org.warp.picalculator.math.functions.Variable; import org.warp.picalculator.math.functions.equations.Equation; import org.warp.picalculator.math.functions.equations.EquationsSystem; import org.warp.picalculator.screens.MathInputScreen; @@ -22,12 +22,13 @@ import org.warp.picalculator.screens.SolveEquationScreen; public class Calculator { - public static String angleMode = "deg"; - public static Screen[] sessions = new Screen[5]; - public static int currentSession = 0; - public static boolean haxMode = true; - public static boolean exactMode = false; - public static Function parseString(String string) throws Error { + public AngleMode angleMode = AngleMode.DEG; + public boolean exactMode = false; + + public Calculator() { + } + + public Function parseString(String string) throws Error { if (string.contains("{")) { if (!string.startsWith("{")) { throw new Error(Errors.SYNTAX_ERROR); @@ -45,7 +46,7 @@ public class Calculator { } } - public static Function parseEquationString(String string) throws Error { + public Function parseEquationString(String string) throws Error { String[] parts = string.split("="); if (parts.length == 1) { Equation e = new Equation(null, null, null); @@ -62,7 +63,7 @@ public class Calculator { } } - public static ArrayList solveExpression(ArrayList input) throws Error { + public ArrayList solveExpression(ArrayList input) throws Error { ArrayList results = new ArrayList<>(); ArrayList partialResults = new ArrayList<>(); for (Function f : input) { @@ -92,33 +93,7 @@ public class Calculator { return results; } - public static void solve(MathInputScreen es) throws Error { - for (Function f : es.f) { - if (f instanceof Equation) { - PIDisplay.INSTANCE.setScreen(new SolveEquationScreen(es)); - return; - } - } - - ArrayList results = solveExpression(es.f); - 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(EquationScreen equationScreen, char letter) throws Error { + /*public void solve(EquationScreen equationScreen, char letter) throws Error { if (Calculator.currentSession == 0 && Calculator.sessions[0] instanceof EquationScreen) { EquationScreen es = (EquationScreen) Calculator.sessions[0]; ArrayList f = es.f; @@ -138,41 +113,4 @@ public class Calculator { } }*/ - public static void simplify(MathInputScreen es) throws Error { - ArrayList results = new ArrayList<>(); - ArrayList partialResults = new ArrayList<>(); - for (Function f : es.f2) { - if (f instanceof Equation) { - PIDisplay.INSTANCE.setScreen(new SolveEquationScreen(es)); - } else { - results.add(f); - 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(); - } - } - 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(); - } - } - } - } diff --git a/src/org/warp/picalculator/math/MathematicalSymbols.java b/src/org/warp/picalculator/math/MathematicalSymbols.java index fba603f9..e6fbd722 100644 --- a/src/org/warp/picalculator/math/MathematicalSymbols.java +++ b/src/org/warp/picalculator/math/MathematicalSymbols.java @@ -24,6 +24,7 @@ public class MathematicalSymbols { public static final String ARC_SINE = "Ⓕ"; public static final String ARC_COSINE = "Ⓖ"; public static final String ARC_TANGENT = "Ⓗ"; + public static final String PI = "π"; public static final String[] functions() { return concat(functionsNSN(), functionsSN()); @@ -50,7 +51,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", "ⓧ", "Ⓨ", "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", "ⓧ", "Ⓨ", "Z", PI}; } public static String[] genericSyntax() { diff --git a/src/org/warp/picalculator/math/functions/Number.java b/src/org/warp/picalculator/math/functions/Number.java index d4a1f565..fe552965 100644 --- a/src/org/warp/picalculator/math/functions/Number.java +++ b/src/org/warp/picalculator/math/functions/Number.java @@ -27,7 +27,7 @@ public class Number implements Function { protected int line; protected boolean small; - public Number(Function parent, BigInteger val) { + public Number(Calculator calc, Function parent, BigInteger val) { this.parent = parent; term = new BigDecimal(val).setScale(Utils.scale, Utils.scaleMode2); } diff --git a/src/org/warp/picalculator/math/functions/Variable.java b/src/org/warp/picalculator/math/functions/Variable.java index 718d4553..cec605c2 100644 --- a/src/org/warp/picalculator/math/functions/Variable.java +++ b/src/org/warp/picalculator/math/functions/Variable.java @@ -95,6 +95,16 @@ public class Variable implements Function { private int calcLine() { return Utils.getFontHeight(small) / 2; } + + public static class VariableValue { + public final Variable v; + public final Number n; + + public VariableValue(Variable v, Number n) { + this.v = v; + this.n = n; + } + } @Override public Variable clone() { diff --git a/src/org/warp/picalculator/math/functions/trigonometry/Sine.java b/src/org/warp/picalculator/math/functions/trigonometry/Sine.java index 7df8fa25..0bc8b3b6 100644 --- a/src/org/warp/picalculator/math/functions/trigonometry/Sine.java +++ b/src/org/warp/picalculator/math/functions/trigonometry/Sine.java @@ -1,11 +1,19 @@ package org.warp.picalculator.math.functions.trigonometry; +import java.math.BigInteger; import java.util.ArrayList; +import org.nevec.rjm.BigDecimalMath; +import org.nevec.rjm.BigIntegerMath; import org.warp.picalculator.Error; +import org.warp.picalculator.Utils; +import org.warp.picalculator.math.AngleMode; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.MathematicalSymbols; import org.warp.picalculator.math.functions.AnteriorFunction; import org.warp.picalculator.math.functions.Function; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Number; public class Sine extends AnteriorFunction { @@ -24,15 +32,27 @@ public class Sine extends AnteriorFunction { } @Override - public ArrayList solve() throws Error { - // TODO Auto-generated method stub - return null; + protected boolean isSolvable() { + if (variable instanceof Number) { + if (Calculator.exactMode == false) { + return true; + } + } + if (Calculator.angleMode == AngleMode.DEG) { + Function[] solvableValues = new Function[]{new Number(null, 0), new Number(null, 30), new Number(null, 90), }; + } + return false; } @Override - protected boolean isSolvable() { - // TODO Auto-generated method stub - return false; + public ArrayList solve() throws Error { + ArrayList results = new ArrayList<>(); + if (variable instanceof Number) { + if (Calculator.exactMode == false) { + results.add(new Number(parent, BigDecimalMath.sin(((Number) variable).getTerm()))); + } + } + return results; } @Override diff --git a/src/org/warp/picalculator/math/rules/methods/DivisionRule1.java b/src/org/warp/picalculator/math/rules/methods/DivisionRule1.java new file mode 100644 index 00000000..2f5e12ad --- /dev/null +++ b/src/org/warp/picalculator/math/rules/methods/DivisionRule1.java @@ -0,0 +1,96 @@ +package org.warp.picalculator.math.rules.methods; + +import java.util.ArrayList; + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.functions.Function; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Division; +import org.warp.picalculator.math.functions.Number; + +/** + * Division method
+ * Example: (XY)/(YZ) = X/Z + * @author Andrea Cavalli + * + */ +public class DivisionRule1 { + + public static boolean compare(Function f) { + return ((Division)f).getVariable1().isSolved() && ((Division)f).getVariable2().isSolved() && !(((Division)f).getVariable1() instanceof Number && ((Division)f).getVariable2() instanceof Number) && getFirstWorkingDivisionCouple(getDivisionElements(f)) != null; + } + + public static ArrayList execute(Function f) throws Error { + Function result; + ArrayList elements = getDivisionElements(f); + int[] workingElementCouple = getFirstWorkingDivisionCouple(elements); + Function elem1 = elements.get(workingElementCouple[0]); + Function elem2 = elements.get(workingElementCouple[1]); + + final int size = elements.size(); + Function prec = new Multiplication(null, elem1, elem2); + elem1.setParent(prec); + elem2.setParent(prec); + for (int i = size-1; i >= 0; i--) { + if (i != workingElementCouple[0] & i != workingElementCouple[1]) { + Function a = prec; + Function b = elements.get(i); + prec = new Multiplication(null, a, b); + a.setParent(prec); + b.setParent(prec); + } + } + prec.setParent(f.getParent()); + + result = prec; + + ArrayList results = new ArrayList<>(); + results.add(result); + return results; + } + + private static ArrayList getDivisionElements(Division division) { + ArrayList elementsNumerator = new ArrayList<>(); + Function numMult = division.getVariable1(); + while (numMult instanceof Multiplication) { + elementsNumerator.add(((Multiplication) numMult).getVariable1()); + numMult = ((Multiplication) numMult).getVariable2(); + } + elementsNumerator.add(numMult); + + ArrayList elementsDenominator = new ArrayList<>(); + Function denomMult = division.getVariable1(); + while (denomMult instanceof Multiplication) { + elementsDenominator.add(((Multiplication) denomMult).getVariable1()); + denomMult = ((Multiplication) denomMult).getVariable2(); + } + elementsDenominator.add(denomMult); + + + return elements; + } + + private static int[] getFirstWorkingDivisionCouple(ArrayList elements) { + final int size = elements.size(); + Function a; + Function b; + if (elements.size() == 2) { + return null; + } + for (int i = 0; i < size; i++) { + a = elements.get(i); + for (int j = 0; j < size; j++) { + b = elements.get(j); + if (i != j) { + Function testFunc; + testFunc = new Multiplication(null, a, b); + if (!testFunc.isSolved()) { + return new int[]{i, j}; + } + } + } + } + return null; + } + +} diff --git a/src/org/warp/picalculator/screens/ChooseVariableValueScreen.java b/src/org/warp/picalculator/screens/ChooseVariableValueScreen.java new file mode 100644 index 00000000..6a1fbd06 --- /dev/null +++ b/src/org/warp/picalculator/screens/ChooseVariableValueScreen.java @@ -0,0 +1,85 @@ +package org.warp.picalculator.screens; + +import static org.warp.picalculator.device.graphicengine.Display.Render.glColor3i; +import static org.warp.picalculator.device.graphicengine.Display.Render.glColor4i; +import static org.warp.picalculator.device.graphicengine.Display.Render.glDrawStringCenter; + +import org.warp.picalculator.Error; +import org.warp.picalculator.Main; +import org.warp.picalculator.device.Keyboard.Key; +import org.warp.picalculator.device.PIDisplay; +import org.warp.picalculator.device.graphicengine.Screen; +import org.warp.picalculator.math.Calculator; +import org.warp.picalculator.math.functions.Function; +import org.warp.picalculator.math.functions.Variable; +import org.warp.picalculator.math.functions.Variable.VariableValue; + +public class ChooseVariableValueScreen extends Screen { + + @SuppressWarnings("unused") + private MathInputScreen es; + public Function resultNumberValue; + + public ChooseVariableValueScreen(MathInputScreen es, VariableValue variableValue) { + super(); + canBeInHistory = false; + + this.es = es; + } + + @Override + public void created() throws InterruptedException { + } + + @Override + public void init() throws InterruptedException {} + + @Override + public void render() { + glColor4i(0, 0, 0, 64); + glDrawStringCenter(Main.screenSize[0]/2+1, Main.screenSize[1]/4, "WORK IN PROGRESS. THIS SCREEN MUST HAVE A GUI TO SELECT THE VARIABLE TO SOLVE."); + glDrawStringCenter(Main.screenSize[0]/2, Main.screenSize[1]/4+1, "WORK IN PROGRESS. THIS SCREEN MUST HAVE A GUI TO SELECT THE VARIABLE TO SOLVE."); + glDrawStringCenter(Main.screenSize[0]/2+1, Main.screenSize[1]/4+1, "WORK IN PROGRESS. THIS SCREEN MUST HAVE A GUI TO SELECT THE VARIABLE TO SOLVE."); + glColor3i(255, 0, 0); + glDrawStringCenter(Main.screenSize[0]/2, Main.screenSize[1]/4, "WORK IN PROGRESS. THIS SCREEN MUST HAVE A GUI TO SELECT THE VARIABLE TO SOLVE."); + } + + @Override + public void beforeRender(float dt) { + + } + + @Override + public boolean mustBeRefreshed() { + return true; + } + + @Override + public boolean keyPressed(Key k) { + switch (k) { + case LETTER_X: +// PIDisplay.INSTANCE.goBack(); +// try { +// Calculator.solveExpression('X'); +// } catch (Error e) { +// Screen scr = PIDisplay.INSTANCE.getScreen(); +// if (scr instanceof MathInputScreen) { +// MathInputScreen escr = (MathInputScreen) scr; +// escr.errorLevel = 1; +// escr.err2 = e; +// } else { +// e.printStackTrace(); +// } +// } + return true; + default: + return false; + } + } + + @Override + public boolean keyReleased(Key k) { + return false; + } + +} diff --git a/src/org/warp/picalculator/screens/KeyboardDebugScreen.java b/src/org/warp/picalculator/screens/KeyboardDebugScreen.java new file mode 100644 index 00000000..0ef15378 --- /dev/null +++ b/src/org/warp/picalculator/screens/KeyboardDebugScreen.java @@ -0,0 +1,229 @@ +package org.warp.picalculator.screens; + +import static org.warp.picalculator.device.PIDisplay.colore; +import static org.warp.picalculator.device.PIDisplay.fonts; +import static org.warp.picalculator.device.graphicengine.Display.Render.*; + +import org.warp.picalculator.Main; +import org.warp.picalculator.device.Keyboard.Key; +import org.warp.picalculator.device.graphicengine.Screen; + +public class KeyboardDebugScreen extends Screen { + + public String key; + public String keyevent; + public static int keyX; + public static int keyY; + public static String[] log = new String[]{"POWER ON","LOADING","LOADED","DONE","---"}; + public long beforetime; + + public KeyboardDebugScreen() { + super(); + canBeInHistory = false; + } + + @Override + public void created() throws InterruptedException { + } + + @Override + public void init() throws InterruptedException { + } + + @Override + public void render() { + glSetFont(fonts[2]); + colore(0.75f, 0.0f, 0.0f, 1.0f); + glDrawStringRight(Main.screenSize[0] - 10, 30, "-" + keyevent.toUpperCase() + "-"); + if (keyevent != "NONE") { + glSetFont(fonts[2]); + colore(0.0f, 0.0f, 0.0f, 1.0f); + glDrawStringLeft(10, 30, "Key position"); + glDrawStringLeft(10, 45, "X: " + keyX + ", Y:" + keyY); + glDrawStringLeft(10, 65, "Key value"); + glDrawStringLeft(10, 80, key); + } + glSetFont(fonts[3]); + colore(0.0f, 0.0f, 0.0f, 1.0f); + for (int i = 0; i < 5; i++) { + if (log[i] != null) { + glDrawStringLeft(10, 230 + 15*(i+1), log[i].toUpperCase()); + } + } + + //FROM SERIAL + colore(0.0f, 0.0f, 0.0f, 1.0f); + glFillRect(-80+100+200, 90, 5, 5); + glFillRect(-80+100, 100, 200, 70); + glSetFont(fonts[2]); + colore(1.0f, 1.0f, 1.0f, 1.0f); + glDrawStringCenter(-80+100+200/2, 100+70/2-(currentFont.charH/2), "FROM SERIAL"); + glSetFont(fonts[3]); + colore(0.0f, 0.0f, 1.0f, 1.0f); + for (int i = 0; i < 8; i++) { + if (pinsA[i] == 1) { + colore(0.0f, 1.0f, 0.0f, 1.0f); + } else if (pinsA[i] == 2) { + colore(0.5f, 0.5f, 1.0f, 1.0f); + } else if (pinsA[i] == -1) { + colore(0.7f, 0.7f, 0.7f, 1.0f); + } else if (pinsA[i] == 0) { + colore(1.0f, 0.0f, 0.0f, 1.0f); + } + glFillRect(-80+103+25*(7-i), 80, 20, 20); + colore(0.0f, 0.0f, 0.0f, 1.0f); + glDrawStringCenter(-80+113+25*(7-i), 90-(currentFont.charH/2), ""+(i+1)); + } + for (int i = 15; i >= 8; i--) { + if (pinsA[i] == 1) { + colore(0.0f, 1.0f, 0.0f, 1.0f); + } else if (pinsA[i] == 2) { + colore(0.5f, 0.5f, 1.0f, 1.0f); + } else if (pinsA[i] == -1) { + colore(0.7f, 0.7f, 0.7f, 1.0f); + } else if (pinsA[i] == 0) { + colore(1.0f, 0.0f, 0.0f, 1.0f); + } + glFillRect(-80+103+25*(i-8), 170, 20, 20); + colore(0.0f, 0.0f, 0.0f, 1.0f); + glDrawStringCenter(-80+113+25*(i-8), 180-(currentFont.charH/2), ""+(i+1)); + } + for (int i = 0; i < 8; i++) { + if (dataA[i]) { + colore(0.0f, 1.0f, 0.0f, 1.0f); + } else { + colore(1.0f, 0.0f, 0.0f, 1.0f); + } + glFillRect(-80+160+10*(i), 150, 8, 8); + } + + //TO SERIAL + colore(0.0f, 0.0f, 0.0f, 1.0f); + glFillRect(150+90, 200, 5, 5); + glFillRect(150+100, 100, 200, 70); + glSetFont(fonts[2]); + colore(1.0f, 1.0f, 1.0f, 1.0f); + glDrawStringCenter(150+100+200/2, 100+70/2-(currentFont.charH/2), "TO SERIAL"); + glSetFont(fonts[3]); + colore(0.0f, 0.0f, 1.0f, 1.0f); + for (int i = 15; i >= 8; i--) { + if (pinsB[i] == 1) { + colore(0.0f, 1.0f, 0.0f, 1.0f); + } else if (pinsB[i] == 2) { + colore(0.5f, 0.5f, 1.0f, 1.0f); + } else if (pinsB[i] == -1) { + colore(0.7f, 0.7f, 0.7f, 1.0f); + } else if (pinsB[i] == 0) { + colore(1.0f, 0.0f, 0.0f, 1.0f); + } + glFillRect(150+103+25*(15-i), 80, 20, 20); + colore(0.0f, 0.0f, 0.0f, 1.0f); + glDrawStringCenter(150+113+25*(15-i), 90-(currentFont.charH/2), ""+(i+1)); + } + for (int i = 7; i >= 0; i--) { + if (pinsB[i] == 1) { + colore(0.0f, 1.0f, 0.0f, 1.0f); + } else if (pinsB[i] == 2) { + colore(0.5f, 0.5f, 1.0f, 1.0f); + } else if (pinsB[i] == -1) { + colore(0.7f, 0.7f, 0.7f, 1.0f); + } else if (pinsB[i] == 0) { + colore(1.0f, 0.0f, 0.0f, 1.0f); + } + glFillRect(150+103+25*(i), 170, 20, 20); + colore(0.0f, 0.0f, 0.0f, 1.0f); + glDrawStringCenter(150+113+25*(i), 180-(currentFont.charH/2), ""+(i+1)); + } + for (int i = 0; i < 8; i++) { + if (dataB[i]) { + colore(0.0f, 1.0f, 0.0f, 1.0f); + } else { + colore(1.0f, 0.0f, 0.0f, 1.0f); + } + glFillRect(150+160+10*(i), 150, 8, 8); + } + + //GPIO + for (int i = 0; i < 40; i++) { + if (gpio[i] == true) { + colore(0.0f, 1.0f, 0.0f, 1.0f); + } else { + colore(1.0f, 0.0f, 0.0f, 1.0f); + } + if (i % 2 == 0) { + glFillRect(53+15*((i)/2), 50, 5, 5); + colore(0.0f, 0.0f, 0.0f, 1.0f); + glDrawStringCenter(55+15*((i)/2), 60, ""+(i+1)); + } else { + glFillRect(53+15*((i-1)/2), 40, 5, 5); + colore(0.0f, 0.0f, 0.0f, 1.0f); + glDrawStringCenter(55+15*((i-1)/2), 35-currentFont.charH, ""+(i+1)); + } + } + + //KEYS + for (int c = 0; c < 8; c++) { + for (int r = 0; r < 8; r++) { + if (ks[c][r]) { + colore(0.0f, 1.0f, 0.0f, 1.0f); + } else { + colore(1.0f, 0.0f, 0.0f, 1.0f); + } + glFillRect(250+6*c, 250+6*r, 5, 5); + } + } + } + + @Override + public void beforeRender(float dt) { + if (System.currentTimeMillis()-beforetime >= 1000) { + keyevent = "NONE"; + keyX = 0; + keyY = 0; + key = ""; + } + } + + @Override + public boolean mustBeRefreshed() { + return true; + } + + @Override + public boolean keyPressed(Key k) { + beforetime = System.currentTimeMillis(); + keyevent = "PRESSED"; + keyX = 0; + keyY = 0; + key = k.toString(); + return false; + } + + @Override + public boolean keyReleased(Key k) { + beforetime = System.currentTimeMillis(); + keyevent = "RELEASED"; + keyX = 0; + keyY = 0; + key = k.toString(); + return false; + } + + + public static int[] pinsA = new int[]{2, 2, 2, 2, 2, 2, 2, 1, -1, -1, 0, 0, 0, 0, 2, -1}; + public static int[] pinsB = new int[]{0, 0, 2, 2, 2, 2, -1, 1, 0, -1, 2, 2, 2, 2, 0, -1}; + public static boolean[] dataA = new boolean[8]; + public static boolean[] dataB = new boolean[8]; + public static boolean[][] ks = new boolean[8][8]; + public static boolean[] gpio = new boolean[40]; + + public static void log(String str) { + String[] newlog = log; + for (int i = 1; i < 5; i++) { + newlog[i-1] = newlog[i]; + } + newlog[4] = "[" + System.currentTimeMillis() + "]" + str; + log = newlog; + } + +} diff --git a/src/org/warp/picalculator/screens/MathInputScreen.java b/src/org/warp/picalculator/screens/MathInputScreen.java index 23a88523..c32608cb 100644 --- a/src/org/warp/picalculator/screens/MathInputScreen.java +++ b/src/org/warp/picalculator/screens/MathInputScreen.java @@ -12,7 +12,11 @@ import static org.warp.picalculator.device.graphicengine.Display.Render.glSetFon import java.io.PrintWriter; import java.io.StringWriter; import java.util.ArrayList; +import java.util.Collections; import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; import org.warp.picalculator.Error; import org.warp.picalculator.Errors; @@ -24,9 +28,17 @@ import org.warp.picalculator.device.PIDisplay; import org.warp.picalculator.device.graphicengine.Display; import org.warp.picalculator.device.graphicengine.RAWFont; import org.warp.picalculator.device.graphicengine.Screen; +import org.warp.picalculator.math.AngleMode; import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.MathematicalSymbols; +import org.warp.picalculator.math.functions.AnteriorFunction; import org.warp.picalculator.math.functions.Function; +import org.warp.picalculator.math.functions.FunctionMultipleValues; +import org.warp.picalculator.math.functions.FunctionTwoValues; +import org.warp.picalculator.math.functions.Variable; +import org.warp.picalculator.math.functions.Variable.VariableValue; +import org.warp.picalculator.math.functions.equations.Equation; +import org.warp.picalculator.math.functions.Number; public class MathInputScreen extends Screen { @@ -37,16 +49,20 @@ public class MathInputScreen extends Screen { public volatile float showCaretDelta = 0f; public ArrayList f; public ArrayList f2; + public ArrayList variablesValues; public int resultsCount; public boolean autoscroll; public int scrollX = 0; public int errorLevel = 0; // 0 = nessuno, 1 = risultato, 2 = tutto boolean mustRefresh = true; boolean afterDoNextStep = false; - + public final Calculator calc; + public MathInputScreen() { super(); canBeInHistory = true; + + calc = new Calculator(); } @Override @@ -89,6 +105,7 @@ public class MathInputScreen extends Screen { if (f == null & f2 == null) { f = new ArrayList(); f2 = new ArrayList(); + variablesValues = new ArrayList<>(); resultsCount = 0; } // interpreta("{(5X*(15X/3X))+(25X/(5X*(15X/3X)))=15{X=5"); //TODO RIMUOVERE @@ -124,7 +141,7 @@ public class MathInputScreen extends Screen { ArrayList fncs = new ArrayList(); if (eqn.length() > 0) { try { - fncs.add(Calculator.parseString(eqn.replace("sqrt", "Ⓐ").replace("^", "Ⓑ"))); + fncs.add(calc.parseString(eqn.replace("sqrt", "Ⓐ").replace("^", "Ⓑ"))); } catch (Exception ex) { } @@ -249,29 +266,34 @@ public class MathInputScreen extends Screen { switch (k) { case SIMPLIFY: if (nuovaEquazione.length() > 0) { - try { + if (!afterDoNextStep) { try { - if (!afterDoNextStep) { - interpreta(false); - f2 = f; - afterDoNextStep = true; - Calculator.simplify(this); + try { + interpreta(true); + showVariablesDialog(new Runnable(){ + @Override + public void run() { + equazioneCorrente = nuovaEquazione; + f2 = f; + afterDoNextStep = true; + simplify(MathInputScreen.this); + } + }); + } catch (Exception ex) { + if (Utils.debugOn) + ex.printStackTrace(); + throw new Error(Errors.SYNTAX_ERROR); } - Calculator.simplify(this); - } catch (Exception ex) { - if (Utils.debugOn) { - ex.printStackTrace(); - } - throw new Error(Errors.SYNTAX_ERROR); + } catch (Error e) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + e.printStackTrace(pw); + d.errorStackTrace = sw.toString().toUpperCase().replace("\t", " ").replace("\r", "").split("\n"); + PIDisplay.error = e.id.toString(); + System.err.println(e.id); } - } catch (Error e) { - glClearColor(0xFFDC3C32); - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - e.printStackTrace(pw); - d.errorStackTrace = sw.toString().toUpperCase().replace("\t", " ").replace("\r", "").split("\n"); - PIDisplay.error = e.id.toString(); - System.err.println(e.id); + } else { + simplify(this); } } return true; @@ -288,12 +310,17 @@ public class MathInputScreen extends Screen { try { try { if (afterDoNextStep) { - Calculator.simplify(this); + simplify(this); } else { if (nuovaEquazione != equazioneCorrente && nuovaEquazione.length() > 0) { changeEquationScreen(); - interpreta(false); - Calculator.solve(this); + interpreta(true); + showVariablesDialog(new Runnable(){ + @Override + public void run() { + equazioneCorrente = nuovaEquazione; + solve(); + }}); } } } catch (Exception ex) { @@ -381,6 +408,9 @@ public class MathInputScreen extends Screen { case POWER_OF_x: typeChar(MathematicalSymbols.POWER); return true; + case PI: + typeChar(MathematicalSymbols.PI); + return true; case LETTER_X: typeChar(MathematicalSymbols.variables()[23]); return true; @@ -452,27 +482,12 @@ public class MathInputScreen extends Screen { return true; } case SURD_MODE: - Calculator.exactMode = !Calculator.exactMode; - try { - try { - if (Calculator.exactMode == false) { - f2 = Calculator.solveExpression(f2); - } else { - equazioneCorrente = ""; - Keyboard.keyPressed(Key.SOLVE); - } - } catch (Exception ex) { - if (Utils.debugOn) - ex.printStackTrace(); - throw new Error(Errors.SYNTAX_ERROR); - } - } catch (Error e) { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - e.printStackTrace(pw); - d.errorStackTrace = sw.toString().toUpperCase().replace("\t", " ").replace("\r", "").split("\n"); - PIDisplay.error = e.id.toString(); - System.err.println(e.id); + calc.exactMode = !calc.exactMode; + if (calc.exactMode == false) { + f2 = solveExpression(f2); + } else { + equazioneCorrente = ""; + Keyboard.keyPressed(Key.SOLVE); } return true; case debug1: @@ -480,7 +495,7 @@ public class MathInputScreen extends Screen { return true; case HISTORY_BACK: if (PIDisplay.INSTANCE.canGoBack()) { - if (equazioneCorrente != null && equazioneCorrente.length() > 0 & Calculator.sessions[Calculator.currentSession+1] instanceof MathInputScreen) { + if (equazioneCorrente != null && equazioneCorrente.length() > 0 & PIDisplay.sessions[PIDisplay.currentSession+1] instanceof MathInputScreen) { nuovaEquazione = equazioneCorrente; try { interpreta(true); @@ -491,7 +506,7 @@ public class MathInputScreen extends Screen { return false; case HISTORY_FORWARD: if (PIDisplay.INSTANCE.canGoForward()) { - if (equazioneCorrente != null && equazioneCorrente.length() > 0 & Calculator.sessions[Calculator.currentSession-1] instanceof MathInputScreen) { + if (equazioneCorrente != null && equazioneCorrente.length() > 0 & PIDisplay.sessions[PIDisplay.currentSession-1] instanceof MathInputScreen) { nuovaEquazione = equazioneCorrente; try { interpreta(true); @@ -500,11 +515,156 @@ public class MathInputScreen extends Screen { } } return false; + case debug_DEG: + if (calc.angleMode.equals(AngleMode.DEG) == false) { + calc.angleMode = AngleMode.DEG; + return true; + } + return false; + case debug_RAD: + if (calc.angleMode.equals(AngleMode.RAD) == false) { + calc.angleMode = AngleMode.RAD; + return true; + } + return false; + case debug_GRA: + if (calc.angleMode.equals(AngleMode.GRA) == false) { + calc.angleMode = AngleMode.GRA; + return true; + } + return false; + case DRG_CYCLE: + if (calc.angleMode.equals(AngleMode.DEG) == true) { + calc.angleMode = AngleMode.RAD; + } else if (calc.angleMode.equals(AngleMode.RAD) == true) { + calc.angleMode = AngleMode.GRA; + } else { + calc.angleMode = AngleMode.DEG; + } + return true; default: return false; } } + private ArrayList solveExpression(ArrayList f22) { + try { + try { + return calc.solveExpression(f22); + } catch (Exception ex) { + if (Utils.debugOn) { + ex.printStackTrace(); + } + throw new Error(Errors.SYNTAX_ERROR); + } + } catch (Error e) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + e.printStackTrace(pw); + d.errorStackTrace = sw.toString().toUpperCase().replace("\t", " ").replace("\r", "").split("\n"); + PIDisplay.error = e.id.toString(); + System.err.println(e.id); + } + return null; + } + + protected void simplify(MathInputScreen mathInputScreen) { + try { + try { + showVariablesDialog(); + ArrayList results = new ArrayList<>(); + ArrayList partialResults = new ArrayList<>(); + for (Function f : f2) { + if (f instanceof Equation) { + PIDisplay.INSTANCE.setScreen(new SolveEquationScreen(this)); + } else { + results.add(f); + 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(); + } + } + + if (results.size() == 0) { + resultsCount = 0; + } else { + resultsCount = results.size(); + Collections.reverse(results); + // add elements to al, including duplicates + Set hs = new LinkedHashSet<>(); + hs.addAll(results); + results.clear(); + results.addAll(hs); + f2 = results; + for (Function rf : f2) { + rf.generateGraphics(); + } + } + } catch (Exception ex) { + if (Utils.debugOn) { + ex.printStackTrace(); + } + throw new Error(Errors.SYNTAX_ERROR); + } + } catch (Error e) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + e.printStackTrace(pw); + d.errorStackTrace = sw.toString().toUpperCase().replace("\t", " ").replace("\r", "").split("\n"); + PIDisplay.error = e.id.toString(); + System.err.println(e.id); + } + } + + protected void solve() { + try { + try { + for (Function f : f) { + if (f instanceof Equation) { + PIDisplay.INSTANCE.setScreen(new SolveEquationScreen(this)); + return; + } + } + + ArrayList results = solveExpression(f); + if (results.size() == 0) { + resultsCount = 0; + } else { + resultsCount = results.size(); + Collections.reverse(results); + // add elements to al, including duplicates + Set hs = new LinkedHashSet<>(); + hs.addAll(results); + results.clear(); + results.addAll(hs); + f2 = results; + for (Function rf : f2) { + rf.generateGraphics(); + } + } + } catch (Exception ex) { + if (Utils.debugOn) { + ex.printStackTrace(); + } + throw new Error(Errors.SYNTAX_ERROR); + } + } catch (Error e) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + e.printStackTrace(pw); + d.errorStackTrace = sw.toString().toUpperCase().replace("\t", " ").replace("\r", "").split("\n"); + PIDisplay.error = e.id.toString(); + System.err.println(e.id); + } + } + private void changeEquationScreen() { if (equazioneCorrente != null && equazioneCorrente.length() > 0) { MathInputScreen cloned = clone(); @@ -536,10 +696,70 @@ public class MathInputScreen extends Screen { @Override public boolean keyReleased(Key k) { - return false; } + public void showVariablesDialog(final Runnable runnable) { + Thread ct = new Thread(()->{ + ArrayList variablesInFunctions = getVariables(f.toArray(new Function[f.size()])); + for (VariableValue f : variablesValues) { + if (variablesInFunctions.contains(f.v)) { + variablesInFunctions.remove(f.v); + } + } + + boolean cancelled = false; + for (Function f : variablesInFunctions) { + ChooseVariableValueScreen cvs = new ChooseVariableValueScreen(this, new VariableValue((Variable) f, new Number(null, 0))); + PIDisplay.INSTANCE.setScreen(cvs); + try { + while (PIDisplay.screen == cvs) { + Utils.debug.println(Thread.currentThread().getName()); + Thread.sleep(200); + } + } catch (InterruptedException e) {} + if (cvs.resultNumberValue == null) { + cancelled = true; + break; + } else { + final int is = variablesValues.size(); + for (int i = 0; i < is; i++) { + if (variablesValues.get(i).v == f) { + variablesValues.remove(i); + } + } + variablesValues.add(new VariableValue((Variable) f, (Number) cvs.resultNumberValue)); + } + } + if (!cancelled) { + runnable.run(); + Utils.debug.println(f.toString()); + } + }); + ct.setName("Variables user-input queue thread"); + ct.setPriority(Thread.MIN_PRIORITY); + ct.setDaemon(true); + ct.start(); + } + + private ArrayList getVariables(Function[] fncs) { + ArrayList res = new ArrayList<>(); + for (Function f : fncs) { + if (f instanceof FunctionTwoValues) { + res.addAll(getVariables(new Function[]{((FunctionTwoValues)f).getVariable1(), ((FunctionTwoValues)f).getVariable2()})); + } else if (f instanceof FunctionMultipleValues) { + res.addAll(getVariables(((FunctionMultipleValues)f).getVariables())); + } else if (f instanceof AnteriorFunction) { + res.addAll(getVariables(new Function[]{((AnteriorFunction)f).getVariable()})); + } else if (f instanceof Variable) { + if (!res.contains(f)) { + res.add(f); + } + } + } + return res; + } + @Override public MathInputScreen clone() { MathInputScreen es = this; @@ -557,6 +777,7 @@ public class MathInputScreen extends Screen { es2.errorLevel = es.errorLevel; es2.mustRefresh = es.mustRefresh; es2.afterDoNextStep = es.afterDoNextStep; + es2.variablesValues = Utils.cloner.deepClone(es.variablesValues); return es2; } From ceb1304675c400b9609407a5dcd38b33d8aeb2a1 Mon Sep 17 00:00:00 2001 From: XDrake99 Date: Tue, 17 Jan 2017 22:32:40 +0100 Subject: [PATCH 4/5] Continued fixing some classes --- src/com/rits/cloning/Cloner.java | 20 +-- src/org/nevec/rjm/Bernoulli.java | 2 +- src/org/nevec/rjm/BigIntegerPoly.java | 10 +- src/org/nevec/rjm/BigSurdVec.java | 8 +- src/org/nevec/rjm/Euler.java | 2 +- src/org/nevec/rjm/Factorial.java | 2 +- src/org/nevec/rjm/Ifactor.java | 8 +- src/org/nevec/rjm/PartitionsP.java | 2 +- src/org/nevec/rjm/Prime.java | 2 +- src/org/nevec/rjm/RatPoly.java | 16 +-- src/org/warp/picalculator/Main.java | 1 - src/org/warp/picalculator/Utils.java | 1 - .../warp/picalculator/device/Keyboard.java | 5 - .../warp/picalculator/device/PIDisplay.java | 5 +- .../device/graphicengine/Display.java | 2 +- .../device/graphicengine/RAWFont.java | 2 +- .../warp/picalculator/math/Calculator.java | 67 +++++++--- .../math/functions/AnteriorFunction.java | 35 +++--- .../picalculator/math/functions/Division.java | 14 +-- .../math/functions/EmptyNumber.java | 16 +-- .../math/functions/Expression.java | 101 ++++++++-------- .../picalculator/math/functions/Function.java | 5 +- .../functions/FunctionMultipleValues.java | 50 ++++---- .../math/functions/FunctionTwoValues.java | 29 ++--- .../picalculator/math/functions/Joke.java | 14 +-- .../math/functions/Multiplication.java | 9 +- .../picalculator/math/functions/Negative.java | 18 +-- .../picalculator/math/functions/Number.java | 49 ++++---- .../picalculator/math/functions/Power.java | 11 +- .../picalculator/math/functions/Root.java | 26 ++-- .../math/functions/RootSquare.java | 26 ++-- .../math/functions/Subtraction.java | 11 +- .../warp/picalculator/math/functions/Sum.java | 17 +-- .../math/functions/SumSubtraction.java | 11 +- .../math/functions/Undefined.java | 18 ++- .../picalculator/math/functions/Variable.java | 20 ++- .../math/functions/equations/Equation.java | 20 +-- .../functions/equations/EquationResult.java | 4 +- .../functions/equations/EquationsSystem.java | 19 +-- .../equations/EquationsSystemPart.java | 26 ++-- .../functions/trigonometry/ArcCosine.java | 9 +- .../math/functions/trigonometry/ArcSine.java | 9 +- .../functions/trigonometry/ArcTangent.java | 9 +- .../math/functions/trigonometry/Cosine.java | 9 +- .../math/functions/trigonometry/Sine.java | 22 ++-- .../math/functions/trigonometry/Tangent.java | 9 +- .../picalculator/math/rules/ExpandRule1.java | 26 ++-- .../picalculator/math/rules/ExpandRule5.java | 4 +- .../math/rules/ExponentRule1.java | 7 +- .../math/rules/ExponentRule15.java | 10 +- .../math/rules/ExponentRule2.java | 4 +- .../math/rules/ExponentRule3.java | 2 +- .../math/rules/FractionsRule1.java | 4 +- .../math/rules/FractionsRule2.java | 4 +- .../math/rules/FractionsRule4.java | 2 +- .../math/rules/FractionsRule5.java | 4 +- .../picalculator/math/rules/NumberRule1.java | 4 +- .../picalculator/math/rules/NumberRule2.java | 10 +- .../picalculator/math/rules/NumberRule3.java | 2 +- .../picalculator/math/rules/NumberRule5.java | 8 +- .../picalculator/math/rules/NumberRule6.java | 8 +- .../picalculator/math/rules/NumberRule7.java | 6 +- .../math/rules/UndefinedRule1.java | 4 +- .../math/rules/UndefinedRule2.java | 4 +- .../math/rules/methods/DivisionRule1.java | 19 ++- .../rules/methods/MultiplicationMethod1.java | 8 +- .../math/rules/methods/SumMethod1.java | 22 ++-- .../screens/ChooseVariableValueScreen.java | 4 - .../picalculator/screens/MathInputScreen.java | 114 ++++++++---------- .../screens/SolveEquationScreen.java | 4 +- 70 files changed, 525 insertions(+), 530 deletions(-) diff --git a/src/com/rits/cloning/Cloner.java b/src/com/rits/cloning/Cloner.java index b938f989..c20bc1f6 100644 --- a/src/com/rits/cloning/Cloner.java +++ b/src/com/rits/cloning/Cloner.java @@ -34,12 +34,12 @@ import java.util.regex.Pattern; */ public class Cloner { private final IInstantiationStrategy instantiationStrategy; - private final Set> ignored = new HashSet>(); - private final Set> ignoredInstanceOf = new HashSet>(); - private final Set> nullInstead = new HashSet>(); - private final Map, IFastCloner> fastCloners = new HashMap, IFastCloner>(); - private final Map ignoredInstances = new IdentityHashMap(); - private final ConcurrentHashMap, List> fieldsCache = new ConcurrentHashMap, List>(); + private final Set> ignored = new HashSet<>(); + private final Set> ignoredInstanceOf = new HashSet<>(); + private final Set> nullInstead = new HashSet<>(); + private final Map, IFastCloner> fastCloners = new HashMap<>(); + private final Map ignoredInstances = new IdentityHashMap<>(); + private final ConcurrentHashMap, List> fieldsCache = new ConcurrentHashMap<>(); public IDumpCloned getDumpCloned() { return dumpCloned; @@ -333,7 +333,7 @@ public class Cloner { if (dumpCloned != null) { dumpCloned.startCloning(o.getClass()); } - final Map clones = new IdentityHashMap(16); + final Map clones = new IdentityHashMap<>(16); try { return cloneInternal(o, clones); } catch (final IllegalAccessException e) { @@ -349,7 +349,7 @@ public class Cloner { if (dumpCloned != null) { dumpCloned.startCloning(o.getClass()); } - final Map clones = new IdentityHashMap(16); + final Map clones = new IdentityHashMap<>(16); for (final Object dc : dontCloneThese) { clones.put(dc, dc); } @@ -383,7 +383,7 @@ public class Cloner { } // caches immutables for quick reference - private final ConcurrentHashMap, Boolean> immutables = new ConcurrentHashMap, Boolean>(); + private final ConcurrentHashMap, Boolean> immutables = new ConcurrentHashMap<>(); private boolean cloneAnonymousParent = true; /** @@ -602,7 +602,7 @@ public class Cloner { protected List allFields(final Class c) { List l = fieldsCache.get(c); if (l == null) { - l = new LinkedList(); + l = new LinkedList<>(); final Field[] fields = c.getDeclaredFields(); addAll(l, fields); Class sc = c; diff --git a/src/org/nevec/rjm/Bernoulli.java b/src/org/nevec/rjm/Bernoulli.java index 8187efd1..d0de5b56 100644 --- a/src/org/nevec/rjm/Bernoulli.java +++ b/src/org/nevec/rjm/Bernoulli.java @@ -15,7 +15,7 @@ public class Bernoulli { /* * The list of all Bernoulli numbers as a vector, n=0,2,4,.... */ - static Vector a = new Vector(); + static Vector a = new Vector<>(); public Bernoulli() { if (a.size() == 0) { diff --git a/src/org/nevec/rjm/BigIntegerPoly.java b/src/org/nevec/rjm/BigIntegerPoly.java index 3d4a61d0..f5e754de 100644 --- a/src/org/nevec/rjm/BigIntegerPoly.java +++ b/src/org/nevec/rjm/BigIntegerPoly.java @@ -28,7 +28,7 @@ public class BigIntegerPoly implements Cloneable { * Creates the polynomial p(x)=0. */ public BigIntegerPoly() { - a = new Vector(); + a = new Vector<>(); } /** @@ -38,7 +38,7 @@ public class BigIntegerPoly implements Cloneable { * the string of the form a0,a1,a2,a3 with the coefficients */ public BigIntegerPoly(final String L) throws NumberFormatException { - a = new Vector(); + a = new Vector<>(); Scanner sc = new Scanner(L); sc.useDelimiter(","); while (sc.hasNextBigInteger()) @@ -537,7 +537,7 @@ public class BigIntegerPoly implements Cloneable { */ public Vector iroots() { /* The vector of the roots */ - Vector res = new Vector(); + Vector res = new Vector<>(); /* * collect the zero @@ -581,7 +581,7 @@ public class BigIntegerPoly implements Cloneable { /* * The vector of the factors to be returned */ - Vector res = new Vector(); + Vector res = new Vector<>(); if (degree() < 2) return res; @@ -679,7 +679,7 @@ public class BigIntegerPoly implements Cloneable { /* * this ought be entirely rewritten in terms of the LLL algorithm */ - Vector fac = new Vector(); + Vector fac = new Vector<>(); /* collect integer roots (polynomial factors of degree 1) */ Vector r = iroots(); diff --git a/src/org/nevec/rjm/BigSurdVec.java b/src/org/nevec/rjm/BigSurdVec.java index 2e82a92f..c12ef143 100644 --- a/src/org/nevec/rjm/BigSurdVec.java +++ b/src/org/nevec/rjm/BigSurdVec.java @@ -39,7 +39,7 @@ public class BigSurdVec implements Comparable { * @since 2012-02-15 */ public BigSurdVec() { - terms = new Vector(); + terms = new Vector<>(); } /* ctor */ /** @@ -50,7 +50,7 @@ public class BigSurdVec implements Comparable { * @since 2012-02-15 */ public BigSurdVec(BigSurd a) { - terms = new Vector(1); + terms = new Vector<>(1); terms.add(a); } /* ctor */ @@ -64,7 +64,7 @@ public class BigSurdVec implements Comparable { * @since 2012-02-15 */ public BigSurdVec(BigSurd a, BigSurd b) { - terms = new Vector(2); + terms = new Vector<>(2); terms.add(a); terms.add(b); try { @@ -90,7 +90,7 @@ public class BigSurdVec implements Comparable { if (terms.size() <= 1) return; - Vector newter = new Vector(); + Vector newter = new Vector<>(); newter.add(terms.firstElement()); /* * add j-th element to the existing vector and combine were possible diff --git a/src/org/nevec/rjm/Euler.java b/src/org/nevec/rjm/Euler.java index 93e7174e..c28c31c5 100644 --- a/src/org/nevec/rjm/Euler.java +++ b/src/org/nevec/rjm/Euler.java @@ -14,7 +14,7 @@ public class Euler { /* * The list of all Euler numbers as a vector, n=0,2,4,.... */ - static protected Vector a = new Vector(); + static protected Vector a = new Vector<>(); /** * Ctor(). Fill the hash list initially with E_0 to E_3. diff --git a/src/org/nevec/rjm/Factorial.java b/src/org/nevec/rjm/Factorial.java index bfb19788..77c5d6c8 100644 --- a/src/org/nevec/rjm/Factorial.java +++ b/src/org/nevec/rjm/Factorial.java @@ -14,7 +14,7 @@ public class Factorial { /** * The list of all factorials as a vector. */ - static Vector a = new Vector(); + static Vector a = new Vector<>(); /** * ctor(). diff --git a/src/org/nevec/rjm/Ifactor.java b/src/org/nevec/rjm/Ifactor.java index 98049b19..dc5e22d3 100644 --- a/src/org/nevec/rjm/Ifactor.java +++ b/src/org/nevec/rjm/Ifactor.java @@ -45,7 +45,7 @@ public class Ifactor implements Cloneable, Comparable { */ public Ifactor(int number) { n = new BigInteger("" + number); - primeexp = new Vector(); + primeexp = new Vector<>(); if (number > 1) { int primindx = 0; Prime primes = new Prime(); @@ -86,7 +86,7 @@ public class Ifactor implements Cloneable, Comparable { */ public Ifactor(BigInteger number) { n = number; - primeexp = new Vector(); + primeexp = new Vector<>(); if (number.compareTo(BigInteger.ONE) == 0) { primeexp.add(new Integer(1)); primeexp.add(new Integer(0)); @@ -125,7 +125,7 @@ public class Ifactor implements Cloneable, Comparable { * continuous prime-smooth basis. */ public Ifactor(Vector pows) { - primeexp = new Vector(2 * pows.size()); + primeexp = new Vector<>(2 * pows.size()); if (pows.size() > 0) { n = BigInteger.ONE; Prime primes = new Prime(); @@ -512,7 +512,7 @@ public class Ifactor implements Cloneable, Comparable { * multiplied * by 1 or by a product that contains the factors p1..py. */ - Vector d = new Vector(); + Vector d = new Vector<>(); if (n.compareTo(BigInteger.ZERO) == 0) return d; d.add(BigInteger.ONE); diff --git a/src/org/nevec/rjm/PartitionsP.java b/src/org/nevec/rjm/PartitionsP.java index 07e48902..163f169f 100644 --- a/src/org/nevec/rjm/PartitionsP.java +++ b/src/org/nevec/rjm/PartitionsP.java @@ -13,7 +13,7 @@ public class PartitionsP { /** * The list of all partitions as a vector. */ - static protected Vector a = new Vector(); + static protected Vector a = new Vector<>(); /** * The maximum integer covered by the high end of the list. diff --git a/src/org/nevec/rjm/Prime.java b/src/org/nevec/rjm/Prime.java index a5197102..cce2e510 100644 --- a/src/org/nevec/rjm/Prime.java +++ b/src/org/nevec/rjm/Prime.java @@ -21,7 +21,7 @@ public class Prime { /** * The list of all numbers as a vector. */ - static Vector a = new Vector(); + static Vector a = new Vector<>(); /** * The maximum integer covered by the high end of the list. diff --git a/src/org/nevec/rjm/RatPoly.java b/src/org/nevec/rjm/RatPoly.java index 60f77d3b..668b0305 100644 --- a/src/org/nevec/rjm/RatPoly.java +++ b/src/org/nevec/rjm/RatPoly.java @@ -31,7 +31,7 @@ class RatPoly { * Initializes the zero-valued polynomial x=0. */ public RatPoly() { - a = new Vector(); + a = new Vector<>(); } /* ctor */ /** @@ -42,7 +42,7 @@ class RatPoly { * is created. */ public RatPoly(final Vector L) { - a = new Vector(); + a = new Vector<>(); for (int i = 0; i < L.size(); i++) a.add(L.elementAt(i).clone()); simplify(); @@ -55,7 +55,7 @@ class RatPoly { * the string of the form a0,a1,a2,a3 with the coefficients */ public RatPoly(final String L) throws NumberFormatException { - a = new Vector(); + a = new Vector<>(); Scanner sc = new Scanner(L); sc.useDelimiter(","); while (sc.hasNext()) { @@ -129,7 +129,7 @@ class RatPoly { * @since 2008-11-13 */ protected void init(final Vector A, final Vector B, int nmax) throws Error { - a = new Vector(); + a = new Vector<>(); Factorial f = new Factorial(); for (int n = 0; n <= nmax; n++) { Rational c = new Rational(1, 1); @@ -888,7 +888,7 @@ class RatPoly { Random rand = new Random(); MathContext mc = new MathContext(digits + 3, RoundingMode.DOWN); - Vector res = new Vector(); + Vector res = new Vector<>(); final int d = mon.degree(); double randRad = 0.; @@ -918,7 +918,7 @@ class RatPoly { for (; !convr;)// ORIGINAL LINE: for(int itr =0 ; ! convr ; itr++) { convr = true; - Vector resPlus = new Vector(); + Vector resPlus = new Vector<>(); for (int v = 0; v < d; v++) { /* * evaluate f(x)/(x-root1)/(x-root2)/... (x-rootdegr), Newton @@ -959,7 +959,7 @@ class RatPoly { */ public Vector iroots() { /* The vector of the roots */ - Vector res = new Vector(); + Vector res = new Vector<>(); int lowd = ldegree(); if (lowd == 0 && a.elementAt(0).compareTo(BigInteger.ZERO) == 0) { @@ -984,7 +984,7 @@ class RatPoly { * and eventually get the integer polynomial by ignoring the * denominators */ - Vector ipo = new Vector(); + Vector ipo = new Vector<>(); for (int i = 0; i < a.size(); i++) { BigInteger d = a.elementAt(i).a.multiply(lcmDeno).divide(a.elementAt(i).b); ipo.add(d); diff --git a/src/org/warp/picalculator/Main.java b/src/org/warp/picalculator/Main.java index ccc8c744..18dc6dee 100644 --- a/src/org/warp/picalculator/Main.java +++ b/src/org/warp/picalculator/Main.java @@ -2,7 +2,6 @@ package org.warp.picalculator; import org.warp.picalculator.device.Keyboard; import org.warp.picalculator.device.PIDisplay; -import org.warp.picalculator.screens.KeyboardDebugScreen; import org.warp.picalculator.screens.LoadingScreen; import com.pi4j.wiringpi.Gpio; diff --git a/src/org/warp/picalculator/Utils.java b/src/org/warp/picalculator/Utils.java index 4ddce5af..58892462 100644 --- a/src/org/warp/picalculator/Utils.java +++ b/src/org/warp/picalculator/Utils.java @@ -10,7 +10,6 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; import java.util.ArrayList; -import java.util.Collection; import java.util.List; import org.nevec.rjm.BigDecimalMath; diff --git a/src/org/warp/picalculator/device/Keyboard.java b/src/org/warp/picalculator/device/Keyboard.java index 1d95c91c..417cf288 100644 --- a/src/org/warp/picalculator/device/Keyboard.java +++ b/src/org/warp/picalculator/device/Keyboard.java @@ -1,16 +1,11 @@ package org.warp.picalculator.device; import java.awt.event.KeyEvent; -import java.util.Arrays; - import org.warp.picalculator.Utils; -import org.warp.picalculator.device.Keyboard.Key; import org.warp.picalculator.device.chip.ParallelToSerial; import org.warp.picalculator.device.chip.SerialToParallel; import org.warp.picalculator.device.graphicengine.Display; import org.warp.picalculator.device.graphicengine.Screen; -import org.warp.picalculator.math.AngleMode; -import org.warp.picalculator.math.Calculator; import org.warp.picalculator.screens.KeyboardDebugScreen; import org.warp.picalculator.screens.MarioScreen; diff --git a/src/org/warp/picalculator/device/PIDisplay.java b/src/org/warp/picalculator/device/PIDisplay.java index a9fe724a..dd2068cb 100644 --- a/src/org/warp/picalculator/device/PIDisplay.java +++ b/src/org/warp/picalculator/device/PIDisplay.java @@ -23,9 +23,6 @@ import org.warp.picalculator.Utils; import org.warp.picalculator.device.graphicengine.Display; import org.warp.picalculator.device.graphicengine.RAWFont; import org.warp.picalculator.device.graphicengine.Screen; -import org.warp.picalculator.math.AngleMode; -import org.warp.picalculator.math.Calculator; - import com.pi4j.wiringpi.Gpio; /** @@ -322,7 +319,7 @@ public final class PIDisplay { if (error != null) { glSetFont(Utils.getFont(false, false)); glColor3i(129, 28, 22); - glDrawStringRight(Main.screenSize[0] - 2, Main.screenSize[1]- this.glyphsHeight[1] - 2, "ANDREA CAVALLI'S CALCULATOR"); + glDrawStringRight(Main.screenSize[0] - 2, Main.screenSize[1]- PIDisplay.glyphsHeight[1] - 2, "ANDREA CAVALLI'S CALCULATOR"); glColor3i(149, 32, 26); glDrawStringCenter((Main.screenSize[0] / 2), 22, error); glColor3i(164, 34, 28); diff --git a/src/org/warp/picalculator/device/graphicengine/Display.java b/src/org/warp/picalculator/device/graphicengine/Display.java index 87f081ac..59a943d4 100644 --- a/src/org/warp/picalculator/device/graphicengine/Display.java +++ b/src/org/warp/picalculator/device/graphicengine/Display.java @@ -179,7 +179,7 @@ public class Display { newColor = skin[(s0 + texx) + (t0 + texy) * skinwidth]; if (transparent) { oldColor = canvas2d[(x0 + texx*onex + width) + (y0 + texy*oney + height) * size[0]]; - float a2 = ((float)(newColor >> 24 & 0xFF)) / 255f; + float a2 = (newColor >> 24 & 0xFF) / 255f; float a1 = 1f-a2; int r = (int) ((oldColor >> 16 & 0xFF) * a1 + (newColor >> 16 & 0xFF) * a2); int g = (int) ((oldColor >> 8 & 0xFF) * a1 + (newColor >> 8 & 0xFF) * a2); diff --git a/src/org/warp/picalculator/device/graphicengine/RAWFont.java b/src/org/warp/picalculator/device/graphicengine/RAWFont.java index f2b9a7c7..3e1b988f 100644 --- a/src/org/warp/picalculator/device/graphicengine/RAWFont.java +++ b/src/org/warp/picalculator/device/graphicengine/RAWFont.java @@ -166,7 +166,7 @@ public class RAWFont { j = x + cpos + dx; if (j > 0 & j < screenSize[0]) { int bit = dx + dy * charW; - currentInt = (int) (Math.floor((double)bit)/((double)intBits)); + currentInt = (int) (Math.floor(bit)/(intBits)); currentIntBitPosition = bit-(currentInt*intBits); bitData = (chars32[charIndex*charIntCount+currentInt] >> currentIntBitPosition) & 1; screenPos = x + cpos + dx + (y + dy) * screenSize[0]; diff --git a/src/org/warp/picalculator/math/Calculator.java b/src/org/warp/picalculator/math/Calculator.java index 9ba1eb49..316a14e1 100644 --- a/src/org/warp/picalculator/math/Calculator.java +++ b/src/org/warp/picalculator/math/Calculator.java @@ -2,30 +2,31 @@ package org.warp.picalculator.math; import java.math.BigInteger; import java.util.ArrayList; -import java.util.Collections; -import java.util.LinkedHashSet; import java.util.List; -import java.util.Set; - 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.math.functions.Expression; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.Number; -import org.warp.picalculator.math.functions.Variable; +import org.warp.picalculator.math.functions.Variable.VariableValue; import org.warp.picalculator.math.functions.equations.Equation; import org.warp.picalculator.math.functions.equations.EquationsSystem; -import org.warp.picalculator.screens.MathInputScreen; -import org.warp.picalculator.screens.SolveEquationScreen; public class Calculator { public AngleMode angleMode = AngleMode.DEG; public boolean exactMode = false; + public ArrayList f; + public ArrayList f2; + public ArrayList variablesValues; + public int resultsCount; public Calculator() { + f = new ArrayList<>(); + f2 = new ArrayList<>(); + variablesValues = new ArrayList<>(); + resultsCount = 0; } public Function parseString(String string) throws Error { @@ -34,7 +35,7 @@ public class Calculator { throw new Error(Errors.SYNTAX_ERROR); } String[] parts = string.substring(1).split("\\{"); - EquationsSystem s = new EquationsSystem(null); + EquationsSystem s = new EquationsSystem(this); for (String part : parts) { s.addFunctionToEnd(parseEquationString(part)); } @@ -42,21 +43,21 @@ public class Calculator { } else if (string.contains("=")) { return parseEquationString(string); } else { - return new Expression(null, string); + return new Expression(this, string); } } public Function parseEquationString(String string) throws Error { String[] parts = string.split("="); if (parts.length == 1) { - Equation e = new Equation(null, null, null); - e.setVariable1(new Expression(e, parts[0])); - e.setVariable2(new Number(e, BigInteger.ZERO)); + Equation e = new Equation(this, null, null); + e.setVariable1(new Expression(this, parts[0])); + e.setVariable2(new Number(this, BigInteger.ZERO)); return e; } else if (parts.length == 2) { - Equation e = new Equation(null, null, null); - e.setVariable1(new Expression(e, parts[0])); - e.setVariable2(new Expression(e, parts[1])); + Equation e = new Equation(this, null, null); + e.setVariable1(new Expression(this, parts[0])); + e.setVariable2(new Expression(this, parts[1])); return e; } else { throw new Error(Errors.SYNTAX_ERROR); @@ -85,13 +86,45 @@ public class Calculator { partialResults.add(itm); } } - results = new ArrayList(partialResults); + results = new ArrayList<>(partialResults); partialResults.clear(); } } } return results; } + + public Function getChild() { + return f.get(0); + } + + public void init() { + if (f == null & f2 == null) { + f = new ArrayList<>(); + f2 = new ArrayList<>(); + variablesValues = new ArrayList<>(); + resultsCount = 0; + } + } + + public void parseInputString(String eqn) throws Error { + ArrayList fncs = new ArrayList<>(); + if (eqn.length() > 0) { + try { + fncs.add(parseString(eqn.replace("sqrt", "Ⓐ").replace("^", "Ⓑ"))); + } catch (Exception ex) { + + } + } + f = fncs; + for (Function f : f) { + try { + f.generateGraphics(); + } catch (NullPointerException ex) { + throw new Error(Errors.SYNTAX_ERROR); + } + } + } /*public void solve(EquationScreen equationScreen, char letter) throws Error { if (Calculator.currentSession == 0 && Calculator.sessions[0] instanceof EquationScreen) { diff --git a/src/org/warp/picalculator/math/functions/AnteriorFunction.java b/src/org/warp/picalculator/math/functions/AnteriorFunction.java index 2ac636f3..3eeb0d6e 100644 --- a/src/org/warp/picalculator/math/functions/AnteriorFunction.java +++ b/src/org/warp/picalculator/math/functions/AnteriorFunction.java @@ -3,25 +3,30 @@ package org.warp.picalculator.math.functions; import static org.warp.picalculator.device.graphicengine.Display.Render.glDrawStringLeft; import static org.warp.picalculator.device.graphicengine.Display.Render.glGetStringWidth; -import java.math.BigInteger; import java.util.ArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.Utils; import org.warp.picalculator.device.graphicengine.Display; +import org.warp.picalculator.math.Calculator; import com.rits.cloning.Cloner; public abstract class AnteriorFunction implements Function { - public AnteriorFunction(Function parent, Function value) { - setParent(parent); - setVariable(value); + public AnteriorFunction(Function value) { + this.root = value.getRoot(); + variable = value; } - - protected abstract Function NewInstance(Function parent2, Function f); - protected Function parent; - protected Function variable = new Number(null, BigInteger.ZERO); + public AnteriorFunction(Calculator root, Function value) { + this.root = root; + variable = value; + } + + protected abstract Function NewInstance(Calculator root, Function value); + + protected final Calculator root; + protected Function variable; protected int width; protected int height; protected int line; @@ -35,13 +40,9 @@ public abstract class AnteriorFunction implements Function { variable = value; } - public Function getParent() { - return parent; - } - - public Function setParent(Function value) { - parent = value; - return this; + @Override + public Calculator getRoot() { + return root; } @Override @@ -55,7 +56,7 @@ public abstract class AnteriorFunction implements Function { if (result == null || result.isEmpty()) { result = new ArrayList<>(); - ArrayList l1 = new ArrayList(); + ArrayList l1 = new ArrayList<>(); if (variable.isSolved()) { l1.add(variable); } else { @@ -63,7 +64,7 @@ public abstract class AnteriorFunction implements Function { } for (Function f : l1) { - result.add(NewInstance(this.parent, (Function)f)); + result.add(NewInstance(this.root, f)); } } diff --git a/src/org/warp/picalculator/math/functions/Division.java b/src/org/warp/picalculator/math/functions/Division.java index 5bb4895a..1c28b767 100644 --- a/src/org/warp/picalculator/math/functions/Division.java +++ b/src/org/warp/picalculator/math/functions/Division.java @@ -18,13 +18,13 @@ import org.warp.picalculator.math.rules.UndefinedRule2; public class Division extends FunctionTwoValues { - public Division(Function parent, Function value1, Function value2) { - super(parent, value1, value2); + public Division(Calculator root, Function value1, Function value2) { + super(root, value1, value2); } @Override - protected Function NewInstance(Function parent2, Function value1, Function value2) { - return new Division(parent, value1, value2); + protected Function NewInstance(Calculator root, Function value1, Function value2) { + return new Division(root, value1, value2); } @Override @@ -39,7 +39,7 @@ public class Division extends FunctionTwoValues { if (FractionsRule2.compare(this)) return true; if (FractionsRule3.compare(this)) return true; if (UndefinedRule2.compare(this)) return true; - if (variable1 instanceof Number && variable2 instanceof Number && Calculator.exactMode == false) { + if (variable1 instanceof Number && variable2 instanceof Number && root.exactMode == false) { return true; } return false; @@ -56,8 +56,8 @@ public class Division extends FunctionTwoValues { result = FractionsRule3.execute(this); } else if (UndefinedRule2.compare(this)) { result = UndefinedRule2.execute(this); - } else if (variable1 instanceof Number && variable2 instanceof Number && Calculator.exactMode == false) { - result.add(((Number)variable1).divide((Number)variable2).setParent(parent)); + } else if (variable1 instanceof Number && variable2 instanceof Number && root.exactMode == false) { + result.add(((Number)variable1).divide((Number)variable2)); } return result; } diff --git a/src/org/warp/picalculator/math/functions/EmptyNumber.java b/src/org/warp/picalculator/math/functions/EmptyNumber.java index b9641479..22d9cf1c 100644 --- a/src/org/warp/picalculator/math/functions/EmptyNumber.java +++ b/src/org/warp/picalculator/math/functions/EmptyNumber.java @@ -5,10 +5,15 @@ import java.util.List; import org.warp.picalculator.Error; import org.warp.picalculator.Utils; import org.warp.picalculator.device.graphicengine.Display; +import org.warp.picalculator.math.Calculator; public class EmptyNumber implements Function { - public static EmptyNumber emptyNumber = new EmptyNumber(); + public EmptyNumber(Calculator root) { + this.root = root; + } + + private final Calculator root; @Override public String getSymbol() { @@ -52,13 +57,8 @@ public class EmptyNumber implements Function { } @Override - public Function setParent(Function parent) { - return null; - } - - @Override - public Function getParent() { - return null; + public Calculator getRoot() { + return root; } private boolean small = false; diff --git a/src/org/warp/picalculator/math/functions/Expression.java b/src/org/warp/picalculator/math/functions/Expression.java index d8ac1b83..687ed9f3 100644 --- a/src/org/warp/picalculator/math/functions/Expression.java +++ b/src/org/warp/picalculator/math/functions/Expression.java @@ -13,6 +13,7 @@ import java.util.regex.Pattern; import org.warp.picalculator.Error; import org.warp.picalculator.Errors; import org.warp.picalculator.Utils; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.MathematicalSymbols; import org.warp.picalculator.math.functions.trigonometry.ArcCosine; import org.warp.picalculator.math.functions.trigonometry.ArcSine; @@ -23,30 +24,29 @@ import org.warp.picalculator.math.functions.trigonometry.Tangent; public class Expression extends FunctionMultipleValues { - public Expression(Function parent) { - super(parent); + public Expression(Calculator root) { + super(root); } - public Expression(Function parent, Function[] values) { - super(parent, values); + public Expression(Calculator root, Function[] values) { + super(root, values); } private boolean initialParenthesis = false; - public Expression(Function parent, String string) throws Error { - this(parent, string, "", true); + public Expression(Calculator root, String string) throws Error { + this(root, string, "", true); } - public Expression(Function parent, String string, String debugSpaces, boolean initialParenthesis) throws Error { - super(parent); - this.parent = parent; + public Expression(Calculator root, String string, String debugSpaces, boolean initialParenthesis) throws Error { + super(root); this.initialParenthesis = initialParenthesis; boolean isNumber = false; // Determine if the expression is already a number: // Determina se l'espressione è già un numero: try { - new Number(this, string); + new Number(root, string); isNumber = true; } catch (NumberFormatException ex) { isNumber = false; @@ -60,7 +60,7 @@ public class Expression extends FunctionMultipleValues { if (isNumber){ // If the expression is already a number: // Se l'espressione è già un numero: - Number t = new Number(this, string); + Number t = new Number(root, string); setVariables(new Function[] { t }); Utils.debug.println(debugSpaces + "•Result:" + t.toString()); } else { @@ -211,7 +211,7 @@ public class Expression extends FunctionMultipleValues { debugSpaces += " "; // Convert the expression to a list of objects - Expression imputRawParenthesis = new Expression(this); + Expression imputRawParenthesis = new Expression(root); imputRawParenthesis.setVariables(new Function[] {}); String tmp = ""; final String[] functions = concat(concat(concat(concat(MathematicalSymbols.functions(), MathematicalSymbols.parentheses()), MathematicalSymbols.signums(true)), MathematicalSymbols.variables()), MathematicalSymbols.genericSyntax()); @@ -225,49 +225,49 @@ public class Expression extends FunctionMultipleValues { Function f = null; switch (charI) { case MathematicalSymbols.SUM: - f = new Sum(this, null, null); + f = new Sum(root, null, null); break; case MathematicalSymbols.SUM_SUBTRACTION: - f = new SumSubtraction(this, null, null); + f = new SumSubtraction(root, null, null); break; case MathematicalSymbols.SUBTRACTION: - f = new Subtraction(this, null, null); + f = new Subtraction(root, null, null); break; case MathematicalSymbols.MINUS: - f = new Negative(this, null); + f = new Negative(root, null); break; case MathematicalSymbols.MULTIPLICATION: - f = new Multiplication(this, null, null); + f = new Multiplication(root, null, null); break; case MathematicalSymbols.DIVISION: - f = new Division(this, null, null); + f = new Division(root, null, null); break; case MathematicalSymbols.NTH_ROOT: - f = new Root(this, null, null); + f = new Root(root, null, null); break; case MathematicalSymbols.SQUARE_ROOT: - f = new RootSquare(this, null); + f = new RootSquare(root, null); break; case MathematicalSymbols.POWER: - f = new Power(this, null, null); + f = new Power(root, null, null); break; case MathematicalSymbols.SINE: - f = new Sine(this, null); + f = new Sine(root, null); break; case MathematicalSymbols.COSINE: - f = new Cosine(this, null); + f = new Cosine(root, null); break; case MathematicalSymbols.TANGENT: - f = new Tangent(this, null); + f = new Tangent(root, null); break; case MathematicalSymbols.ARC_SINE: - f = new ArcSine(this, null); + f = new ArcSine(root, null); break; case MathematicalSymbols.ARC_COSINE: - f = new ArcCosine(this, null); + f = new ArcCosine(root, null); break; case MathematicalSymbols.ARC_TANGENT: - f = new ArcTangent(this, null); + f = new ArcTangent(root, null); break; case MathematicalSymbols.PARENTHESIS_OPEN: // Find the last closed parenthesis @@ -300,15 +300,16 @@ public class Expression extends FunctionMultipleValues { tmpExpr += processExpression.charAt(i); i++; } - f = new Expression(this, tmpExpr, debugSpaces, false); + f = new Expression(root, tmpExpr, debugSpaces, false); break; default: if (Utils.isInArray(charI, MathematicalSymbols.variables())) { - f = new Variable(this, charI); + f = new Variable(root, charI); } else { if (charI == "(" || charI == ")") { throw new Error(Errors.UNBALANCED_BRACKETS); } else { + System.err.println("Unexpected character while parsing expression: " + charI); throw new Error(Errors.SYNTAX_ERROR); } // throw new java.lang.RuntimeException("Il carattere " + charI + " non è tra le funzioni designate!\nAggiungerlo ad esse o rimuovere il carattere dall'espressione!"); @@ -319,28 +320,28 @@ public class Expression extends FunctionMultipleValues { } else if (f instanceof Variable) { if (imputRawParenthesis.getVariablesLength() == 0) { if (tmp.length() > 0) { - imputRawParenthesis.addFunctionToEnd(new Number(this, tmp)); + imputRawParenthesis.addFunctionToEnd(new Number(root, tmp)); Utils.debug.println(debugSpaces + "•Added number to expression:" + tmp); - imputRawParenthesis.addFunctionToEnd(new Multiplication(this, null, null)); - Utils.debug.println(debugSpaces + "•Added multiplication to expression:" + new Multiplication(this, null, null).getSymbol()); + imputRawParenthesis.addFunctionToEnd(new Multiplication(root, null, null)); + Utils.debug.println(debugSpaces + "•Added multiplication to expression:" + new Multiplication(root, null, null).getSymbol()); } } else { Function precedentFunction = imputRawParenthesis.getVariable(imputRawParenthesis.getVariablesLength() - 1); if (tmp.length() > 0) { if (precedentFunction instanceof Number || precedentFunction instanceof Variable) { - imputRawParenthesis.addFunctionToEnd(new Multiplication(this, null, null)); - Utils.debug.println(debugSpaces + "•Added multiplication to expression:" + new Multiplication(this, null, null).getSymbol()); + imputRawParenthesis.addFunctionToEnd(new Multiplication(root, null, null)); + Utils.debug.println(debugSpaces + "•Added multiplication to expression:" + new Multiplication(root, null, null).getSymbol()); } if (tmp.equals("-")) { - imputRawParenthesis.addFunctionToEnd(new Subtraction(this, null, null)); + imputRawParenthesis.addFunctionToEnd(new Subtraction(root, null, null)); } else { - imputRawParenthesis.addFunctionToEnd(new Number(this, tmp)); + imputRawParenthesis.addFunctionToEnd(new Number(root, tmp)); Utils.debug.println(debugSpaces + "•Added number to expression:" + tmp); } } if (tmp.length() > 0 || (precedentFunction instanceof Number || precedentFunction instanceof Variable)) { - imputRawParenthesis.addFunctionToEnd(new Multiplication(this, null, null)); - Utils.debug.println(debugSpaces + "•Added multiplication to expression:" + new Multiplication(this, null, null).getSymbol()); + imputRawParenthesis.addFunctionToEnd(new Multiplication(root, null, null)); + Utils.debug.println(debugSpaces + "•Added multiplication to expression:" + new Multiplication(root, null, null).getSymbol()); } } } else { @@ -350,7 +351,7 @@ public class Expression extends FunctionMultipleValues { tmp = "-1"; } } - imputRawParenthesis.addFunctionToEnd(new Number(this, tmp)); + imputRawParenthesis.addFunctionToEnd(new Number(root, tmp)); Utils.debug.println(debugSpaces + "•Added number to expression:" + tmp); } } @@ -373,7 +374,7 @@ public class Expression extends FunctionMultipleValues { if (tmp.length() > 0) { Utils.debug.println(debugSpaces + "•Added variable to expression:" + tmp); try { - imputRawParenthesis.addFunctionToEnd(new Number(this, tmp)); + imputRawParenthesis.addFunctionToEnd(new Number(root, tmp)); } catch (NumberFormatException ex) { throw new Error(Errors.SYNTAX_ERROR); } @@ -406,7 +407,7 @@ public class Expression extends FunctionMultipleValues { Utils.debug.println(debugSpaces + "•Pushing classes..."); Function[] oldFunctionsArray = imputRawParenthesis.getVariables(); - ArrayList oldFunctionsList = new ArrayList(); + ArrayList oldFunctionsList = new ArrayList<>(); for (int i = 0; i < oldFunctionsArray.length; i++) { Function funzione = oldFunctionsArray[i]; if (funzione != null) { @@ -417,7 +418,7 @@ public class Expression extends FunctionMultipleValues { oldFunctionsArray[i-1] = null; oldFunctionsList.remove(oldFunctionsList.size()-1); i -= 1; - funzione = new RootSquare(this, null); + funzione = new RootSquare(root, null); } } //Aggiunta della funzione alla lista grezza @@ -448,7 +449,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(this, oldFunctionsList.get(0), oldFunctionsList.remove(1))); + oldFunctionsList.set(0, new Multiplication(root, oldFunctionsList.get(0), oldFunctionsList.remove(1))); } } Utils.debug.println(debugSpaces + " •Phase: "+step); @@ -512,8 +513,8 @@ public class Expression extends FunctionMultipleValues { change = true; if (i + 1 < oldFunctionsList.size() && i - 1 >= 0) { - ((FunctionTwoValues) funzioneTMP).setVariable1((Function) oldFunctionsList.get(i - 1)); - ((FunctionTwoValues) funzioneTMP).setVariable2((Function) oldFunctionsList.get(i + 1)); + ((FunctionTwoValues) funzioneTMP).setVariable1(oldFunctionsList.get(i - 1)); + ((FunctionTwoValues) funzioneTMP).setVariable2(oldFunctionsList.get(i + 1)); oldFunctionsList.set(i, funzioneTMP); // è importante togliere prima gli elementi @@ -546,7 +547,7 @@ public class Expression extends FunctionMultipleValues { } else { change = true; - ((AnteriorFunction) funzioneTMP).setVariable((Function) nextFunc); + ((AnteriorFunction) funzioneTMP).setVariable(nextFunc); oldFunctionsList.set(i, funzioneTMP); // è importante togliere prima gli elementi in @@ -580,7 +581,7 @@ public class Expression extends FunctionMultipleValues { } while (((oldFunctionsList.size() != before || step != "sums") && oldFunctionsList.size() > 1)); } if(oldFunctionsList.isEmpty()) { - setVariables(new Function[]{new Number(this, 0)}); + setVariables(new Function[]{new Number(root, 0)}); } else { setVariables(oldFunctionsList); } @@ -630,7 +631,7 @@ public class Expression extends FunctionMultipleValues { if (f instanceof Number || f instanceof Variable) { ret.add(f); } else { - ret.add(new Expression(this, new Function[]{(Function) f})); + ret.add(new Expression(root, new Function[]{f})); } } return ret; @@ -640,7 +641,7 @@ public class Expression extends FunctionMultipleValues { if (f.isSolved() == false) { List partial = f.solveOneStep(); for (Function fnc : partial) { - ret.add(new Expression(this, new Function[]{(Function) fnc})); + ret.add(new Expression(root, new Function[]{fnc})); } } } @@ -662,7 +663,7 @@ public class Expression extends FunctionMultipleValues { public boolean parenthesisNeeded() { boolean parenthesisneeded = true; - if (parent == null || initialParenthesis || parent instanceof Division) { + if (initialParenthesis) { parenthesisneeded = false; } else { if (functions.length == 1) { diff --git a/src/org/warp/picalculator/math/functions/Function.java b/src/org/warp/picalculator/math/functions/Function.java index e7e0a9ba..eddf83df 100644 --- a/src/org/warp/picalculator/math/functions/Function.java +++ b/src/org/warp/picalculator/math/functions/Function.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.functions; import java.util.List; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; public interface Function { public String getSymbol(); @@ -21,9 +22,7 @@ public interface Function { public int getLine(); - public Function setParent(Function parent); - - public Function getParent(); + public Calculator getRoot(); public void setSmall(boolean small); diff --git a/src/org/warp/picalculator/math/functions/FunctionMultipleValues.java b/src/org/warp/picalculator/math/functions/FunctionMultipleValues.java index 2c97e02c..60dcbe5f 100644 --- a/src/org/warp/picalculator/math/functions/FunctionMultipleValues.java +++ b/src/org/warp/picalculator/math/functions/FunctionMultipleValues.java @@ -3,20 +3,31 @@ package org.warp.picalculator.math.functions; import java.util.Arrays; import java.util.List; +import org.warp.picalculator.math.Calculator; + import com.rits.cloning.Cloner; public abstract class FunctionMultipleValues implements Function { - public FunctionMultipleValues(Function parent) { - setParent(parent); - setVariables(new Function[] {}); + public FunctionMultipleValues(Calculator root) { + this.root = root; + functions = new Function[] {}; } - public FunctionMultipleValues(Function parent, Function[] values) { - setParent(parent); - setVariables(values); + public FunctionMultipleValues(Function[] values) { + if (values.length > 0) { + this.root = values[0].getRoot(); + } else { + throw new NullPointerException("Nessun elemento nell'array. Impossibile ricavare il nodo root"); + } + functions = values; } - protected Function parent; + public FunctionMultipleValues(Calculator root, Function[] values) { + this.root = root; + functions = values; + } + + protected final Calculator root; protected Function[] functions; protected int width; protected int height; @@ -27,34 +38,28 @@ public abstract class FunctionMultipleValues implements Function { return functions; } - public void setVariables(Function[] value) { - for (Function f : value) { - f.setParent(this); - } - functions = value; - } - public void setVariables(final List value) { int vsize = value.size(); Function[] tmp = new Function[vsize]; for (int i = 0; i < vsize; i++) { tmp[i] = value.get(i); - tmp[i].setParent(this); } functions = tmp; } + public void setVariables(final Function[] value) { + functions = value; + } + public Function getVariable(int index) { return functions[index]; } public void setVariable(int index, Function value) { - value.setParent(this); functions[index] = value; } public void addFunctionToEnd(Function value) { - value.setParent(this); int index = functions.length; setVariablesLength(index + 1); functions[index] = value; @@ -83,14 +88,9 @@ public abstract class FunctionMultipleValues implements Function { protected abstract boolean isSolvable(); - - public Function setParent(Function value) { - parent = value; - return this; - } - - public Function getParent() { - return parent; + @Override + public Calculator getRoot() { + return root; } @Override diff --git a/src/org/warp/picalculator/math/functions/FunctionTwoValues.java b/src/org/warp/picalculator/math/functions/FunctionTwoValues.java index 4dc28b91..8074c037 100644 --- a/src/org/warp/picalculator/math/functions/FunctionTwoValues.java +++ b/src/org/warp/picalculator/math/functions/FunctionTwoValues.java @@ -8,19 +8,20 @@ import java.util.ArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.Utils; import org.warp.picalculator.device.graphicengine.Display; +import org.warp.picalculator.math.Calculator; import com.rits.cloning.Cloner; public abstract class FunctionTwoValues implements Function { - public FunctionTwoValues(Function parent, Function value1, Function value2) { - this.parent = parent; + public FunctionTwoValues(Calculator root, Function value1, Function value2) { + this.root = root; variable1 = value1; variable2 = value2; } - protected abstract Function NewInstance(Function parent2, Function value1, Function value2); + protected abstract Function NewInstance(Calculator root, Function value1, Function value2); - protected Function parent; + protected final Calculator root; protected Function variable1 = null; protected Function variable2 = null; @@ -34,25 +35,19 @@ public abstract class FunctionTwoValues implements Function { } public void setVariable1(Function value) { - value.setParent(this); variable1 = value; } - public Function getParent() { - return parent; + @Override + public Calculator getRoot() { + return root; } - - public Function setParent(Function value) { - parent = value; - return this; - } - + public Function getVariable2() { return variable2; } public void setVariable2(Function value) { - value.setParent(this); variable2 = value; } @@ -74,8 +69,8 @@ public abstract class FunctionTwoValues implements Function { if (result == null || result.isEmpty()) { result = new ArrayList<>(); - ArrayList l1 = new ArrayList(); - ArrayList l2 = new ArrayList(); + ArrayList l1 = new ArrayList<>(); + ArrayList l2 = new ArrayList<>(); if (variable1.isSolved()) { l1.add(variable1); } else { @@ -90,7 +85,7 @@ public abstract class FunctionTwoValues implements Function { Function[][] results = Utils.joinFunctionsResults(l1, l2); for (Function[] f : results) { - result.add(NewInstance(this.parent, (Function)f[0], (Function)f[1])); + result.add(NewInstance(this.root, f[0], f[1])); } } diff --git a/src/org/warp/picalculator/math/functions/Joke.java b/src/org/warp/picalculator/math/functions/Joke.java index 38d0886d..a8201aeb 100644 --- a/src/org/warp/picalculator/math/functions/Joke.java +++ b/src/org/warp/picalculator/math/functions/Joke.java @@ -7,6 +7,7 @@ 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; +import org.warp.picalculator.math.Calculator; public class Joke implements Function { @@ -16,8 +17,10 @@ public class Joke implements Function { private static final String[] jokes = new String[]{"♓", "TORNADO", "SHARKNADO"}; private static final int[] jokesFont = new int[]{4, -1, -1}; private final byte joke; + private final Calculator root; - public Joke(byte joke) { + public Joke(Calculator root, byte joke) { + this.root = root; this.joke = joke; } @@ -75,15 +78,10 @@ public class Joke implements Function { public int getLine() { return getHeight()/2; } - - @Override - public Function setParent(Function value) { - return this; - } @Override - public Function getParent() { - return null; + public Calculator getRoot() { + return root; } private boolean small = false; diff --git a/src/org/warp/picalculator/math/functions/Multiplication.java b/src/org/warp/picalculator/math/functions/Multiplication.java index 91b6067e..57fc82c1 100644 --- a/src/org/warp/picalculator/math/functions/Multiplication.java +++ b/src/org/warp/picalculator/math/functions/Multiplication.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.functions; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.MathematicalSymbols; import org.warp.picalculator.math.rules.ExponentRule15; import org.warp.picalculator.math.rules.NumberRule1; @@ -13,8 +14,8 @@ import org.warp.picalculator.math.rules.methods.MultiplicationMethod1; public class Multiplication extends FunctionTwoValues { - public Multiplication(Function parent, Function value1, Function value2) { - super(parent, value1, value2); + public Multiplication(Calculator root, Function value1, Function value2) { + super(root, value1, value2); if (value1 instanceof Variable && value2 instanceof Variable == false) { variable1 = value2; variable2 = value1; @@ -22,8 +23,8 @@ public class Multiplication extends FunctionTwoValues { } @Override - protected Function NewInstance(Function parent2, Function value1, Function value2) { - return new Multiplication(parent, value1, value2); + protected Function NewInstance(Calculator root, Function value1, Function value2) { + return new Multiplication(root, value1, value2); } @Override diff --git a/src/org/warp/picalculator/math/functions/Negative.java b/src/org/warp/picalculator/math/functions/Negative.java index 84e83e10..dba70998 100644 --- a/src/org/warp/picalculator/math/functions/Negative.java +++ b/src/org/warp/picalculator/math/functions/Negative.java @@ -6,19 +6,20 @@ 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.Calculator; import org.warp.picalculator.math.MathematicalSymbols; 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); + public Negative(Calculator root, Function value) { + super(root, value); } - + @Override - public Function NewInstance(Function parent, Function value) { - return new Negative(parent, value); + public Function NewInstance(Calculator root, Function value) { + return new Negative(root, value); } @Override @@ -44,6 +45,7 @@ public class Negative extends AnteriorFunction { return false; } + @Override public ArrayList solve() throws Error { if (variable == null) { throw new Error(Errors.SYNTAX_ERROR); @@ -56,7 +58,7 @@ public class Negative extends AnteriorFunction { } else if (variable.isSolved()) { try { Number var = (Number) getVariable(); - result.add(var.multiply(new Number(null, "-1"))); + result.add(var.multiply(new Number(root, "-1"))); } catch(NullPointerException ex) { throw new Error(Errors.ERROR); } catch(NumberFormatException ex) { @@ -65,7 +67,7 @@ public class Negative extends AnteriorFunction { throw new Error(Errors.NUMBER_TOO_SMALL); } } else { - ArrayList l1 = new ArrayList(); + ArrayList l1 = new ArrayList<>(); if (variable.isSolved()) { l1.add(variable); } else { @@ -73,7 +75,7 @@ public class Negative extends AnteriorFunction { } for (Function f : l1) { - result.add(new Negative(this.parent, (Function)f)); + result.add(new Negative(root, f)); } } return result; diff --git a/src/org/warp/picalculator/math/functions/Number.java b/src/org/warp/picalculator/math/functions/Number.java index fe552965..1f357449 100644 --- a/src/org/warp/picalculator/math/functions/Number.java +++ b/src/org/warp/picalculator/math/functions/Number.java @@ -20,37 +20,37 @@ import com.rits.cloning.Cloner; public class Number implements Function { - private Function parent; + private final Calculator root; protected BigDecimal term; protected int width; protected int height; protected int line; protected boolean small; - public Number(Calculator calc, Function parent, BigInteger val) { - this.parent = parent; + public Number(Calculator root, BigInteger val) { + this.root = root; term = new BigDecimal(val).setScale(Utils.scale, Utils.scaleMode2); } - public Number(Function parent, BigDecimal val) { - this.parent = parent; + public Number(Calculator root, BigDecimal val) { + this.root = root; term = val.setScale(Utils.scale, Utils.scaleMode2); } - public Number(Function parent, String s) throws Error { - this(parent, new BigInteger(s)); + public Number(Calculator root, String s) throws Error { + this(root, new BigDecimal(s).setScale(Utils.scale, Utils.scaleMode2)); } - public Number(Function parent, int s) { - this(parent, BigDecimal.valueOf(s).setScale(Utils.scale, Utils.scaleMode2)); + public Number(Calculator root, int s) { + this(root, BigDecimal.valueOf(s).setScale(Utils.scale, Utils.scaleMode2)); } - public Number(Function parent, float s) { - this(parent, BigDecimal.valueOf(s).setScale(Utils.scale, Utils.scaleMode2)); + public Number(Calculator root, float s) { + this(root, BigDecimal.valueOf(s).setScale(Utils.scale, Utils.scaleMode2)); } - public Number(Function parent, double s) { - this(parent, BigDecimal.valueOf(s).setScale(Utils.scale, Utils.scaleMode2)); + public Number(Calculator root, double s) { + this(root, BigDecimal.valueOf(s).setScale(Utils.scale, Utils.scaleMode2)); } public BigDecimal getTerm() { @@ -74,26 +74,26 @@ public class Number implements Function { } public Number add(Number f) { - Number ret = new Number(this.parent, getTerm().add(f.getTerm())); + Number ret = new Number(this.root, getTerm().add(f.getTerm())); return ret; } public Number multiply(Number f) { - Number ret = new Number(this.parent, getTerm().multiply(f.getTerm())); + Number ret = new Number(this.root, getTerm().multiply(f.getTerm())); return ret; } public Number divide(Number f) throws Error { - Number ret = new Number(this.parent, BigDecimalMath.divideRound(getTerm(), f.getTerm())); + Number ret = new Number(this.root, BigDecimalMath.divideRound(getTerm(), f.getTerm())); return ret; } public Number pow(Number f) throws Error { - Number ret = new Number(this.parent, BigDecimal.ONE); + Number ret = new Number(this.root, BigDecimal.ONE); if (Utils.isIntegerValue(f.term)) { final BigInteger bi = f.term.toBigInteger(); for (BigInteger i = BigInteger.ZERO; i.compareTo(bi) < 0; i = i.add(BigInteger.ONE)) { - ret = ret.multiply(new Number(this.parent, getTerm())); + ret = ret.multiply(new Number(this.root, getTerm())); } } else { ret.term = BigDecimalMath.pow(term, f.term); @@ -113,7 +113,7 @@ public class Number implements Function { s = s+"…"; } - if (Calculator.exactMode == false) { + if (root.exactMode == false) { String cuttedNumber = s.split("\\.")[0]; if (cuttedNumber.length() > 8) { return cuttedNumber.substring(0, 1)+","+cuttedNumber.substring(1, 8)+"ℯ℮"+(cuttedNumber.length()-1); @@ -265,14 +265,9 @@ public class Number implements Function { return false; } - public Function setParent(Function value) { - parent = value; - return this; - } - @Override - public Function getParent() { - return parent; + public Calculator getRoot() { + return root; } /* @@ -302,7 +297,7 @@ public class Number implements Function { { BigInteger n = getTerm().toBigIntegerExact(); BigInteger two = BigInteger.valueOf(2); - LinkedList fs = new LinkedList(); + LinkedList fs = new LinkedList<>(); if (n.compareTo(two) < 0) { diff --git a/src/org/warp/picalculator/math/functions/Power.java b/src/org/warp/picalculator/math/functions/Power.java index 61be6cd9..720cdb16 100644 --- a/src/org/warp/picalculator/math/functions/Power.java +++ b/src/org/warp/picalculator/math/functions/Power.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.functions; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.MathematicalSymbols; import org.warp.picalculator.math.rules.ExponentRule1; import org.warp.picalculator.math.rules.ExponentRule2; @@ -14,13 +15,13 @@ import org.warp.picalculator.math.rules.UndefinedRule1; public class Power extends FunctionTwoValues { - public Power(Function parent, Function value1, Function value2) { - super(parent, value1, value2); + public Power(Calculator root, Function value1, Function value2) { + super(root, value1, value2); } @Override - protected Function NewInstance(Function parent2, Function value1, Function value2) { - return new Power(parent, value1, value2); + protected Function NewInstance(Calculator root, Function value1, Function value2) { + return new Power(root, value1, value2); } @Override @@ -74,7 +75,7 @@ public class Power extends FunctionTwoValues { } else if (FractionsRule5.compare(this)) { result.addAll(FractionsRule5.execute(this)); } else if (variable1 instanceof Number & variable2 instanceof Number) { - result.add((Function) ((Number)variable1).pow((Number)variable2)); + result.add(((Number)variable1).pow((Number)variable2)); } return result; } diff --git a/src/org/warp/picalculator/math/functions/Root.java b/src/org/warp/picalculator/math/functions/Root.java index c0de42bf..fa44fc92 100644 --- a/src/org/warp/picalculator/math/functions/Root.java +++ b/src/org/warp/picalculator/math/functions/Root.java @@ -12,13 +12,13 @@ import org.warp.picalculator.math.MathematicalSymbols; public class Root extends FunctionTwoValues { - public Root(Function parent, Function value1, Function value2) { - super(parent, value1, value2); + public Root(Calculator root, Function value1, Function value2) { + super(root, value1, value2); } @Override - protected Function NewInstance(Function parent2, Function value1, Function value2) { - return new Root(parent, value1, value2); + protected Function NewInstance(Calculator root, Function value1, Function value2) { + return new Root(root, value1, value2); } @Override @@ -42,14 +42,14 @@ public class Root extends FunctionTwoValues { @Override protected boolean isSolvable() { if (variable1 instanceof Number & variable2 instanceof Number) { - if (Calculator.exactMode == false) { + if (root.exactMode == false) { return true; } try { - Number exponent = new Number(this.parent, BigDecimal.ONE); + Number exponent = new Number(root, BigDecimal.ONE); exponent = exponent.divide((Number) variable1); Number resultVal = ((Number)variable2).pow(exponent); - Number originalVariable = resultVal.pow(new Number(null, 2)); + Number originalVariable = resultVal.pow(new Number(root, 2)); if (originalVariable.equals(variable2)) { return true; } @@ -57,7 +57,7 @@ public class Root extends FunctionTwoValues { ex.printStackTrace(); } } - if (variable1 instanceof Number && ((Number)variable1).equals(new Number(null, 2))) { + if (variable1 instanceof Number && ((Number)variable1).equals(new Number(root, 2))) { return true; } return false; @@ -66,11 +66,11 @@ public class Root extends FunctionTwoValues { @Override public ArrayList solve() throws Error { ArrayList result = new ArrayList<>(); - if (Calculator.exactMode) { - if (variable1 instanceof Number && ((Number)variable1).equals(new Number(null, 2))) { - result.add(new RootSquare(parent, variable2)); + if (root.exactMode) { + if (variable1 instanceof Number && ((Number)variable1).equals(new Number(root, 2))) { + result.add(new RootSquare(root, variable2)); } else { - Number exponent = new Number(this.parent, BigInteger.ONE); + Number exponent = new Number(root, BigInteger.ONE); exponent = exponent.divide((Number) variable1); result.add(((Number)variable2).pow(exponent)); } @@ -78,7 +78,7 @@ public class Root extends FunctionTwoValues { Number exp = (Number) variable1; Number numb = (Number) variable2; - result.add(numb.pow(new Number(null, 1).divide(exp)).setParent(parent)); + result.add(numb.pow(new Number(root, 1).divide(exp))); } return result; } diff --git a/src/org/warp/picalculator/math/functions/RootSquare.java b/src/org/warp/picalculator/math/functions/RootSquare.java index 75d2c376..19f42496 100644 --- a/src/org/warp/picalculator/math/functions/RootSquare.java +++ b/src/org/warp/picalculator/math/functions/RootSquare.java @@ -10,13 +10,13 @@ import org.warp.picalculator.math.MathematicalSymbols; public class RootSquare extends AnteriorFunction { - public RootSquare(Function parent, Function value) { - super(parent, value); + public RootSquare(Calculator root, Function value) { + super(root, value); } @Override - public Function NewInstance(Function parent, Function value) { - return new RootSquare(parent, value); + public Function NewInstance(Calculator root, Function value) { + return new RootSquare(root, value); } @Override @@ -37,14 +37,14 @@ public class RootSquare extends AnteriorFunction { @Override protected boolean isSolvable() { if (variable instanceof Number) { - if (Calculator.exactMode == false) { + if (root.exactMode == false) { return true; } try { - Number exponent = new Number(this.parent, BigInteger.ONE); - exponent = exponent.divide(new Number(null, 2)); + Number exponent = new Number(root, BigInteger.ONE); + exponent = exponent.divide(new Number(root, 2)); Number resultVal = ((Number)variable).pow(exponent); - Number originalVariable = resultVal.pow(new Number(null, 2)); + Number originalVariable = resultVal.pow(new Number(root, 2)); if (originalVariable.equals(variable)) { return true; } @@ -58,15 +58,15 @@ public class RootSquare extends AnteriorFunction { @Override public ArrayList solve() throws Error { ArrayList result = new ArrayList<>(); - if (Calculator.exactMode) { - Number exponent = new Number(this.parent, BigInteger.ONE); - exponent = exponent.divide(new Number(null, 2)); + if (root.exactMode) { + Number exponent = new Number(root, BigInteger.ONE); + exponent = exponent.divide(new Number(root, 2)); result.add(((Number)variable).pow(exponent)); } else { - Number exp = new Number(null, 2); + Number exp = new Number(root, 2); Number numb = (Number) variable; - result.add(numb.pow(new Number(null, 1).divide(exp)).setParent(parent)); + result.add(numb.pow(new Number(root, 1).divide(exp))); } return result; } diff --git a/src/org/warp/picalculator/math/functions/Subtraction.java b/src/org/warp/picalculator/math/functions/Subtraction.java index 5525a2d0..99ce1393 100644 --- a/src/org/warp/picalculator/math/functions/Subtraction.java +++ b/src/org/warp/picalculator/math/functions/Subtraction.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.functions; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.MathematicalSymbols; import org.warp.picalculator.math.rules.ExpandRule1; import org.warp.picalculator.math.rules.ExpandRule5; @@ -15,13 +16,13 @@ import org.warp.picalculator.math.rules.methods.SumMethod1; public class Subtraction extends FunctionTwoValues { - public Subtraction(Function parent, Function value1, Function value2) { - super(parent, value1, value2); + public Subtraction(Calculator root, Function value1, Function value2) { + super(root, value1, value2); } @Override - protected Function NewInstance(Function parent2, Function value1, Function value2) { - return new Subtraction(parent, value1, value2); + protected Function NewInstance(Calculator root, Function value1, Function value2) { + return new Subtraction(root, value1, value2); } @Override @@ -65,7 +66,7 @@ public class Subtraction extends FunctionTwoValues { } else if (SumMethod1.compare(this)) { result = SumMethod1.execute(this); } else if (variable1.isSolved() & variable2.isSolved()) { - result.add(((Number)variable1).add(((Number)variable2).multiply(new Number(this.parent, "-1")))); + result.add(((Number)variable1).add(((Number)variable2).multiply(new Number(root, "-1")))); } return result; } diff --git a/src/org/warp/picalculator/math/functions/Sum.java b/src/org/warp/picalculator/math/functions/Sum.java index 76b0591b..6214770a 100644 --- a/src/org/warp/picalculator/math/functions/Sum.java +++ b/src/org/warp/picalculator/math/functions/Sum.java @@ -8,6 +8,7 @@ import java.util.ArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.Errors; import org.warp.picalculator.Utils; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.MathematicalSymbols; import org.warp.picalculator.math.rules.NumberRule3; import org.warp.picalculator.math.rules.NumberRule5; @@ -20,13 +21,13 @@ import org.warp.picalculator.math.rules.methods.SumMethod1; public class Sum extends FunctionTwoValues { - public Sum(Function parent, Function value1, Function value2) { - super(parent, value1, value2); + public Sum(Calculator root, Function value1, Function value2) { + super(root, value1, value2); } @Override - protected Function NewInstance(Function parent2, Function value1, Function value2) { - return new Sum(parent, value1, value2); + protected Function NewInstance(Calculator root, Function value1, Function value2) { + return new Sum(root, value1, value2); } @Override @@ -73,15 +74,15 @@ public class Sum extends FunctionTwoValues { } else if (SumMethod1.compare(this)) { result = SumMethod1.execute(this); } else if (variable1.isSolved() & variable2.isSolved()) { - if ((parent == null || parent.getParent() == null)) { + if ((root.getChild().equals(this))) { if (((Number)variable1).term.compareTo(new BigDecimal(2)) == 0 && ((Number)variable2).term.compareTo(new BigDecimal(2)) == 0) { - result.add(new Joke(Joke.FISH)); + result.add(new Joke(root, Joke.FISH)); return result; } else if (((Number)variable1).term.compareTo(new BigDecimal(20)) == 0 && ((Number)variable2).term.compareTo(new BigDecimal(20)) == 0) { - result.add(new Joke(Joke.TORNADO)); + result.add(new Joke(root, Joke.TORNADO)); return result; } else if (((Number)variable1).term.compareTo(new BigDecimal(29)) == 0 && ((Number)variable2).term.compareTo(new BigDecimal(29)) == 0) { - result.add(new Joke(Joke.SHARKNADO)); + result.add(new Joke(root, Joke.SHARKNADO)); return result; } } diff --git a/src/org/warp/picalculator/math/functions/SumSubtraction.java b/src/org/warp/picalculator/math/functions/SumSubtraction.java index 7a4dc127..8172c4eb 100644 --- a/src/org/warp/picalculator/math/functions/SumSubtraction.java +++ b/src/org/warp/picalculator/math/functions/SumSubtraction.java @@ -9,6 +9,7 @@ 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.Calculator; import org.warp.picalculator.math.MathematicalSymbols; import org.warp.picalculator.math.rules.ExpandRule1; import org.warp.picalculator.math.rules.NumberRule3; @@ -17,13 +18,13 @@ import org.warp.picalculator.math.rules.NumberRule5; public class SumSubtraction extends FunctionTwoValues { - public SumSubtraction(Function parent, Function value1, Function value2) { - super(parent, value1, value2); + public SumSubtraction(Calculator root, Function value1, Function value2) { + super(root, value1, value2); } @Override - protected Function NewInstance(Function parent2, Function value1, Function value2) { - return new SumSubtraction(parent, value1, value2); + protected Function NewInstance(Calculator root, Function value1, Function value2) { + return new SumSubtraction(root, value1, value2); } @Override @@ -59,7 +60,7 @@ public class SumSubtraction extends FunctionTwoValues { result = NumberRule5.execute(this); } else if (variable1.isSolved() & variable2.isSolved()) { result.add(((Number)variable1).add((Number)variable2)); - result.add(((Number)variable1).add(((Number)variable2).multiply(new Number(this.parent, "-1")))); + result.add(((Number)variable1).add(((Number)variable2).multiply(new Number(root, "-1")))); } return result; } diff --git a/src/org/warp/picalculator/math/functions/Undefined.java b/src/org/warp/picalculator/math/functions/Undefined.java index 3aaf8a44..90562ad4 100644 --- a/src/org/warp/picalculator/math/functions/Undefined.java +++ b/src/org/warp/picalculator/math/functions/Undefined.java @@ -5,11 +5,14 @@ import java.util.List; import org.warp.picalculator.Error; import org.warp.picalculator.Utils; import org.warp.picalculator.device.graphicengine.Display; +import org.warp.picalculator.math.Calculator; public class Undefined implements Function { + + protected final Calculator root; - public Undefined(Function parent) { - setParent(parent); + public Undefined(Calculator root) { + this.root = root; } @Override @@ -28,7 +31,6 @@ public class Undefined implements Function { } private int width, height, line; - private Function parent; private boolean small; @Override @@ -60,14 +62,8 @@ public class Undefined implements Function { } @Override - public Function setParent(Function parent) { - this.parent = parent; - return this; - } - - @Override - public Function getParent() { - return parent; + public Calculator getRoot() { + return root; } @Override diff --git a/src/org/warp/picalculator/math/functions/Variable.java b/src/org/warp/picalculator/math/functions/Variable.java index cec605c2..50e03a4b 100644 --- a/src/org/warp/picalculator/math/functions/Variable.java +++ b/src/org/warp/picalculator/math/functions/Variable.java @@ -9,25 +9,26 @@ import java.util.List; import org.warp.picalculator.Error; import org.warp.picalculator.Utils; import org.warp.picalculator.device.graphicengine.Display; +import org.warp.picalculator.math.Calculator; import com.rits.cloning.Cloner; public class Variable implements Function { - private Function parent; protected char var; protected int width; protected int height; protected int line; protected boolean small; + protected final Calculator root; - public Variable(Function parent, char val) { - this.parent = parent; + public Variable(Calculator root, char val) { + this.root = root; var = val; } - public Variable(Function parent, String s) throws Error { - this(parent, s.charAt(0)); + public Variable(Calculator root, String s) throws Error { + this(root, s.charAt(0)); } public char getChar() { @@ -142,13 +143,8 @@ public class Variable implements Function { return false; } - public Function setParent(Function value) { - parent = value; - return this; - } - @Override - public Function getParent() { - return parent; + public Calculator getRoot() { + return root; } } diff --git a/src/org/warp/picalculator/math/functions/equations/Equation.java b/src/org/warp/picalculator/math/functions/equations/Equation.java index a06a4ae9..870a177f 100644 --- a/src/org/warp/picalculator/math/functions/equations/Equation.java +++ b/src/org/warp/picalculator/math/functions/equations/Equation.java @@ -8,6 +8,7 @@ import java.util.Set; import org.warp.picalculator.Error; import org.warp.picalculator.Errors; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.MathematicalSymbols; import org.warp.picalculator.math.SolveMethod; import org.warp.picalculator.math.functions.Function; @@ -19,8 +20,13 @@ import com.rits.cloning.Cloner; public class Equation extends FunctionTwoValues { - public Equation(Function parent, Function value1, Function value2) { - super(parent, value1,value2); + public Equation(Calculator root, Function value1, Function value2) { + super(root, value1,value2); + } + + @Override + protected Function NewInstance(Calculator root, Function value1, Function value2) { + return new Equation(root, value1, value2); } @Override @@ -46,9 +52,9 @@ public class Equation extends FunctionTwoValues { if (((Number)variable2).getTerm().compareTo(new BigDecimal(0)) == 0) { result.add(this); } else { - Equation e = new Equation(this.parent, null, null); - e.setVariable1(new Subtraction(e, variable1, variable2)); - e.setVariable2(new Number(e, "0")); + Equation e = new Equation(root, null, null); + e.setVariable1(new Subtraction(root, variable1, variable2)); + e.setVariable2(new Number(root, "0")); result.add(e); } } @@ -65,10 +71,10 @@ public class Equation extends FunctionTwoValues { //WORK IN PROGRESS public ArrayList solveStep(char charIncognita) { - ArrayList result = new ArrayList(); + ArrayList result = new ArrayList<>(); result.add(this.clone()); for (SolveMethod t : SolveMethod.techniques) { - ArrayList newResults = new ArrayList(); + ArrayList newResults = new ArrayList<>(); final int sz = result.size(); for (int n = 0; n < sz; n++) { newResults.addAll(t.solve(result.get(n))); diff --git a/src/org/warp/picalculator/math/functions/equations/EquationResult.java b/src/org/warp/picalculator/math/functions/equations/EquationResult.java index 0581f5d9..6c0ee773 100644 --- a/src/org/warp/picalculator/math/functions/equations/EquationResult.java +++ b/src/org/warp/picalculator/math/functions/equations/EquationResult.java @@ -1,12 +1,10 @@ package org.warp.picalculator.math.functions.equations; -import java.math.BigInteger; - import org.warp.picalculator.math.functions.Number; public class EquationResult { public boolean isAnEquation = false; - public Number LR = new Number(null, new BigInteger("0")); + public Number LR; 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 c005fa3c..7c8f9403 100644 --- a/src/org/warp/picalculator/math/functions/equations/EquationsSystem.java +++ b/src/org/warp/picalculator/math/functions/equations/EquationsSystem.java @@ -6,6 +6,7 @@ import java.util.ArrayList; import java.util.List; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Expression; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.FunctionMultipleValues; @@ -13,17 +14,17 @@ import org.warp.picalculator.math.functions.Number; public class EquationsSystem extends FunctionMultipleValues { static final int spacing = 2; - - public EquationsSystem(Function parent) { - super(parent); + + public EquationsSystem(Calculator root) { + super(root); } - public EquationsSystem(Function parent, Function value) { - super(parent, new Function[]{value}); + public EquationsSystem(Calculator root, Function value) { + super(root, new Function[]{value}); } - public EquationsSystem(Function parent, Function[] value) { - super(parent, value); + public EquationsSystem(Calculator root, Function[] value) { + super(root, value); } @Override @@ -52,7 +53,7 @@ public class EquationsSystem extends FunctionMultipleValues { if (f instanceof Number) { ret.add(f); } else { - ret.add(new Expression(this.parent, new Function[]{(Function) f})); + ret.add(new Expression(this.root, new Function[]{f})); } } return ret; @@ -62,7 +63,7 @@ public class EquationsSystem extends FunctionMultipleValues { if (f.isSolved() == false) { List partial = f.solveOneStep(); for (Function fnc : partial) { - ret.add(new Expression(this.parent, new Function[]{(Function) fnc})); + ret.add(new Expression(this.root, new 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 f34f046b..f9435bd7 100644 --- a/src/org/warp/picalculator/math/functions/equations/EquationsSystemPart.java +++ b/src/org/warp/picalculator/math/functions/equations/EquationsSystemPart.java @@ -6,37 +6,27 @@ import java.util.ArrayList; import java.util.List; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.MathematicalSymbols; import org.warp.picalculator.math.functions.AnteriorFunction; import org.warp.picalculator.math.functions.Function; public class EquationsSystemPart extends AnteriorFunction { - public EquationsSystemPart(Function parent, Equation equazione) { - super(parent, equazione); + public EquationsSystemPart(Calculator root, Equation equazione) { + super(root, equazione); } + @Override + protected Function NewInstance(Calculator root, Function value) { + return new EquationsSystemPart(root, value); + } + @Override public String getSymbol() { return MathematicalSymbols.SYSTEM; } - @Override - protected boolean isSolvable() throws Error { - return variable.isSolved()==false; - } - - @Override - public List solveOneStep() throws NumberFormatException, Error { - // TODO implementare il calcolo dei sistemi - if (variable.isSolved()) { - List ret = new ArrayList<>(); - ret.add(variable); - return ret; - } - return variable.solveOneStep(); - } - @Override public void generateGraphics() { variable.setSmall(false); diff --git a/src/org/warp/picalculator/math/functions/trigonometry/ArcCosine.java b/src/org/warp/picalculator/math/functions/trigonometry/ArcCosine.java index 0e42f216..d1697784 100644 --- a/src/org/warp/picalculator/math/functions/trigonometry/ArcCosine.java +++ b/src/org/warp/picalculator/math/functions/trigonometry/ArcCosine.java @@ -3,19 +3,20 @@ package org.warp.picalculator.math.functions.trigonometry; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.MathematicalSymbols; import org.warp.picalculator.math.functions.AnteriorFunction; import org.warp.picalculator.math.functions.Function; public class ArcCosine extends AnteriorFunction { - public ArcCosine(Function parent, Function value) { - super(parent, value); + public ArcCosine(Calculator root, Function value) { + super(root, value); } @Override - public Function NewInstance(Function parent, Function value) { - return new ArcCosine(parent, value); + public Function NewInstance(Calculator root, Function value) { + return new ArcCosine(root, value); } @Override diff --git a/src/org/warp/picalculator/math/functions/trigonometry/ArcSine.java b/src/org/warp/picalculator/math/functions/trigonometry/ArcSine.java index 3fda1c1f..ad00e7c4 100644 --- a/src/org/warp/picalculator/math/functions/trigonometry/ArcSine.java +++ b/src/org/warp/picalculator/math/functions/trigonometry/ArcSine.java @@ -3,19 +3,20 @@ package org.warp.picalculator.math.functions.trigonometry; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.MathematicalSymbols; import org.warp.picalculator.math.functions.AnteriorFunction; import org.warp.picalculator.math.functions.Function; public class ArcSine extends AnteriorFunction { - public ArcSine(Function parent, Function value) { - super(parent, value); + public ArcSine(Calculator root, Function value) { + super(root, value); } @Override - public Function NewInstance(Function parent, Function value) { - return new ArcSine(parent, value); + public Function NewInstance(Calculator root, Function value) { + return new ArcSine(root, value); } @Override diff --git a/src/org/warp/picalculator/math/functions/trigonometry/ArcTangent.java b/src/org/warp/picalculator/math/functions/trigonometry/ArcTangent.java index 3d3d8048..65496150 100644 --- a/src/org/warp/picalculator/math/functions/trigonometry/ArcTangent.java +++ b/src/org/warp/picalculator/math/functions/trigonometry/ArcTangent.java @@ -3,19 +3,20 @@ package org.warp.picalculator.math.functions.trigonometry; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.MathematicalSymbols; import org.warp.picalculator.math.functions.AnteriorFunction; import org.warp.picalculator.math.functions.Function; public class ArcTangent extends AnteriorFunction { - public ArcTangent(Function parent, Function value) { - super(parent, value); + public ArcTangent(Calculator root, Function value) { + super(root, value); } @Override - public Function NewInstance(Function parent, Function value) { - return new ArcTangent(parent, value); + public Function NewInstance(Calculator root, Function value) { + return new ArcTangent(root, value); } @Override diff --git a/src/org/warp/picalculator/math/functions/trigonometry/Cosine.java b/src/org/warp/picalculator/math/functions/trigonometry/Cosine.java index 8364a36d..a89435d7 100644 --- a/src/org/warp/picalculator/math/functions/trigonometry/Cosine.java +++ b/src/org/warp/picalculator/math/functions/trigonometry/Cosine.java @@ -3,19 +3,20 @@ package org.warp.picalculator.math.functions.trigonometry; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.MathematicalSymbols; import org.warp.picalculator.math.functions.AnteriorFunction; import org.warp.picalculator.math.functions.Function; public class Cosine extends AnteriorFunction { - public Cosine(Function parent, Function value) { - super(parent, value); + public Cosine(Calculator root, Function value) { + super(root, value); } @Override - public Function NewInstance(Function parent, Function value) { - return new Cosine(parent, value); + public Function NewInstance(Calculator root, Function value) { + return new Cosine(root, value); } @Override diff --git a/src/org/warp/picalculator/math/functions/trigonometry/Sine.java b/src/org/warp/picalculator/math/functions/trigonometry/Sine.java index 0bc8b3b6..66da00f5 100644 --- a/src/org/warp/picalculator/math/functions/trigonometry/Sine.java +++ b/src/org/warp/picalculator/math/functions/trigonometry/Sine.java @@ -1,29 +1,25 @@ package org.warp.picalculator.math.functions.trigonometry; -import java.math.BigInteger; import java.util.ArrayList; import org.nevec.rjm.BigDecimalMath; -import org.nevec.rjm.BigIntegerMath; import org.warp.picalculator.Error; -import org.warp.picalculator.Utils; import org.warp.picalculator.math.AngleMode; import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.MathematicalSymbols; import org.warp.picalculator.math.functions.AnteriorFunction; import org.warp.picalculator.math.functions.Function; -import org.warp.picalculator.math.functions.Multiplication; import org.warp.picalculator.math.functions.Number; public class Sine extends AnteriorFunction { - public Sine(Function parent, Function value) { - super(parent, value); + public Sine(Calculator root, Function value) { + super(root, value); } @Override - public Function NewInstance(Function parent, Function value) { - return new Sine(parent, value); + public Function NewInstance(Calculator root, Function value) { + return new Sine(root, value); } @Override @@ -34,12 +30,12 @@ public class Sine extends AnteriorFunction { @Override protected boolean isSolvable() { if (variable instanceof Number) { - if (Calculator.exactMode == false) { + if (root.exactMode == false) { return true; } } - if (Calculator.angleMode == AngleMode.DEG) { - Function[] solvableValues = new Function[]{new Number(null, 0), new Number(null, 30), new Number(null, 90), }; + if (root.angleMode == AngleMode.DEG) { + Function[] solvableValues = new Function[]{new Number(root, 0), new Number(root, 30), new Number(root, 90), }; } return false; } @@ -48,8 +44,8 @@ public class Sine extends AnteriorFunction { public ArrayList solve() throws Error { ArrayList results = new ArrayList<>(); if (variable instanceof Number) { - if (Calculator.exactMode == false) { - results.add(new Number(parent, BigDecimalMath.sin(((Number) variable).getTerm()))); + if (root.exactMode == false) { + results.add(new Number(root, BigDecimalMath.sin(((Number) variable).getTerm()))); } } return results; diff --git a/src/org/warp/picalculator/math/functions/trigonometry/Tangent.java b/src/org/warp/picalculator/math/functions/trigonometry/Tangent.java index 5f8f626d..5b2002f0 100644 --- a/src/org/warp/picalculator/math/functions/trigonometry/Tangent.java +++ b/src/org/warp/picalculator/math/functions/trigonometry/Tangent.java @@ -3,19 +3,20 @@ package org.warp.picalculator.math.functions.trigonometry; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.MathematicalSymbols; import org.warp.picalculator.math.functions.AnteriorFunction; import org.warp.picalculator.math.functions.Function; public class Tangent extends AnteriorFunction { - public Tangent(Function parent, Function value) { - super(parent, value); + public Tangent(Calculator root, Function value) { + super(root, value); } @Override - public Function NewInstance(Function parent, Function value) { - return new Tangent(parent, value); + public Function NewInstance(Calculator root, Function value) { + return new Tangent(root, value); } @Override diff --git a/src/org/warp/picalculator/math/rules/ExpandRule1.java b/src/org/warp/picalculator/math/rules/ExpandRule1.java index d230506b..c5ed15d0 100644 --- a/src/org/warp/picalculator/math/rules/ExpandRule1.java +++ b/src/org/warp/picalculator/math/rules/ExpandRule1.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Expression; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.FunctionTwoValues; @@ -55,6 +56,7 @@ public class ExpandRule1 { public static ArrayList execute(Function f) throws Error { ArrayList result = new ArrayList<>(); + Calculator root = f.getRoot(); Expression expr = null; int fromSubtraction = 0; @@ -78,10 +80,10 @@ public class ExpandRule1 { if (fnc instanceof Sum) { Function a = ((Sum) fnc).getVariable1(); Function b = ((Sum) fnc).getVariable2(); - Subtraction fnc2 = new Subtraction(f.getParent(), null, b); - fnc2.setVariable1(new Negative(fnc2, a)); + Subtraction fnc2 = new Subtraction(root, null, b); + fnc2.setVariable1(new Negative(root, a)); if (fromSubtraction > 0) { - subtraction = new Subtraction(f.getParent(), null, null); + subtraction = new Subtraction(root, null, null); subtraction.setVariable1(((FunctionTwoValues)f).getVariable1()); subtraction.setVariable2(fnc2); result.add(subtraction); @@ -91,10 +93,10 @@ public class ExpandRule1 { } 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)); + Sum fnc2 = new Sum(root, null, b); + fnc2.setVariable1(new Negative(root, a)); if (fromSubtraction > 0) { - subtraction = new Subtraction(f.getParent(), null, null); + subtraction = new Subtraction(root, null, null); subtraction.setVariable1(((FunctionTwoValues)f).getVariable1()); subtraction.setVariable2(fnc2); result.add(subtraction); @@ -104,16 +106,16 @@ public class ExpandRule1 { } else if (fnc instanceof SumSubtraction) { Function a = ((SumSubtraction) fnc).getVariable1(); Function b = ((SumSubtraction) fnc).getVariable2(); - Sum fnc2 = new Sum(f.getParent(), null, b); - fnc2.setVariable1(new Negative(fnc2, a)); - Subtraction fnc3 = new Subtraction(f.getParent(), null, b); - fnc3.setVariable1(new Negative(fnc2, a)); + Sum fnc2 = new Sum(root, null, b); + fnc2.setVariable1(new Negative(root, a)); + Subtraction fnc3 = new Subtraction(root, null, b); + fnc3.setVariable1(new Negative(root, a)); if (fromSubtraction > 0) { - subtraction = new SumSubtraction(f.getParent(), null, null); + subtraction = new SumSubtraction(root, null, null); subtraction.setVariable1(((FunctionTwoValues)f).getVariable1()); subtraction.setVariable2(fnc2); result.add(subtraction); - subtraction = new SumSubtraction(f.getParent(), null, null); + subtraction = new SumSubtraction(root, null, null); subtraction.setVariable1(((FunctionTwoValues)f).getVariable1()); subtraction.setVariable2(fnc3); result.add(subtraction); diff --git a/src/org/warp/picalculator/math/rules/ExpandRule5.java b/src/org/warp/picalculator/math/rules/ExpandRule5.java index e44e213d..c8abd2d9 100644 --- a/src/org/warp/picalculator/math/rules/ExpandRule5.java +++ b/src/org/warp/picalculator/math/rules/ExpandRule5.java @@ -39,10 +39,10 @@ public class ExpandRule5 { if (f instanceof Negative) { Negative fnc = (Negative) f; - result.add(((Negative)((Expression)fnc.getVariable()).getVariable(0)).getVariable().setParent(f.getParent())); + result.add(((Negative)((Expression)fnc.getVariable()).getVariable(0)).getVariable().setParent(root)); } else if (f instanceof Subtraction) { Subtraction fnc = (Subtraction) f; - result.add(((Negative)((Expression)fnc.getVariable2()).getVariable(0)).getVariable().setParent(f.getParent())); + result.add(((Negative)((Expression)fnc.getVariable2()).getVariable(0)).getVariable().setParent(root)); } return result; } diff --git a/src/org/warp/picalculator/math/rules/ExponentRule1.java b/src/org/warp/picalculator/math/rules/ExponentRule1.java index 5a59fb63..ff883fc7 100644 --- a/src/org/warp/picalculator/math/rules/ExponentRule1.java +++ b/src/org/warp/picalculator/math/rules/ExponentRule1.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.Number; import org.warp.picalculator.math.functions.Power; @@ -17,15 +18,17 @@ public class ExponentRule1 { public static boolean compare(Function f) { Power fnc = (Power) f; - if (fnc.getVariable1().equals(new Number(null, 1))) { + Calculator root = f.getRoot(); + if (fnc.getVariable1().equals(new Number(root, 1))) { return true; } return false; } public static ArrayList execute(Function f) throws Error { + Calculator root = f.getRoot(); ArrayList result = new ArrayList<>(); - result.add(new Number(f.getParent(), 1)); + result.add(new Number(root, 1)); return result; } diff --git a/src/org/warp/picalculator/math/rules/ExponentRule15.java b/src/org/warp/picalculator/math/rules/ExponentRule15.java index 83eb29b7..475f1208 100644 --- a/src/org/warp/picalculator/math/rules/ExponentRule15.java +++ b/src/org/warp/picalculator/math/rules/ExponentRule15.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Expression; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.Multiplication; @@ -26,13 +27,14 @@ public class ExponentRule15 { } public static ArrayList execute(Function f) throws Error { + Calculator root = f.getRoot(); ArrayList result = new ArrayList<>(); Multiplication fnc = (Multiplication) f; - Power p = new Power(fnc.getParent(), null, null); - Expression expr = new Expression(p); - Function a = fnc.getVariable1().setParent(expr); + Power p = new Power(root, null, null); + Expression expr = new Expression(root); + Function a = fnc.getVariable1(); expr.addFunctionToEnd(a); - Number two = new Number(p, 2); + Number two = new Number(root, 2); p.setVariable1(expr); p.setVariable2(two); result.add(p); diff --git a/src/org/warp/picalculator/math/rules/ExponentRule2.java b/src/org/warp/picalculator/math/rules/ExponentRule2.java index 20e85088..adddb866 100644 --- a/src/org/warp/picalculator/math/rules/ExponentRule2.java +++ b/src/org/warp/picalculator/math/rules/ExponentRule2.java @@ -17,7 +17,7 @@ public class ExponentRule2 { public static boolean compare(Function f) { Power fnc = (Power) f; - if (fnc.getVariable2().equals(new Number(null, 1))) { + if (fnc.getVariable2().equals(new Number(root, 1))) { return true; } return false; @@ -25,7 +25,7 @@ public class ExponentRule2 { public static ArrayList execute(Function f) throws Error { ArrayList result = new ArrayList<>(); - result.add(((Power)f).getVariable1().setParent(f.getParent())); + result.add(((Power)f).getVariable1().setParent(root)); return result; } diff --git a/src/org/warp/picalculator/math/rules/ExponentRule3.java b/src/org/warp/picalculator/math/rules/ExponentRule3.java index f3a64d4a..9a1f08d0 100644 --- a/src/org/warp/picalculator/math/rules/ExponentRule3.java +++ b/src/org/warp/picalculator/math/rules/ExponentRule3.java @@ -17,7 +17,7 @@ public class ExponentRule3 { public static boolean compare(Function f) { Power fnc = (Power) f; - if (fnc.getVariable2().equals(new Number(null, 0))) { + if (fnc.getVariable2().equals(new Number(root, 0))) { return true; } return false; diff --git a/src/org/warp/picalculator/math/rules/FractionsRule1.java b/src/org/warp/picalculator/math/rules/FractionsRule1.java index cd12b213..01c74600 100644 --- a/src/org/warp/picalculator/math/rules/FractionsRule1.java +++ b/src/org/warp/picalculator/math/rules/FractionsRule1.java @@ -19,10 +19,10 @@ public class FractionsRule1 { Division fnc = (Division) f; if (fnc.getVariable1() instanceof Number) { Number numb1 = (Number) fnc.getVariable1(); - if (numb1.equals(new Number(null, 0))) { + if (numb1.equals(new Number(root, 0))) { if (fnc.getVariable2() instanceof Number) { Number numb2 = (Number) fnc.getVariable2(); - if (numb2.equals(new Number(null, 0))) { + if (numb2.equals(new Number(root, 0))) { return false; } } diff --git a/src/org/warp/picalculator/math/rules/FractionsRule2.java b/src/org/warp/picalculator/math/rules/FractionsRule2.java index 067ef9b8..a0e2805b 100644 --- a/src/org/warp/picalculator/math/rules/FractionsRule2.java +++ b/src/org/warp/picalculator/math/rules/FractionsRule2.java @@ -19,7 +19,7 @@ public class FractionsRule2 { Division fnc = (Division) f; if (fnc.getVariable2() instanceof Number) { Number numb = (Number) fnc.getVariable2(); - if (numb.equals(new Number(null, 1))) { + if (numb.equals(new Number(root, 1))) { return true; } } @@ -29,7 +29,7 @@ public class FractionsRule2 { public static ArrayList execute(Function f) throws Error { ArrayList result = new ArrayList<>(); Division fnc = (Division) f; - result.add(fnc.getVariable1().setParent(f.getParent())); + result.add(fnc.getVariable1().setParent(root)); return result; } diff --git a/src/org/warp/picalculator/math/rules/FractionsRule4.java b/src/org/warp/picalculator/math/rules/FractionsRule4.java index e96b240b..df0872a6 100644 --- a/src/org/warp/picalculator/math/rules/FractionsRule4.java +++ b/src/org/warp/picalculator/math/rules/FractionsRule4.java @@ -20,7 +20,7 @@ public class FractionsRule4 { Power fnc = (Power) f; if (fnc.getVariable1() instanceof Division && fnc.getVariable2() instanceof Number) { Number n2 = (Number) fnc.getVariable2(); - if (n2.equals(new Number(null, -1))) { + if (n2.equals(new Number(root, -1))) { return true; } } diff --git a/src/org/warp/picalculator/math/rules/FractionsRule5.java b/src/org/warp/picalculator/math/rules/FractionsRule5.java index 06f5f13f..0cc8b0c0 100644 --- a/src/org/warp/picalculator/math/rules/FractionsRule5.java +++ b/src/org/warp/picalculator/math/rules/FractionsRule5.java @@ -32,8 +32,8 @@ public class FractionsRule5 { 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); + Function c = ((Number)fnc.getVariable2()).multiply(new Number(root, "-1")); + Division dv = new Division(root, b, a); Power pow = new Power(f.getParent(), dv, c); dv.setParent(pow); result.add(pow); diff --git a/src/org/warp/picalculator/math/rules/NumberRule1.java b/src/org/warp/picalculator/math/rules/NumberRule1.java index 420bcf93..dbddb6c7 100644 --- a/src/org/warp/picalculator/math/rules/NumberRule1.java +++ b/src/org/warp/picalculator/math/rules/NumberRule1.java @@ -19,13 +19,13 @@ public class NumberRule1 { Multiplication mult = (Multiplication) f; if (mult.getVariable1() instanceof Number) { Number numb = (Number) mult.getVariable1(); - if (numb.equals(new Number(null, 0))) { + if (numb.equals(new Number(root, 0))) { return true; } } if (mult.getVariable2() instanceof Number) { Number numb = (Number) mult.getVariable2(); - if (numb.equals(new Number(null, 0))) { + if (numb.equals(new Number(root, 0))) { return true; } } diff --git a/src/org/warp/picalculator/math/rules/NumberRule2.java b/src/org/warp/picalculator/math/rules/NumberRule2.java index d6e3f996..31077cc8 100644 --- a/src/org/warp/picalculator/math/rules/NumberRule2.java +++ b/src/org/warp/picalculator/math/rules/NumberRule2.java @@ -19,13 +19,13 @@ public class NumberRule2 { Multiplication mult = (Multiplication) f; if (mult.getVariable1() instanceof Number) { Number numb = (Number) mult.getVariable1(); - if (numb.equals(new Number(null, 1))) { + if (numb.equals(new Number(root, 1))) { return true; } } if (mult.getVariable2() instanceof Number) { Number numb = (Number) mult.getVariable2(); - if (numb.equals(new Number(null, 1))) { + if (numb.equals(new Number(root, 1))) { return true; } } @@ -39,20 +39,20 @@ public class NumberRule2 { Multiplication mult = (Multiplication) f; if (aFound == false & mult.getVariable1() instanceof Number) { Number numb = (Number) mult.getVariable1(); - if (numb.equals(new Number(null, 1))) { + if (numb.equals(new Number(root, 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))) { + if (numb.equals(new Number(root, 1))) { a = mult.getVariable1(); aFound = true; } } - result.add(a.setParent(f.getParent())); + result.add(a.setParent(root)); return result; } diff --git a/src/org/warp/picalculator/math/rules/NumberRule3.java b/src/org/warp/picalculator/math/rules/NumberRule3.java index 7b72d052..1397821d 100644 --- a/src/org/warp/picalculator/math/rules/NumberRule3.java +++ b/src/org/warp/picalculator/math/rules/NumberRule3.java @@ -48,7 +48,7 @@ public class NumberRule3 { ArrayList result = new ArrayList<>(); if (f instanceof SumSubtraction) { Multiplication mul = new Multiplication(f.getParent(), null, null); - mul.setVariable1(new Number(null, 2)); + mul.setVariable1(new Number(root, 2)); mul.setVariable2(f); result.add(mul); } diff --git a/src/org/warp/picalculator/math/rules/NumberRule5.java b/src/org/warp/picalculator/math/rules/NumberRule5.java index 8cc92c1e..dc2ddcfc 100644 --- a/src/org/warp/picalculator/math/rules/NumberRule5.java +++ b/src/org/warp/picalculator/math/rules/NumberRule5.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.FunctionTwoValues; import org.warp.picalculator.math.functions.Number; @@ -22,20 +23,21 @@ public class NumberRule5 { public static boolean compare(Function f) { FunctionTwoValues fnc = (FunctionTwoValues) f; - if (fnc.getVariable1().equals(new Number(null, 0)) || fnc.getVariable2().equals(new Number(null, 0))) { + if (fnc.getVariable1().equals(new Number(root, 0)) || fnc.getVariable2().equals(new Number(root, 0))) { return true; } return false; } public static ArrayList execute(Function f) throws Error { + Calculator root = f.getRoot(); ArrayList result = new ArrayList<>(); FunctionTwoValues fnc = (FunctionTwoValues) f; Function a = fnc.getVariable1(); - if (a.equals(new Number(null, 0))) { + if (a.equals(new Number(root, 0))) { a = fnc.getVariable2(); } - result.add(a.setParent(f.getParent())); + result.add(a); return result; } diff --git a/src/org/warp/picalculator/math/rules/NumberRule6.java b/src/org/warp/picalculator/math/rules/NumberRule6.java index a691b704..a192807c 100644 --- a/src/org/warp/picalculator/math/rules/NumberRule6.java +++ b/src/org/warp/picalculator/math/rules/NumberRule6.java @@ -20,13 +20,13 @@ public class NumberRule6 { Multiplication mult = (Multiplication) f; if (mult.getVariable1() instanceof Number) { Number numb = (Number) mult.getVariable1(); - if (numb.equals(new Number(null, -1))) { + if (numb.equals(new Number(root, -1))) { return true; } } if (mult.getVariable2() instanceof Number) { Number numb = (Number) mult.getVariable2(); - if (numb.equals(new Number(null, -1))) { + if (numb.equals(new Number(root, -1))) { return true; } } @@ -40,14 +40,14 @@ public class NumberRule6 { Multiplication mult = (Multiplication) f; if (aFound == false & mult.getVariable1() instanceof Number) { Number numb = (Number) mult.getVariable1(); - if (numb.equals(new Number(null, -1))) { + if (numb.equals(new Number(root, -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))) { + if (numb.equals(new Number(root, -1))) { a = mult.getVariable1(); aFound = true; } diff --git a/src/org/warp/picalculator/math/rules/NumberRule7.java b/src/org/warp/picalculator/math/rules/NumberRule7.java index e8cdb008..0c4f6663 100644 --- a/src/org/warp/picalculator/math/rules/NumberRule7.java +++ b/src/org/warp/picalculator/math/rules/NumberRule7.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.Multiplication; import org.warp.picalculator.math.functions.Number; @@ -21,9 +22,10 @@ public class NumberRule7 { } public static ArrayList execute(Sum f) throws Error { + Calculator root = f.getRoot(); ArrayList result = new ArrayList<>(); - Multiplication mult = new Multiplication(f.getParent(), null, null); - mult.setVariable1(new Number(null, 2)); + Multiplication mult = new Multiplication(root, null, null); + mult.setVariable1(new Number(root, 2)); mult.setVariable2(f.getVariable1()); result.add(mult); return result; diff --git a/src/org/warp/picalculator/math/rules/UndefinedRule1.java b/src/org/warp/picalculator/math/rules/UndefinedRule1.java index b41209fa..59466edc 100644 --- a/src/org/warp/picalculator/math/rules/UndefinedRule1.java +++ b/src/org/warp/picalculator/math/rules/UndefinedRule1.java @@ -18,7 +18,7 @@ public class UndefinedRule1 { public static boolean compare(Function f) { Power fnc = (Power) f; - if (fnc.getVariable1().equals(new Number(null, 0)) && fnc.getVariable2().equals(new Number(null, 0))) { + if (fnc.getVariable1().equals(new Number(root, 0)) && fnc.getVariable2().equals(new Number(root, 0))) { return true; } return false; @@ -26,7 +26,7 @@ public class UndefinedRule1 { public static ArrayList execute(Function f) throws Error { ArrayList result = new ArrayList<>(); - result.add(new Undefined(f.getParent())); + result.add(new Undefined(root)); return result; } diff --git a/src/org/warp/picalculator/math/rules/UndefinedRule2.java b/src/org/warp/picalculator/math/rules/UndefinedRule2.java index da4d2ee7..38c921e3 100644 --- a/src/org/warp/picalculator/math/rules/UndefinedRule2.java +++ b/src/org/warp/picalculator/math/rules/UndefinedRule2.java @@ -20,7 +20,7 @@ public class UndefinedRule2 { Division fnc = (Division) f; if (fnc.getVariable2() instanceof Number) { Number numb = (Number) fnc.getVariable2(); - if (numb.equals(new Number(null, 0))) { + if (numb.equals(new Number(root, 0))) { return true; } } @@ -29,7 +29,7 @@ public class UndefinedRule2 { public static ArrayList execute(Function f) throws Error { ArrayList result = new ArrayList<>(); - result.add(new Undefined(f.getParent())); + result.add(new Undefined(root)); return result; } diff --git a/src/org/warp/picalculator/math/rules/methods/DivisionRule1.java b/src/org/warp/picalculator/math/rules/methods/DivisionRule1.java index 2f5e12ad..8c64f5c4 100644 --- a/src/org/warp/picalculator/math/rules/methods/DivisionRule1.java +++ b/src/org/warp/picalculator/math/rules/methods/DivisionRule1.java @@ -5,6 +5,7 @@ import java.util.ArrayList; import org.warp.picalculator.Error; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Division; import org.warp.picalculator.math.functions.Number; @@ -16,11 +17,12 @@ import org.warp.picalculator.math.functions.Number; */ public class DivisionRule1 { - public static boolean compare(Function f) { - return ((Division)f).getVariable1().isSolved() && ((Division)f).getVariable2().isSolved() && !(((Division)f).getVariable1() instanceof Number && ((Division)f).getVariable2() instanceof Number) && getFirstWorkingDivisionCouple(getDivisionElements(f)) != null; + public static boolean compare(Division f) { + return f.getVariable1().isSolved() && f.getVariable2().isSolved() && !(f.getVariable1() instanceof Number && f.getVariable2() instanceof Number) && getFirstWorkingDivisionCouple(getDivisionElements(f)) != null; } - public static ArrayList execute(Function f) throws Error { + public static ArrayList execute(Division f) throws Error { + Calculator root = f.getRoot(); Function result; ArrayList elements = getDivisionElements(f); int[] workingElementCouple = getFirstWorkingDivisionCouple(elements); @@ -28,19 +30,14 @@ public class DivisionRule1 { Function elem2 = elements.get(workingElementCouple[1]); final int size = elements.size(); - Function prec = new Multiplication(null, elem1, elem2); - elem1.setParent(prec); - elem2.setParent(prec); + Function prec = new Multiplication(root, elem1, elem2); for (int i = size-1; i >= 0; i--) { if (i != workingElementCouple[0] & i != workingElementCouple[1]) { Function a = prec; Function b = elements.get(i); - prec = new Multiplication(null, a, b); - a.setParent(prec); - b.setParent(prec); + prec = new Multiplication(root, a, b); } } - prec.setParent(f.getParent()); result = prec; @@ -83,7 +80,7 @@ public class DivisionRule1 { b = elements.get(j); if (i != j) { Function testFunc; - testFunc = new Multiplication(null, a, b); + testFunc = new Multiplication(root, a, b); if (!testFunc.isSolved()) { return new int[]{i, j}; } diff --git a/src/org/warp/picalculator/math/rules/methods/MultiplicationMethod1.java b/src/org/warp/picalculator/math/rules/methods/MultiplicationMethod1.java index 842913a6..d4a2b89a 100644 --- a/src/org/warp/picalculator/math/rules/methods/MultiplicationMethod1.java +++ b/src/org/warp/picalculator/math/rules/methods/MultiplicationMethod1.java @@ -27,19 +27,19 @@ public class MultiplicationMethod1 { Function elem2 = elements.get(workingElementCouple[1]); final int size = elements.size(); - Function prec = new Multiplication(null, elem1, elem2); + Function prec = new Multiplication(root, elem1, elem2); elem1.setParent(prec); elem2.setParent(prec); for (int i = size-1; i >= 0; i--) { if (i != workingElementCouple[0] & i != workingElementCouple[1]) { Function a = prec; Function b = elements.get(i); - prec = new Multiplication(null, a, b); + prec = new Multiplication(root, a, b); a.setParent(prec); b.setParent(prec); } } - prec.setParent(f.getParent()); + prec.setParent(root); result = prec; @@ -71,7 +71,7 @@ public class MultiplicationMethod1 { b = elements.get(j); if (i != j) { Function testFunc; - testFunc = new Multiplication(null, a, b); + testFunc = new Multiplication(root, a, b); if (!testFunc.isSolved()) { return new int[]{i, j}; } diff --git a/src/org/warp/picalculator/math/rules/methods/SumMethod1.java b/src/org/warp/picalculator/math/rules/methods/SumMethod1.java index afda8dbe..0cc6294c 100644 --- a/src/org/warp/picalculator/math/rules/methods/SumMethod1.java +++ b/src/org/warp/picalculator/math/rules/methods/SumMethod1.java @@ -31,7 +31,7 @@ public class SumMethod1 { Function elem2 = elements.get(workingElementCouple[1]); final int size = elements.size(); - Function prec = new Sum(null, elem1, elem2); + Function prec = new Sum(root, elem1, elem2); elem1.setParent(prec); elem2.setParent(prec); for (int i = size-1; i >= 0; i--) { @@ -39,21 +39,21 @@ public class SumMethod1 { Function a = prec; Function b = elements.get(i); if (b instanceof Negative) { - prec = new Subtraction(null, a, ((Negative)b).getVariable()); + prec = new Subtraction(root, a, ((Negative)b).getVariable()); a.setParent(prec); ((FunctionTwoValues)prec).getVariable2().setParent(prec); } else if (b instanceof Number && ((Number) b).getTerm().compareTo(BigDecimal.ZERO) < 0) { - prec = new Subtraction(null, a, ((Number)b).multiply(new Number(null, -1))); + prec = new Subtraction(root, a, ((Number)b).multiply(new Number(root, -1))); a.setParent(prec); ((FunctionTwoValues)prec).getVariable2().setParent(prec); } else { - prec = new Sum(null, a, b); + prec = new Sum(root, a, b); a.setParent(prec); b.setParent(prec); } } } - prec.setParent(f.getParent()); + prec.setParent(root); result = prec; @@ -68,7 +68,7 @@ public class SumMethod1 { if (sum instanceof Sum) { elements.add(((FunctionTwoValues) sum).getVariable2()); } else { - elements.add(new Negative(null, ((FunctionTwoValues) sum).getVariable2())); + elements.add(new Negative(root, ((FunctionTwoValues) sum).getVariable2())); } sum = ((FunctionTwoValues) sum).getVariable1(); } @@ -90,15 +90,15 @@ public class SumMethod1 { if (i != j) { Function testFunc; if (b instanceof Negative) { - testFunc = new Subtraction(null, a, ((Negative)b).getVariable()); + testFunc = new Subtraction(root, a, ((Negative)b).getVariable()); } else if (b instanceof Number && ((Number) b).getTerm().compareTo(BigDecimal.ZERO) < 0) { - testFunc = new Subtraction(null, a, ((Number)b).multiply(new Number(null, -1))); + testFunc = new Subtraction(root, a, ((Number)b).multiply(new Number(root, -1))); } else if (a instanceof Negative) { - testFunc = new Subtraction(null, b, ((Negative)a).getVariable()); + testFunc = new Subtraction(root, b, ((Negative)a).getVariable()); } else if (a instanceof Number && ((Number) a).getTerm().compareTo(BigDecimal.ZERO) < 0) { - testFunc = new Subtraction(null, b, ((Number)a).multiply(new Number(null, -1))); + testFunc = new Subtraction(root, b, ((Number)a).multiply(new Number(root, -1))); } else { - testFunc = new Sum(null, a, b); + testFunc = new Sum(root, a, b); } if (!testFunc.isSolved()) { return new int[]{i, j}; diff --git a/src/org/warp/picalculator/screens/ChooseVariableValueScreen.java b/src/org/warp/picalculator/screens/ChooseVariableValueScreen.java index 6a1fbd06..7162086a 100644 --- a/src/org/warp/picalculator/screens/ChooseVariableValueScreen.java +++ b/src/org/warp/picalculator/screens/ChooseVariableValueScreen.java @@ -4,14 +4,10 @@ import static org.warp.picalculator.device.graphicengine.Display.Render.glColor3 import static org.warp.picalculator.device.graphicengine.Display.Render.glColor4i; import static org.warp.picalculator.device.graphicengine.Display.Render.glDrawStringCenter; -import org.warp.picalculator.Error; import org.warp.picalculator.Main; import org.warp.picalculator.device.Keyboard.Key; -import org.warp.picalculator.device.PIDisplay; import org.warp.picalculator.device.graphicengine.Screen; -import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Function; -import org.warp.picalculator.math.functions.Variable; import org.warp.picalculator.math.functions.Variable.VariableValue; public class ChooseVariableValueScreen extends Screen { diff --git a/src/org/warp/picalculator/screens/MathInputScreen.java b/src/org/warp/picalculator/screens/MathInputScreen.java index c32608cb..c8bab1f5 100644 --- a/src/org/warp/picalculator/screens/MathInputScreen.java +++ b/src/org/warp/picalculator/screens/MathInputScreen.java @@ -47,16 +47,12 @@ public class MathInputScreen extends Screen { public volatile int caretPos = 0; public volatile boolean showCaret = true; public volatile float showCaretDelta = 0f; - public ArrayList f; - public ArrayList f2; - public ArrayList variablesValues; - public int resultsCount; + public Calculator calc; public boolean autoscroll; public int scrollX = 0; public int errorLevel = 0; // 0 = nessuno, 1 = risultato, 2 = tutto boolean mustRefresh = true; boolean afterDoNextStep = false; - public final Calculator calc; public MathInputScreen() { super(); @@ -102,12 +98,7 @@ public class MathInputScreen extends Screen { // Funzione f = new Parentesi("5Y+XY=2", "") // calcola("((5^2+3√(100/0.1))+Ⓐ(7)+9/15*2√(26/2))/21"); - if (f == null & f2 == null) { - f = new ArrayList(); - f2 = new ArrayList(); - variablesValues = new ArrayList<>(); - resultsCount = 0; - } + calc.init(); // interpreta("{(5X*(15X/3X))+(25X/(5X*(15X/3X)))=15{X=5"); //TODO RIMUOVERE // long start = System.nanoTime(); @@ -138,22 +129,8 @@ public class MathInputScreen extends Screen { if (!temporary) { equazioneCorrente = eqn; } - ArrayList fncs = new ArrayList(); - if (eqn.length() > 0) { - try { - fncs.add(calc.parseString(eqn.replace("sqrt", "Ⓐ").replace("^", "Ⓑ"))); - } catch (Exception ex) { - - } - } - f = fncs; - for (Function f : f) { - try { - f.generateGraphics(); - } catch (NullPointerException ex) { - throw new Error(Errors.SYNTAX_ERROR); - } - } + + calc.parseInputString(eqn); } @Override @@ -205,9 +182,9 @@ public class MathInputScreen extends Screen { glColor(textColor); PIDisplay.drawSkinPart(Main.screenSize[0]-16, padding+20+fontBig.charH/2-16/2, 304, 0, 304+16, 16); } - if (f != null) { + if (calc.f != null) { int topSpacing = 0; - Iterator iter = f.iterator(); + Iterator iter = calc.f.iterator(); while (iter.hasNext()) { Function fnc = iter.next(); try { @@ -235,14 +212,14 @@ public class MathInputScreen extends Screen { topSpacing += fnc.getHeight() + 2; } } - if (f2 != null) { + if (calc.f2 != null) { int bottomSpacing = 0; - for (Function f : f2) { + for (Function f : calc.f2) { 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(); + if (calc.resultsCount > 1 && calc.resultsCount != calc.f2.size()) { + String resultsCountText = calc.resultsCount+" total results".toUpperCase(); glColor(0xFF9AAEA0); glSetFont(Utils.getFont(true)); bottomSpacing += fontBig.charH+2; @@ -274,7 +251,7 @@ public class MathInputScreen extends Screen { @Override public void run() { equazioneCorrente = nuovaEquazione; - f2 = f; + calc.f2 = calc.f; afterDoNextStep = true; simplify(MathInputScreen.this); } @@ -302,9 +279,9 @@ public class MathInputScreen extends Screen { Utils.debug.println("Resetting after error..."); PIDisplay.error = null; this.equazioneCorrente = null; - this.f = null; - this.f2 = null; - this.resultsCount = 0; + calc.f = null; + calc.f2 = null; + calc.resultsCount = 0; return true; } else { try { @@ -476,15 +453,15 @@ public class MathInputScreen extends Screen { caretPos = 0; nuovaEquazione=""; afterDoNextStep = false; - if (f != null) { - f.clear(); + if (calc.f != null) { + calc.f.clear(); } return true; } case SURD_MODE: calc.exactMode = !calc.exactMode; if (calc.exactMode == false) { - f2 = solveExpression(f2); + calc.f2 = solveExpression(calc.f2); } else { equazioneCorrente = ""; Keyboard.keyPressed(Key.SOLVE); @@ -574,7 +551,7 @@ public class MathInputScreen extends Screen { showVariablesDialog(); ArrayList results = new ArrayList<>(); ArrayList partialResults = new ArrayList<>(); - for (Function f : f2) { + for (Function f : calc.f2) { if (f instanceof Equation) { PIDisplay.INSTANCE.setScreen(new SolveEquationScreen(this)); } else { @@ -587,23 +564,23 @@ public class MathInputScreen extends Screen { partialResults.add(itm); } } - results = new ArrayList(partialResults); + results = new ArrayList<>(partialResults); partialResults.clear(); } } if (results.size() == 0) { - resultsCount = 0; + calc.resultsCount = 0; } else { - resultsCount = results.size(); + calc.resultsCount = results.size(); Collections.reverse(results); // add elements to al, including duplicates Set hs = new LinkedHashSet<>(); hs.addAll(results); results.clear(); results.addAll(hs); - f2 = results; - for (Function rf : f2) { + calc.f2 = results; + for (Function rf : calc.f2) { rf.generateGraphics(); } } @@ -626,26 +603,26 @@ public class MathInputScreen extends Screen { protected void solve() { try { try { - for (Function f : f) { + for (Function f : calc.f) { if (f instanceof Equation) { PIDisplay.INSTANCE.setScreen(new SolveEquationScreen(this)); return; } } - ArrayList results = solveExpression(f); + ArrayList results = solveExpression(calc.f); if (results.size() == 0) { - resultsCount = 0; + calc.resultsCount = 0; } else { - resultsCount = results.size(); + calc.resultsCount = results.size(); Collections.reverse(results); // add elements to al, including duplicates Set hs = new LinkedHashSet<>(); hs.addAll(results); results.clear(); results.addAll(hs); - f2 = results; - for (Function rf : f2) { + calc.f2 = results; + for (Function rf : calc.f2) { rf.generateGraphics(); } } @@ -699,10 +676,14 @@ public class MathInputScreen extends Screen { return false; } + public void showVariablesDialog() { + showVariablesDialog(null); + } + public void showVariablesDialog(final Runnable runnable) { Thread ct = new Thread(()->{ - ArrayList variablesInFunctions = getVariables(f.toArray(new Function[f.size()])); - for (VariableValue f : variablesValues) { + ArrayList variablesInFunctions = getVariables(calc.f.toArray(new Function[calc.f.size()])); + for (VariableValue f : calc.variablesValues) { if (variablesInFunctions.contains(f.v)) { variablesInFunctions.remove(f.v); } @@ -710,7 +691,7 @@ public class MathInputScreen extends Screen { boolean cancelled = false; for (Function f : variablesInFunctions) { - ChooseVariableValueScreen cvs = new ChooseVariableValueScreen(this, new VariableValue((Variable) f, new Number(null, 0))); + ChooseVariableValueScreen cvs = new ChooseVariableValueScreen(this, new VariableValue((Variable) f, new Number(calc, 0))); PIDisplay.INSTANCE.setScreen(cvs); try { while (PIDisplay.screen == cvs) { @@ -722,18 +703,20 @@ public class MathInputScreen extends Screen { cancelled = true; break; } else { - final int is = variablesValues.size(); + final int is = calc.variablesValues.size(); for (int i = 0; i < is; i++) { - if (variablesValues.get(i).v == f) { - variablesValues.remove(i); + if (calc.variablesValues.get(i).v == f) { + calc.variablesValues.remove(i); } } - variablesValues.add(new VariableValue((Variable) f, (Number) cvs.resultNumberValue)); + calc.variablesValues.add(new VariableValue((Variable) f, (Number) cvs.resultNumberValue)); } } if (!cancelled) { - runnable.run(); - Utils.debug.println(f.toString()); + if (runnable != null) { + runnable.run(); + } + Utils.debug.println(calc.f.toString()); } }); ct.setName("Variables user-input queue thread"); @@ -770,14 +753,15 @@ public class MathInputScreen extends Screen { 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.calc.f = Utils.cloner.deepClone(es.calc.f); +// es2.calc.f2 = Utils.cloner.deepClone(es.calc.f2); +// es2.calc.resultsCount = es.calc.resultsCount; es2.autoscroll = es.autoscroll; es2.errorLevel = es.errorLevel; es2.mustRefresh = es.mustRefresh; es2.afterDoNextStep = es.afterDoNextStep; - es2.variablesValues = Utils.cloner.deepClone(es.variablesValues); +// es2.calc.variablesValues = Utils.cloner.deepClone(es.calc.variablesValues); + es2.calc = Utils.cloner.deepClone(es.calc); return es2; } diff --git a/src/org/warp/picalculator/screens/SolveEquationScreen.java b/src/org/warp/picalculator/screens/SolveEquationScreen.java index 684c009d..df8ca635 100644 --- a/src/org/warp/picalculator/screens/SolveEquationScreen.java +++ b/src/org/warp/picalculator/screens/SolveEquationScreen.java @@ -56,13 +56,13 @@ public class SolveEquationScreen extends Screen { case LETTER_X: PIDisplay.INSTANCE.goBack(); try { - Calculator.solveExpression('X'); + es.calc.solveExpression('X'); } catch (Error e) { Screen scr = PIDisplay.INSTANCE.getScreen(); if (scr instanceof MathInputScreen) { MathInputScreen escr = (MathInputScreen) scr; escr.errorLevel = 1; - escr.err2 = e; + //escr.err2 = e; //TODO: What is this variable, and why it doesn't exists? } else { e.printStackTrace(); } From a8508eea8e27a80d14d4b7916c3891eaacdbce0c Mon Sep 17 00:00:00 2001 From: XDrake99 Date: Wed, 18 Jan 2017 17:49:54 +0100 Subject: [PATCH 5/5] Finished fixing all the classes. --- .../picalculator/math/functions/Root.java | 12 +++++----- .../math/functions/RootSquare.java | 8 +++---- .../warp/picalculator/math/functions/Sum.java | 4 ++-- .../picalculator/math/rules/ExpandRule5.java | 5 ++-- .../math/rules/ExponentRule16.java | 10 ++++---- .../math/rules/ExponentRule2.java | 4 ++-- .../math/rules/ExponentRule3.java | 4 ++-- .../math/rules/ExponentRule4.java | 12 ++++++---- .../math/rules/FractionsRule1.java | 24 ++++++++++--------- .../math/rules/FractionsRule2.java | 4 ++-- .../math/rules/FractionsRule3.java | 2 +- .../math/rules/FractionsRule4.java | 4 ++-- .../math/rules/FractionsRule5.java | 23 +++++++++--------- .../picalculator/math/rules/NumberRule1.java | 4 +++- .../picalculator/math/rules/NumberRule2.java | 3 +++ .../picalculator/math/rules/NumberRule3.java | 6 +++-- .../picalculator/math/rules/NumberRule4.java | 6 +++-- .../picalculator/math/rules/NumberRule5.java | 1 + .../picalculator/math/rules/NumberRule6.java | 5 +++- .../picalculator/math/rules/SyntaxRule1.java | 6 +++-- .../picalculator/math/rules/SyntaxRule2.java | 3 ++- .../math/rules/UndefinedRule1.java | 3 +++ .../math/rules/UndefinedRule2.java | 3 +++ .../math/rules/VariableRule1.java | 10 ++++---- .../math/rules/VariableRule2.java | 12 ++++++---- .../math/rules/VariableRule3.java | 12 ++++++---- .../rules/methods/MultiplicationMethod1.java | 11 +++++---- .../math/rules/methods/SumMethod1.java | 23 ++++++++---------- 28 files changed, 129 insertions(+), 95 deletions(-) diff --git a/src/org/warp/picalculator/math/functions/Root.java b/src/org/warp/picalculator/math/functions/Root.java index fa44fc92..359ff25e 100644 --- a/src/org/warp/picalculator/math/functions/Root.java +++ b/src/org/warp/picalculator/math/functions/Root.java @@ -41,10 +41,10 @@ public class Root extends FunctionTwoValues { @Override protected boolean isSolvable() { - if (variable1 instanceof Number & variable2 instanceof Number) { + if (variable1 instanceof Number & variable2 instanceof Number) { if (root.exactMode == false) { - return true; - } + return true; + } try { Number exponent = new Number(root, BigDecimal.ONE); exponent = exponent.divide((Number) variable1); @@ -55,7 +55,7 @@ public class Root extends FunctionTwoValues { } } catch (Exception | Error ex) { ex.printStackTrace(); - } + } } if (variable1 instanceof Number && ((Number)variable1).equals(new Number(root, 2))) { return true; @@ -65,7 +65,7 @@ public class Root extends FunctionTwoValues { @Override public ArrayList solve() throws Error { - ArrayList result = new ArrayList<>(); + ArrayList result = new ArrayList<>(); if (root.exactMode) { if (variable1 instanceof Number && ((Number)variable1).equals(new Number(root, 2))) { result.add(new RootSquare(root, variable2)); @@ -78,7 +78,7 @@ public class Root extends FunctionTwoValues { Number exp = (Number) variable1; Number numb = (Number) variable2; - result.add(numb.pow(new Number(root, 1).divide(exp))); + result.add(numb.pow(new Number(root, 1).divide(exp))); } return result; } diff --git a/src/org/warp/picalculator/math/functions/RootSquare.java b/src/org/warp/picalculator/math/functions/RootSquare.java index 19f42496..7fe74908 100644 --- a/src/org/warp/picalculator/math/functions/RootSquare.java +++ b/src/org/warp/picalculator/math/functions/RootSquare.java @@ -36,8 +36,8 @@ public class RootSquare extends AnteriorFunction { @Override protected boolean isSolvable() { - if (variable instanceof Number) { - if (root.exactMode == false) { + if (variable instanceof Number) { + if (root.exactMode == false) { return true; } try { @@ -57,7 +57,7 @@ public class RootSquare extends AnteriorFunction { @Override public ArrayList solve() throws Error { - ArrayList result = new ArrayList<>(); + ArrayList result = new ArrayList<>(); if (root.exactMode) { Number exponent = new Number(root, BigInteger.ONE); exponent = exponent.divide(new Number(root, 2)); @@ -66,7 +66,7 @@ public class RootSquare extends AnteriorFunction { Number exp = new Number(root, 2); Number numb = (Number) variable; - result.add(numb.pow(new Number(root, 1).divide(exp))); + result.add(numb.pow(new Number(root, 1).divide(exp))); } return result; } diff --git a/src/org/warp/picalculator/math/functions/Sum.java b/src/org/warp/picalculator/math/functions/Sum.java index 6214770a..6df3d953 100644 --- a/src/org/warp/picalculator/math/functions/Sum.java +++ b/src/org/warp/picalculator/math/functions/Sum.java @@ -74,14 +74,14 @@ public class Sum extends FunctionTwoValues { } else if (SumMethod1.compare(this)) { result = SumMethod1.execute(this); } else if (variable1.isSolved() & variable2.isSolved()) { - if ((root.getChild().equals(this))) { + if ((root.getChild().equals(this))) { if (((Number)variable1).term.compareTo(new BigDecimal(2)) == 0 && ((Number)variable2).term.compareTo(new BigDecimal(2)) == 0) { result.add(new Joke(root, Joke.FISH)); return result; } else if (((Number)variable1).term.compareTo(new BigDecimal(20)) == 0 && ((Number)variable2).term.compareTo(new BigDecimal(20)) == 0) { result.add(new Joke(root, Joke.TORNADO)); return result; - } else if (((Number)variable1).term.compareTo(new BigDecimal(29)) == 0 && ((Number)variable2).term.compareTo(new BigDecimal(29)) == 0) { + } else if (((Number)variable1).term.compareTo(new BigDecimal(29)) == 0 && ((Number)variable2).term.compareTo(new BigDecimal(29)) == 0) { result.add(new Joke(root, Joke.SHARKNADO)); return result; } diff --git a/src/org/warp/picalculator/math/rules/ExpandRule5.java b/src/org/warp/picalculator/math/rules/ExpandRule5.java index c8abd2d9..6b1546d3 100644 --- a/src/org/warp/picalculator/math/rules/ExpandRule5.java +++ b/src/org/warp/picalculator/math/rules/ExpandRule5.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Expression; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.Negative; @@ -39,10 +40,10 @@ public class ExpandRule5 { if (f instanceof Negative) { Negative fnc = (Negative) f; - result.add(((Negative)((Expression)fnc.getVariable()).getVariable(0)).getVariable().setParent(root)); + result.add(((Negative)((Expression)fnc.getVariable()).getVariable(0)).getVariable()); } else if (f instanceof Subtraction) { Subtraction fnc = (Subtraction) f; - result.add(((Negative)((Expression)fnc.getVariable2()).getVariable(0)).getVariable().setParent(root)); + result.add(((Negative)((Expression)fnc.getVariable2()).getVariable(0)).getVariable()); } return result; } diff --git a/src/org/warp/picalculator/math/rules/ExponentRule16.java b/src/org/warp/picalculator/math/rules/ExponentRule16.java index 9c08d7e6..30187fc5 100644 --- a/src/org/warp/picalculator/math/rules/ExponentRule16.java +++ b/src/org/warp/picalculator/math/rules/ExponentRule16.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Expression; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.Multiplication; @@ -29,13 +30,14 @@ public class ExponentRule16 { } public static ArrayList execute(Function f) throws Error { + Calculator root = f.getRoot(); ArrayList result = new ArrayList<>(); Multiplication fnc = (Multiplication) f; - Power p = new Power(fnc.getParent(), null, null); - Expression expr = new Expression(p); - Function a = fnc.getVariable1().setParent(expr); + Power p = new Power(fnc.getRoot(), null, null); + Expression expr = new Expression(root); + Function a = fnc.getVariable1(); expr.addFunctionToEnd(a); - Number two = new Number(p, 2); + Number two = new Number(root, 2); p.setVariable1(expr); p.setVariable2(two); result.add(p); diff --git a/src/org/warp/picalculator/math/rules/ExponentRule2.java b/src/org/warp/picalculator/math/rules/ExponentRule2.java index adddb866..3ca5c3cd 100644 --- a/src/org/warp/picalculator/math/rules/ExponentRule2.java +++ b/src/org/warp/picalculator/math/rules/ExponentRule2.java @@ -17,7 +17,7 @@ public class ExponentRule2 { public static boolean compare(Function f) { Power fnc = (Power) f; - if (fnc.getVariable2().equals(new Number(root, 1))) { + if (fnc.getVariable2().equals(new Number(f.getRoot(), 1))) { return true; } return false; @@ -25,7 +25,7 @@ public class ExponentRule2 { public static ArrayList execute(Function f) throws Error { ArrayList result = new ArrayList<>(); - result.add(((Power)f).getVariable1().setParent(root)); + result.add(((Power)f).getVariable1()); return result; } diff --git a/src/org/warp/picalculator/math/rules/ExponentRule3.java b/src/org/warp/picalculator/math/rules/ExponentRule3.java index 9a1f08d0..59652a1f 100644 --- a/src/org/warp/picalculator/math/rules/ExponentRule3.java +++ b/src/org/warp/picalculator/math/rules/ExponentRule3.java @@ -17,7 +17,7 @@ public class ExponentRule3 { public static boolean compare(Function f) { Power fnc = (Power) f; - if (fnc.getVariable2().equals(new Number(root, 0))) { + if (fnc.getVariable2().equals(new Number(f.getRoot(), 0))) { return true; } return false; @@ -25,7 +25,7 @@ public class ExponentRule3 { public static ArrayList execute(Function f) throws Error { ArrayList result = new ArrayList<>(); - result.add(new Number(f.getParent(), 1)); + result.add(new Number(f.getRoot(), 1)); return result; } diff --git a/src/org/warp/picalculator/math/rules/ExponentRule4.java b/src/org/warp/picalculator/math/rules/ExponentRule4.java index 71d9e4c4..73d07bb2 100644 --- a/src/org/warp/picalculator/math/rules/ExponentRule4.java +++ b/src/org/warp/picalculator/math/rules/ExponentRule4.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Expression; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.Multiplication; @@ -26,6 +27,7 @@ public class ExponentRule4 { } public static ArrayList execute(Function f) throws Error { + Calculator root = f.getRoot(); ArrayList result = new ArrayList<>(); Power fnc = (Power) f; Expression expr = (Expression) fnc.getVariable1(); @@ -33,14 +35,14 @@ public class ExponentRule4 { Function a = mult.getVariable1(); Function b = mult.getVariable2(); Number n = (Number) fnc.getVariable2(); - Multiplication retMult = new Multiplication(f.getParent(), null, null); - Power p1 = new Power(retMult, null, null); - Expression e1 = new Expression(p1); + Multiplication retMult = new Multiplication(root, null, null); + Power p1 = new Power(root, null, null); + Expression e1 = new Expression(root); e1.addFunctionToEnd(a); p1.setVariable1(e1); p1.setVariable2(n); - Power p2 = new Power(retMult, null, null); - Expression e2 = new Expression(p2); + Power p2 = new Power(root, null, null); + Expression e2 = new Expression(root); e2.addFunctionToEnd(b); p2.setVariable1(e2); p2.setVariable2(n); diff --git a/src/org/warp/picalculator/math/rules/FractionsRule1.java b/src/org/warp/picalculator/math/rules/FractionsRule1.java index 01c74600..f8b80fdc 100644 --- a/src/org/warp/picalculator/math/rules/FractionsRule1.java +++ b/src/org/warp/picalculator/math/rules/FractionsRule1.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Division; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.Number; @@ -16,25 +17,26 @@ import org.warp.picalculator.math.functions.Number; public class FractionsRule1 { public static boolean compare(Function f) { - Division fnc = (Division) f; - if (fnc.getVariable1() instanceof Number) { - Number numb1 = (Number) fnc.getVariable1(); - if (numb1.equals(new Number(root, 0))) { - if (fnc.getVariable2() instanceof Number) { - Number numb2 = (Number) fnc.getVariable2(); - if (numb2.equals(new Number(root, 0))) { - return false; - } + Calculator root = f.getRoot(); + Division fnc = (Division) f; + if (fnc.getVariable1() instanceof Number) { + Number numb1 = (Number) fnc.getVariable1(); + if (numb1.equals(new Number(root, 0))) { + if (fnc.getVariable2() instanceof Number) { + Number numb2 = (Number) fnc.getVariable2(); + if (numb2.equals(new Number(root, 0))) { + return false; } - return true; } + return true; } + } return false; } public static ArrayList execute(Function f) throws Error { ArrayList result = new ArrayList<>(); - result.add(new Number(f.getParent(), 0)); + result.add(new Number(f.getRoot(), 0)); return result; } diff --git a/src/org/warp/picalculator/math/rules/FractionsRule2.java b/src/org/warp/picalculator/math/rules/FractionsRule2.java index a0e2805b..4c110be7 100644 --- a/src/org/warp/picalculator/math/rules/FractionsRule2.java +++ b/src/org/warp/picalculator/math/rules/FractionsRule2.java @@ -19,7 +19,7 @@ public class FractionsRule2 { Division fnc = (Division) f; if (fnc.getVariable2() instanceof Number) { Number numb = (Number) fnc.getVariable2(); - if (numb.equals(new Number(root, 1))) { + if (numb.equals(new Number(f.getRoot(), 1))) { return true; } } @@ -29,7 +29,7 @@ public class FractionsRule2 { public static ArrayList execute(Function f) throws Error { ArrayList result = new ArrayList<>(); Division fnc = (Division) f; - result.add(fnc.getVariable1().setParent(root)); + result.add(fnc.getVariable1()); return result; } diff --git a/src/org/warp/picalculator/math/rules/FractionsRule3.java b/src/org/warp/picalculator/math/rules/FractionsRule3.java index 639643aa..8dd20916 100644 --- a/src/org/warp/picalculator/math/rules/FractionsRule3.java +++ b/src/org/warp/picalculator/math/rules/FractionsRule3.java @@ -25,7 +25,7 @@ public class FractionsRule3 { public static ArrayList execute(Function f) throws Error { ArrayList result = new ArrayList<>(); - result.add(new Number(f.getParent(), 1)); + result.add(new Number(f.getRoot(), 1)); return result; } diff --git a/src/org/warp/picalculator/math/rules/FractionsRule4.java b/src/org/warp/picalculator/math/rules/FractionsRule4.java index df0872a6..04be9cbd 100644 --- a/src/org/warp/picalculator/math/rules/FractionsRule4.java +++ b/src/org/warp/picalculator/math/rules/FractionsRule4.java @@ -20,7 +20,7 @@ public class FractionsRule4 { Power fnc = (Power) f; if (fnc.getVariable1() instanceof Division && fnc.getVariable2() instanceof Number) { Number n2 = (Number) fnc.getVariable2(); - if (n2.equals(new Number(root, -1))) { + if (n2.equals(new Number(f.getRoot(), -1))) { return true; } } @@ -32,7 +32,7 @@ public class FractionsRule4 { Power fnc = (Power) f; Function a = ((Division)fnc.getVariable1()).getVariable1(); Function b = ((Division)fnc.getVariable1()).getVariable2(); - Division res = new Division(f.getParent(), b, a); + Division res = new Division(f.getRoot(), b, a); result.add(res); return result; } diff --git a/src/org/warp/picalculator/math/rules/FractionsRule5.java b/src/org/warp/picalculator/math/rules/FractionsRule5.java index 0cc8b0c0..f7092df0 100644 --- a/src/org/warp/picalculator/math/rules/FractionsRule5.java +++ b/src/org/warp/picalculator/math/rules/FractionsRule5.java @@ -1,11 +1,12 @@ -package org.warp.picalculator.math.rules; -import java.math.BigDecimal; -import java.util.ArrayList; - -import org.warp.picalculator.Error; -import org.warp.picalculator.math.functions.Division; -import org.warp.picalculator.math.functions.Function; -import org.warp.picalculator.math.functions.Number; +package org.warp.picalculator.math.rules; +import java.math.BigDecimal; +import java.util.ArrayList; + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; +import org.warp.picalculator.math.functions.Division; +import org.warp.picalculator.math.functions.Function; +import org.warp.picalculator.math.functions.Number; import org.warp.picalculator.math.functions.Power; /** @@ -20,7 +21,7 @@ public class FractionsRule5 { Power fnc = (Power) f; if (fnc.getVariable1() instanceof Division && fnc.getVariable2() instanceof Number) { Number n2 = (Number) fnc.getVariable2(); - if (n2.getTerm().compareTo(BigDecimal.ZERO) < 0) { + if (n2.getTerm().compareTo(BigDecimal.ZERO) < 0) { return true; } } @@ -28,14 +29,14 @@ public class FractionsRule5 { } public static ArrayList execute(Function f) throws Error { + Calculator root = f.getRoot(); 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(root, "-1")); Division dv = new Division(root, b, a); - Power pow = new Power(f.getParent(), dv, c); - dv.setParent(pow); + Power pow = new Power(root, dv, c); result.add(pow); return result; } diff --git a/src/org/warp/picalculator/math/rules/NumberRule1.java b/src/org/warp/picalculator/math/rules/NumberRule1.java index dbddb6c7..9e3e00ff 100644 --- a/src/org/warp/picalculator/math/rules/NumberRule1.java +++ b/src/org/warp/picalculator/math/rules/NumberRule1.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.Multiplication; import org.warp.picalculator.math.functions.Number; @@ -16,6 +17,7 @@ import org.warp.picalculator.math.functions.Number; public class NumberRule1 { public static boolean compare(Function f) { + Calculator root = f.getRoot(); Multiplication mult = (Multiplication) f; if (mult.getVariable1() instanceof Number) { Number numb = (Number) mult.getVariable1(); @@ -34,7 +36,7 @@ public class NumberRule1 { public static ArrayList execute(Function f) throws Error { ArrayList result = new ArrayList<>(); - result.add(new Number(f.getParent(), "0")); + result.add(new Number(f.getRoot(), "0")); return result; } diff --git a/src/org/warp/picalculator/math/rules/NumberRule2.java b/src/org/warp/picalculator/math/rules/NumberRule2.java index 31077cc8..e7f23c7d 100644 --- a/src/org/warp/picalculator/math/rules/NumberRule2.java +++ b/src/org/warp/picalculator/math/rules/NumberRule2.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.Multiplication; import org.warp.picalculator.math.functions.Number; @@ -16,6 +17,7 @@ import org.warp.picalculator.math.functions.Number; public class NumberRule2 { public static boolean compare(Function f) { + Calculator root = f.getRoot(); Multiplication mult = (Multiplication) f; if (mult.getVariable1() instanceof Number) { Number numb = (Number) mult.getVariable1(); @@ -33,6 +35,7 @@ public class NumberRule2 { } public static ArrayList execute(Function f) throws Error { + Calculator root = f.getRoot(); ArrayList result = new ArrayList<>(); Function a = null; boolean aFound = false; diff --git a/src/org/warp/picalculator/math/rules/NumberRule3.java b/src/org/warp/picalculator/math/rules/NumberRule3.java index 1397821d..e69d18bf 100644 --- a/src/org/warp/picalculator/math/rules/NumberRule3.java +++ b/src/org/warp/picalculator/math/rules/NumberRule3.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.Multiplication; import org.warp.picalculator.math.functions.Negative; @@ -45,14 +46,15 @@ public class NumberRule3 { } public static ArrayList execute(Function f) throws Error { + Calculator root = f.getRoot(); ArrayList result = new ArrayList<>(); if (f instanceof SumSubtraction) { - Multiplication mul = new Multiplication(f.getParent(), null, null); + Multiplication mul = new Multiplication(root, null, null); mul.setVariable1(new Number(root, 2)); mul.setVariable2(f); result.add(mul); } - result.add(new Number(f.getParent(), 0)); + result.add(new Number(root, 0)); return result; } diff --git a/src/org/warp/picalculator/math/rules/NumberRule4.java b/src/org/warp/picalculator/math/rules/NumberRule4.java index 9b8506a1..9e9e9bf4 100644 --- a/src/org/warp/picalculator/math/rules/NumberRule4.java +++ b/src/org/warp/picalculator/math/rules/NumberRule4.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.Subtraction; import org.warp.picalculator.math.functions.Sum; @@ -24,10 +25,11 @@ public class NumberRule4 { } public static ArrayList execute(Function f) throws Error { + Calculator root = f.getRoot(); ArrayList result = new ArrayList<>(); SumSubtraction ss = (SumSubtraction) f; - result.add(new Sum(f.getParent(), ss.getVariable1(), ss.getVariable2())); - result.add(new Subtraction(f.getParent(), ss.getVariable1(), ss.getVariable2())); + result.add(new Sum(root, ss.getVariable1(), ss.getVariable2())); + result.add(new Subtraction(root, ss.getVariable1(), ss.getVariable2())); return result; } diff --git a/src/org/warp/picalculator/math/rules/NumberRule5.java b/src/org/warp/picalculator/math/rules/NumberRule5.java index dc2ddcfc..9bdf90a7 100644 --- a/src/org/warp/picalculator/math/rules/NumberRule5.java +++ b/src/org/warp/picalculator/math/rules/NumberRule5.java @@ -22,6 +22,7 @@ import org.warp.picalculator.math.functions.Number; public class NumberRule5 { public static boolean compare(Function f) { + Calculator root = f.getRoot(); FunctionTwoValues fnc = (FunctionTwoValues) f; if (fnc.getVariable1().equals(new Number(root, 0)) || fnc.getVariable2().equals(new Number(root, 0))) { return true; diff --git a/src/org/warp/picalculator/math/rules/NumberRule6.java b/src/org/warp/picalculator/math/rules/NumberRule6.java index a192807c..998e4546 100644 --- a/src/org/warp/picalculator/math/rules/NumberRule6.java +++ b/src/org/warp/picalculator/math/rules/NumberRule6.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.Multiplication; import org.warp.picalculator.math.functions.Negative; @@ -17,6 +18,7 @@ import org.warp.picalculator.math.functions.Number; public class NumberRule6 { public static boolean compare(Function f) { + Calculator root = f.getRoot(); Multiplication mult = (Multiplication) f; if (mult.getVariable1() instanceof Number) { Number numb = (Number) mult.getVariable1(); @@ -34,6 +36,7 @@ public class NumberRule6 { } public static ArrayList execute(Function f) throws Error { + Calculator root = f.getRoot(); ArrayList result = new ArrayList<>(); Function a = null; boolean aFound = false; @@ -53,7 +56,7 @@ public class NumberRule6 { } } - Negative minus = new Negative(f.getParent(), null); + Negative minus = new Negative(root, null); minus.setVariable(a); result.add(minus); diff --git a/src/org/warp/picalculator/math/rules/SyntaxRule1.java b/src/org/warp/picalculator/math/rules/SyntaxRule1.java index 0879a772..86ddbd47 100644 --- a/src/org/warp/picalculator/math/rules/SyntaxRule1.java +++ b/src/org/warp/picalculator/math/rules/SyntaxRule1.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.FunctionTwoValues; import org.warp.picalculator.math.functions.Multiplication; @@ -27,6 +28,7 @@ public class SyntaxRule1 { } public static ArrayList execute(Function f) throws Error { + Calculator root = f.getRoot(); ArrayList result = new ArrayList<>(); FunctionTwoValues mOut = (FunctionTwoValues) f; Function a = ((FunctionTwoValues)mOut.getVariable1()).getVariable1(); @@ -34,9 +36,9 @@ public class SyntaxRule1 { Function c = mOut.getVariable2(); FunctionTwoValues mIn; if (f instanceof Multiplication) { - mIn = new Multiplication(mOut, null, null); + mIn = new Multiplication(root, null, null); } else { - mIn = new Sum(mOut, null, null); + mIn = new Sum(root, null, null); } mOut.setVariable1(a); mIn.setVariable1(b); diff --git a/src/org/warp/picalculator/math/rules/SyntaxRule2.java b/src/org/warp/picalculator/math/rules/SyntaxRule2.java index 131b5df3..e53d7aab 100644 --- a/src/org/warp/picalculator/math/rules/SyntaxRule2.java +++ b/src/org/warp/picalculator/math/rules/SyntaxRule2.java @@ -29,6 +29,7 @@ public class SyntaxRule2 { } public static ArrayList execute(Sum f) throws Error { + Calculator root = f.getRoot(); ArrayList result = new ArrayList<>(); Function a = f.getVariable1(); Function b, c; @@ -39,7 +40,7 @@ public class SyntaxRule2 { b = ((Sum)((Expression)f.getVariable2()).getVariable(0)).getVariable1(); c = ((Sum)((Expression)f.getVariable2()).getVariable(0)).getVariable2(); } - Sum mIn = new Sum(f, null, null); + Sum mIn = new Sum(root, null, null); f.setVariable1(mIn); mIn.setVariable1(a); mIn.setVariable2(b); diff --git a/src/org/warp/picalculator/math/rules/UndefinedRule1.java b/src/org/warp/picalculator/math/rules/UndefinedRule1.java index 59466edc..31e102ef 100644 --- a/src/org/warp/picalculator/math/rules/UndefinedRule1.java +++ b/src/org/warp/picalculator/math/rules/UndefinedRule1.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.Number; import org.warp.picalculator.math.functions.Power; @@ -17,6 +18,7 @@ import org.warp.picalculator.math.functions.Undefined; public class UndefinedRule1 { public static boolean compare(Function f) { + Calculator root = f.getRoot(); Power fnc = (Power) f; if (fnc.getVariable1().equals(new Number(root, 0)) && fnc.getVariable2().equals(new Number(root, 0))) { return true; @@ -25,6 +27,7 @@ public class UndefinedRule1 { } public static ArrayList execute(Function f) throws Error { + Calculator root = f.getRoot(); ArrayList result = new ArrayList<>(); result.add(new Undefined(root)); return result; diff --git a/src/org/warp/picalculator/math/rules/UndefinedRule2.java b/src/org/warp/picalculator/math/rules/UndefinedRule2.java index 38c921e3..b202a23f 100644 --- a/src/org/warp/picalculator/math/rules/UndefinedRule2.java +++ b/src/org/warp/picalculator/math/rules/UndefinedRule2.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Division; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.Number; @@ -17,6 +18,7 @@ import org.warp.picalculator.math.functions.Undefined; public class UndefinedRule2 { public static boolean compare(Function f) { + Calculator root = f.getRoot(); Division fnc = (Division) f; if (fnc.getVariable2() instanceof Number) { Number numb = (Number) fnc.getVariable2(); @@ -28,6 +30,7 @@ public class UndefinedRule2 { } public static ArrayList execute(Function f) throws Error { + Calculator root = f.getRoot(); ArrayList result = new ArrayList<>(); result.add(new Undefined(root)); return result; diff --git a/src/org/warp/picalculator/math/rules/VariableRule1.java b/src/org/warp/picalculator/math/rules/VariableRule1.java index c6f13886..d1c324af 100644 --- a/src/org/warp/picalculator/math/rules/VariableRule1.java +++ b/src/org/warp/picalculator/math/rules/VariableRule1.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Expression; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.FunctionTwoValues; @@ -30,6 +31,7 @@ public class VariableRule1 { } public static ArrayList execute(FunctionTwoValues fnc) throws Error { + Calculator root = fnc.getRoot(); ArrayList result = new ArrayList<>(); Multiplication m1 = (Multiplication) fnc.getVariable1(); Multiplication m2 = (Multiplication) fnc.getVariable2(); @@ -37,13 +39,13 @@ public class VariableRule1 { Function b = m2.getVariable1(); Function x = m1.getVariable2(); - Multiplication retm = new Multiplication(fnc.getParent(), null, null); - Expression rete = new Expression(retm); + Multiplication retm = new Multiplication(root, null, null); + Expression rete = new Expression(root); FunctionTwoValues rets; if (fnc instanceof Sum){ - rets = new Sum(rete, null, null); + rets = new Sum(root, null, null); } else { - rets = new Subtraction(rete, null, null); + rets = new Subtraction(root, null, null); } rets.setVariable1(a); rets.setVariable2(b); diff --git a/src/org/warp/picalculator/math/rules/VariableRule2.java b/src/org/warp/picalculator/math/rules/VariableRule2.java index 3af3f9bd..b0a8df7c 100644 --- a/src/org/warp/picalculator/math/rules/VariableRule2.java +++ b/src/org/warp/picalculator/math/rules/VariableRule2.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Expression; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.FunctionTwoValues; @@ -30,22 +31,23 @@ public class VariableRule2 { } public static ArrayList execute(FunctionTwoValues fnc) throws Error { + Calculator root = fnc.getRoot(); ArrayList result = new ArrayList<>(); Multiplication m1 = (Multiplication) fnc.getVariable1(); Function a = m1.getVariable1(); Function x = fnc.getVariable2(); - Multiplication retm = new Multiplication(fnc.getParent(), null, null); - Expression rete = new Expression(retm); + Multiplication retm = new Multiplication(root, null, null); + Expression rete = new Expression(root); FunctionTwoValues rets; if (fnc instanceof Sum) { - rets = new Sum(rete, null, null); + rets = new Sum(root, null, null); } else { - rets = new Subtraction(rete, null, null); + rets = new Subtraction(root, null, null); } rets.setVariable1(a); - rets.setVariable2(new Number(rets, 1)); + rets.setVariable2(new Number(root, 1)); rete.addFunctionToEnd(rets); retm.setVariable1(rete); retm.setVariable2(x); diff --git a/src/org/warp/picalculator/math/rules/VariableRule3.java b/src/org/warp/picalculator/math/rules/VariableRule3.java index b9df4fd2..6ea0a3cd 100644 --- a/src/org/warp/picalculator/math/rules/VariableRule3.java +++ b/src/org/warp/picalculator/math/rules/VariableRule3.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Expression; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.FunctionTwoValues; @@ -30,21 +31,22 @@ public class VariableRule3 { } public static ArrayList execute(FunctionTwoValues fnc) throws Error { + Calculator root = fnc.getRoot(); ArrayList result = new ArrayList<>(); Multiplication m2 = (Multiplication) fnc.getVariable2(); Function a = m2.getVariable1(); Function x = fnc.getVariable1(); - Multiplication retm = new Multiplication(fnc.getParent(), null, null); - Expression rete = new Expression(retm); + Multiplication retm = new Multiplication(root, null, null); + Expression rete = new Expression(root); FunctionTwoValues rets; if (fnc instanceof Sum) { - rets = new Sum(rete, null, null); + rets = new Sum(root, null, null); } else { - rets = new Subtraction(rete, null, null); + rets = new Subtraction(root, null, null); } - rets.setVariable1(new Number(rets, 1)); + rets.setVariable1(new Number(root, 1)); rets.setVariable2(a); rete.addFunctionToEnd(rets); retm.setVariable1(rete); diff --git a/src/org/warp/picalculator/math/rules/methods/MultiplicationMethod1.java b/src/org/warp/picalculator/math/rules/methods/MultiplicationMethod1.java index d4a2b89a..79596c0c 100644 --- a/src/org/warp/picalculator/math/rules/methods/MultiplicationMethod1.java +++ b/src/org/warp/picalculator/math/rules/methods/MultiplicationMethod1.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules.methods; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.Multiplication; import org.warp.picalculator.math.functions.Number; @@ -21,6 +22,7 @@ public class MultiplicationMethod1 { public static ArrayList execute(Function f) throws Error { Function result; + Calculator root = f.getRoot(); ArrayList elements = getMultiplicationElements(f); int[] workingElementCouple = getFirstWorkingMultiplicationCouple(elements); Function elem1 = elements.get(workingElementCouple[0]); @@ -28,18 +30,13 @@ public class MultiplicationMethod1 { final int size = elements.size(); Function prec = new Multiplication(root, elem1, elem2); - elem1.setParent(prec); - elem2.setParent(prec); for (int i = size-1; i >= 0; i--) { if (i != workingElementCouple[0] & i != workingElementCouple[1]) { Function a = prec; Function b = elements.get(i); prec = new Multiplication(root, a, b); - a.setParent(prec); - b.setParent(prec); } } - prec.setParent(root); result = prec; @@ -62,9 +59,13 @@ public class MultiplicationMethod1 { final int size = elements.size(); Function a; Function b; + if (elements.size() == 0) { + return null; + } if (elements.size() == 2) { return null; } + Calculator root = elements.get(0).getRoot(); for (int i = 0; i < size; i++) { a = elements.get(i); for (int j = 0; j < size; j++) { diff --git a/src/org/warp/picalculator/math/rules/methods/SumMethod1.java b/src/org/warp/picalculator/math/rules/methods/SumMethod1.java index 0cc6294c..6f5466a4 100644 --- a/src/org/warp/picalculator/math/rules/methods/SumMethod1.java +++ b/src/org/warp/picalculator/math/rules/methods/SumMethod1.java @@ -4,6 +4,7 @@ import java.math.BigDecimal; import java.util.ArrayList; import org.warp.picalculator.Error; +import org.warp.picalculator.math.Calculator; import org.warp.picalculator.math.functions.Function; import org.warp.picalculator.math.functions.FunctionTwoValues; import org.warp.picalculator.math.functions.Negative; @@ -20,40 +21,35 @@ import org.warp.picalculator.math.functions.Sum; public class SumMethod1 { public static boolean compare(Function f) { - return (f instanceof Sum || f instanceof Subtraction) && ((FunctionTwoValues)f).getVariable1().isSolved() && ((FunctionTwoValues)f).getVariable2().isSolved() && !(((FunctionTwoValues)f).getVariable1() instanceof Number && ((FunctionTwoValues)f).getVariable2() instanceof Number) && getFirstWorkingSumCouple(getSumElements(f)) != null; + Calculator root = f.getRoot(); + return (f instanceof Sum || f instanceof Subtraction) && ((FunctionTwoValues)f).getVariable1().isSolved() && ((FunctionTwoValues)f).getVariable2().isSolved() && !(((FunctionTwoValues)f).getVariable1() instanceof Number && ((FunctionTwoValues)f).getVariable2() instanceof Number) && getFirstWorkingSumCouple(root, getSumElements(f)) != null; } public static ArrayList execute(Function f) throws Error { Function result; + Calculator root = f.getRoot(); ArrayList elements = getSumElements(f); - int[] workingElementCouple = getFirstWorkingSumCouple(elements); + int[] workingElementCouple = getFirstWorkingSumCouple(root, elements); Function elem1 = elements.get(workingElementCouple[0]); Function elem2 = elements.get(workingElementCouple[1]); final int size = elements.size(); Function prec = new Sum(root, elem1, elem2); - elem1.setParent(prec); - elem2.setParent(prec); for (int i = size-1; i >= 0; i--) { if (i != workingElementCouple[0] & i != workingElementCouple[1]) { Function a = prec; Function b = elements.get(i); if (b instanceof Negative) { prec = new Subtraction(root, a, ((Negative)b).getVariable()); - a.setParent(prec); - ((FunctionTwoValues)prec).getVariable2().setParent(prec); + ((FunctionTwoValues)prec).getVariable2(); } else if (b instanceof Number && ((Number) b).getTerm().compareTo(BigDecimal.ZERO) < 0) { prec = new Subtraction(root, a, ((Number)b).multiply(new Number(root, -1))); - a.setParent(prec); - ((FunctionTwoValues)prec).getVariable2().setParent(prec); + ((FunctionTwoValues)prec).getVariable2(); } else { prec = new Sum(root, a, b); - a.setParent(prec); - b.setParent(prec); } } } - prec.setParent(root); result = prec; @@ -63,6 +59,7 @@ public class SumMethod1 { } private static ArrayList getSumElements(Function sum) { + Calculator root = sum.getRoot(); ArrayList elements = new ArrayList<>(); while (sum instanceof Sum || sum instanceof Subtraction) { if (sum instanceof Sum) { @@ -76,7 +73,7 @@ public class SumMethod1 { return elements; } - private static int[] getFirstWorkingSumCouple(ArrayList elements) { + private static int[] getFirstWorkingSumCouple(Calculator root, ArrayList elements) { final int size = elements.size(); Function a; Function b; @@ -95,7 +92,7 @@ public class SumMethod1 { testFunc = new Subtraction(root, a, ((Number)b).multiply(new Number(root, -1))); } else if (a instanceof Negative) { testFunc = new Subtraction(root, b, ((Negative)a).getVariable()); - } else if (a instanceof Number && ((Number) a).getTerm().compareTo(BigDecimal.ZERO) < 0) { + } else if (a instanceof Number && ((Number) a).getTerm().compareTo(BigDecimal.ZERO) < 0) { testFunc = new Subtraction(root, b, ((Number)a).multiply(new Number(root, -1))); } else { testFunc = new Sum(root, a, b);