From c70430542780ccdc90f1eceed659d016951aa2bd Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 15 Apr 2018 21:28:41 +0200 Subject: [PATCH] exo6: add the full implementation. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- exo6/Chiffre.java | 8 +- exo6/Expression.java | 2 +- exo6/Main.java | 31 ++++++ exo6/Makefile | 3 +- exo6/NotDigitException.java | 24 +++++ exo6/Opdiv.java | 2 +- exo6/Opminus.java | 2 +- exo6/Opmulti.java | 2 +- exo6/Opplus.java | 2 +- exo6/Parenthese.java | 7 -- exo6/ParentheseExp.java | 12 +++ exo6/Variable.java | 32 +++--- exo6/design_arithmetic.pdf | Bin 0 -> 3053 bytes exo6/design_arithmetic.uxf | 192 +++++++++++++++++++----------------- 14 files changed, 200 insertions(+), 119 deletions(-) create mode 100644 exo6/NotDigitException.java delete mode 100644 exo6/Parenthese.java create mode 100644 exo6/ParentheseExp.java create mode 100644 exo6/design_arithmetic.pdf diff --git a/exo6/Chiffre.java b/exo6/Chiffre.java index be6f881..a040ee9 100644 --- a/exo6/Chiffre.java +++ b/exo6/Chiffre.java @@ -14,7 +14,11 @@ class Chiffre extends Facteur { private boolean isChiffre() { return Character.isDigit(c); } - public double evaluer() { - return 0.0; + public double evaluer() throws NotDigitException { + if(isChiffre()) { + return Character.getNumericValue(c); + } else { + throw new NotDigitException(c + " is not a digit"); + } } } diff --git a/exo6/Expression.java b/exo6/Expression.java index dc8a855..22296ab 100644 --- a/exo6/Expression.java +++ b/exo6/Expression.java @@ -1,5 +1,5 @@ public abstract class Expression { - abstract double evaluer(); + abstract double evaluer() throws NotDigitException; } diff --git a/exo6/Main.java b/exo6/Main.java index 5bc0ec4..4b714af 100644 --- a/exo6/Main.java +++ b/exo6/Main.java @@ -1,3 +1,4 @@ +import java.util.Map.Entry; class Main { @@ -6,5 +7,35 @@ class Main { * @param String[] args main() function arguments array */ public static void main(String[] args) { + System.out.println("-----"); + String exp = "5 + x"; + try { + Variable x = new Variable(); + Expression ArithmeticExpression = new Opplus(new Chiffre('5'), x); + while (true) { + Entry e = x.getFirstEntry(); + System.out.println("x = " + e.getValue() + " :"); + System.out.println(exp + " = " + ArithmeticExpression.evaluer()); + } + } + catch (Exception e) {} + + System.out.println("-----"); + exp = "(3 + 2*x)*(7/y - 2) - 5/(3*z + 4)"; + try { + Variable x = new Variable(); + Variable y = new Variable(); + Variable z = new Variable(); + Expression ParentheseOne = new ParentheseExp(new Opplus(new Chiffre('3'), new Opmulti(new Chiffre('2'), x))); + Expression ParentheseTwo = new ParentheseExp(new Opminus(new Opdiv(new Chiffre('7'), y), new Chiffre('2'))); + Expression ParentheseThree = new ParentheseExp(new Opplus(new Opmulti(new Chiffre('3'), z), new Chiffre('4'))); + Expression ArithmeticExpression = new Opminus(new Opmulti((Facteur)ParentheseOne, (Terme)ParentheseTwo), new Opdiv(new Chiffre('5'), (Terme)ParentheseThree)); + while (true) { + Entry e = x.getFirstEntry(); + System.out.println("x = y = z = " + e.getValue() + " :"); + System.out.println(exp + " = " + ArithmeticExpression.evaluer()); + } + } + catch (Exception e) {} } } diff --git a/exo6/Makefile b/exo6/Makefile index d8399cf..4c0ea48 100644 --- a/exo6/Makefile +++ b/exo6/Makefile @@ -47,6 +47,7 @@ JVM = java CLASSES = \ NotCharacterException.java \ + NotDigitException.java \ Expression.java \ Terme.java \ Opadd.java \ @@ -58,7 +59,7 @@ CLASSES = \ Opdiv.java \ Chiffre.java \ Variable.java \ - Parenthese.java \ + ParentheseExp.java \ Main.java # diff --git a/exo6/NotDigitException.java b/exo6/NotDigitException.java new file mode 100644 index 0000000..0cd91fd --- /dev/null +++ b/exo6/NotDigitException.java @@ -0,0 +1,24 @@ + +public class NotDigitException extends Exception { + + public NotDigitException() { + super(); + // TODO Auto-generated constructor stub + } + + public NotDigitException(String message) { + super(message); + // TODO Auto-generated constructor stub + } + + public NotDigitException(Throwable cause) { + super(cause); + // TODO Auto-generated constructor stub + } + + public NotDigitException(String message, Throwable cause) { + super(message, cause); + // TODO Auto-generated constructor stub + } + +} diff --git a/exo6/Opdiv.java b/exo6/Opdiv.java index 0fe9718..dd85ebc 100644 --- a/exo6/Opdiv.java +++ b/exo6/Opdiv.java @@ -5,7 +5,7 @@ class Opdiv extends Opmul { super(g, d); } - public double evaluer() { + public double evaluer() throws NotDigitException { return gauche.evaluer() / droite.evaluer(); } } diff --git a/exo6/Opminus.java b/exo6/Opminus.java index 9e5307a..190cc88 100644 --- a/exo6/Opminus.java +++ b/exo6/Opminus.java @@ -5,7 +5,7 @@ class Opminus extends Opadd { super(g, d); } - public double evaluer() { + public double evaluer() throws NotDigitException { return gauche.evaluer() - droite.evaluer(); } } diff --git a/exo6/Opmulti.java b/exo6/Opmulti.java index fdb93fd..13e7c81 100644 --- a/exo6/Opmulti.java +++ b/exo6/Opmulti.java @@ -5,7 +5,7 @@ class Opmulti extends Opmul { super(g, d); } - public double evaluer() { + public double evaluer() throws NotDigitException { return gauche.evaluer() * droite.evaluer(); } } diff --git a/exo6/Opplus.java b/exo6/Opplus.java index 95b200b..185e66b 100644 --- a/exo6/Opplus.java +++ b/exo6/Opplus.java @@ -5,7 +5,7 @@ class Opplus extends Opadd { super(g, d); } - public double evaluer() { + public double evaluer() throws NotDigitException { return gauche.evaluer() + droite.evaluer(); } } diff --git a/exo6/Parenthese.java b/exo6/Parenthese.java deleted file mode 100644 index 2be283b..0000000 --- a/exo6/Parenthese.java +++ /dev/null @@ -1,7 +0,0 @@ - -class Parenthese extends Facteur { - - public double evaluer() { - return 0.0; - } -} diff --git a/exo6/ParentheseExp.java b/exo6/ParentheseExp.java new file mode 100644 index 0000000..a4d9d7e --- /dev/null +++ b/exo6/ParentheseExp.java @@ -0,0 +1,12 @@ + +class ParentheseExp extends Facteur { + private Expression exp; + + ParentheseExp(Expression e) { + exp = e; + } + + public double evaluer() throws NotDigitException { + return exp.evaluer(); + } +} diff --git a/exo6/Variable.java b/exo6/Variable.java index 30b7830..807ec8d 100644 --- a/exo6/Variable.java +++ b/exo6/Variable.java @@ -1,24 +1,32 @@ import java.util.TreeMap; +import java.util.Map.Entry; class Variable extends Terme { - TreeMap v; + //TODO: TreeMap might look overkill + TreeMap v; - Variable(Character c) throws NotCharacterException { - v = new TreeMap(); - //TODO?: remove extragenous whitespace - if (Character.isLetterOrDigit(c)) { - v.put(c, 0.0); // we suppose the default variable value is 0 - } else { - throw new NotCharacterException(c + " is not a character type"); + Variable() { + v = new TreeMap(); + fill(); + } + + private void fill() { + int i = 0; + for (double d = -5.0; d <= 5.0; d = d + 0.25) { + v.put(i, d); + i++; } } - private boolean isVariable() { - //FIXME: this cover more than latin alphabet - return Character.isLetter(v.firstKey()); + public Entry getFirstEntry() { + return v.firstEntry(); + } + + public Integer size() { + return v.size(); } public double evaluer() { - return 0.0; + return v.pollFirstEntry().getValue(); } } diff --git a/exo6/design_arithmetic.pdf b/exo6/design_arithmetic.pdf new file mode 100644 index 0000000000000000000000000000000000000000..0f1b33b0515d881b3d710e74e3147096477c434b GIT binary patch literal 3053 zcmb7Gc|4T)AMYR<)|J#cY7%3HnA>d%!%(iBapZ282V=)f=8z-WoHLapg~TMWqL@mE zQrXJNO*F18NA5x;+S1m}*naD`yRX;pc>el6@6Y%1{=T30=kt2LA8i-Btv=EK1JWLQ z(b)sSf)QXwKq$!E9Bxacu>mIBmPTd+cp#8L0pJb*J(wK=Mw%EJ;&3cB6Cj6yBJ#`e#Mak;S=BB36a-7j{r`nvCo`cz5nqNoWt@bI~Dj|1S8D}$OM2wKd9~f3QI)({O zrG25guAqBY`t7>!Q9pk=P?a(=BIo14Ih^k6spF*`=m?iy;ufbty<{}?s$-Tv@@ad0 z8rF>s&svuwZdNF*|6E{bkeC?rh+9Kg|TT@b&a~j?)AGb z+>JlIVRzc0wm04brt6=_=W`1A1%^z>&WdY7l0c`t zgvy~d4(v;&>gkni4-b-OX!(_cR~#mD5M|xw0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`5%5g;dSO5x@kb-JInGY>q)B(x6U3x zD?nW#1k3l8PIrcnOe+{wG+(n+ZfE{drOb#)JNs>JRL`3u-t$9bH#2Q2t23Tm8Td`+TbfJien`^p{w>9@ zlgsh%&RyUn58o&)mU=oi(cgT&?qk69DVhGe&IGRf%xl58^itw$JN);SbcbJN!W}Z- zFJ0EitQ%~gK$x!$M+SeT%|_jQT;`3v^WGlwMssFy!4Ff}B;WTXMBgr*-o|bpc`J~L zotmJz-K)96P7S|QtrY% zwy0$MB!DuTI?@x|SJK&~4!Z_xeRyi1*D)7Y)F#V9LK;7v(EfSZBo=a&Y;I>a*D^}f z9P?q2X!(ndHP1iS=XWLsRPPqv-CT(Dlnw-25>vQ-*YiNBn|J*-yGy3Fv18Cw&x9Wc0$=twz$x!6tnMbm!M8dG`|<`cOeG@a3a8BaF~GrOEebPM6wAawrD00 ztqCL~7`eKaKth30;uQ`Dw-wQd=LaMhy&AJcfiZuCL_rraQ*;E_a5drxP^e@pMx-}F z^om3pBfwY;!WV}Vs}yS!=YO3>NSpw+SdUl~@(*d^bKq8F7O=Xo185Ndn;J-l+t5YB z3Y8uV_n^`(=`8BM-q(1=a{g~#edhogHJk(d5By?JzVZi>!HBhk#TKmvkZS?4A729p zs1%mBxGF^7wGy!4)(noww|`49UQ1c6lr@=6rZIw7E5`c6;5B}ewatzU3SLw`DBcu> z5Lu2vVo^vG8jICKpmY!j9T?nNG}p1I4ElfG<-%l8IDr5YN_7Py*#a=u0Bc|fE<+&@ z2z?PT7QBjs2}}m(Fxbf*>;ttVxH#xX08EyM+Xn_)6Bo$@fNnKQT6wOr-%#@fdAS4`P7D_7mauc(AuQ0)@gFQ-A;rHV6?!CI UMLClass - 590 - 60 - 100 + 580 + 90 + 140 60 /Expression/ -- -- -/+evaluer():/ +/+evaluer():double/ UMLClass - 380 - 200 + 390 + 230 100 30 @@ -29,19 +29,19 @@ UMLClass - 870 - 200 + 880 + 230 100 30 - /Op-add/ + /Opadd/ UMLClass - 280 - 290 + 290 + 320 100 30 @@ -51,81 +51,85 @@ UMLClass - 770 - 290 - 100 - 50 + 760 + 320 + 150 + 80 - Op+ + Opplus -- -- -+evaluer():gauche.evaluer() + droite.evaluer() ++evaluer(): +gauche.evaluer() + +droite.evaluer() UMLClass - 480 - 290 + 490 + 320 100 30 - /Op-mul/ + /Opmul/ UMLClass - 970 - 290 - 100 - 50 + 960 + 320 + 140 + 80 - Op- + Opminus -- -- -+evaluer():gauche.evaluer() - droite.evaluer() ++evaluer(): +gauche.evaluer() - +droite.evaluer() UMLClass 10 - 470 - 130 + 510 + 150 80 Chiffre -- -c:int <- {0,...,9} -- -+evaluer():int ++evaluer():double UMLClass - 170 - 470 - 180 + 190 + 510 + 220 80 Variable -- --vMap:Map<key, value> +-v:TreeMap<Integer,Double> -- -+evaluer():char ++evaluer():double UMLClass - 380 - 470 + 440 + 510 190 80 - Expression parenthesée + ParentheseExp -- -- +evaluer():exp.evaluer() @@ -134,36 +138,40 @@ UMLClass - 410 - 370 - 100 - 50 + 400 + 400 + 140 + 80 - Op* + Opmulti -- -- -+evaluer():gauche.evaluer() * droite.evaluer() ++evaluer(): +gauche.evaluer() * +droite.evaluer() UMLClass 550 - 370 - 100 - 50 + 400 + 140 + 80 - Op\ + Opdiv -- -- -+evaluer():gauche.evaluer() / droite.evaluer() ++evaluer(): +gauche.evaluer() / +droite.evaluer() Relation - 630 - 110 + 640 + 140 30 80 @@ -173,8 +181,8 @@ Relation - 420 - 160 + 430 + 190 520 60 @@ -184,20 +192,20 @@ Relation - 680 - 80 - 390 + 710 + 110 + 370 150 lt=<- droite - 10.0;10.0;330.0;10.0;330.0;130.0;290.0;130.0 + 10.0;10.0;310.0;10.0;310.0;130.0;270.0;130.0 Relation - 470 - 190 + 480 + 220 420 40 @@ -208,8 +216,8 @@ gauche Relation - 420 - 220 + 430 + 250 30 60 @@ -219,8 +227,8 @@ gauche Relation - 320 - 250 + 330 + 280 230 60 @@ -230,8 +238,8 @@ gauche Relation - 370 - 280 + 380 + 310 130 40 @@ -242,8 +250,8 @@ gauche Relation - 470 - 210 + 480 + 240 210 110 @@ -254,8 +262,8 @@ droite Relation - 910 - 220 + 920 + 250 30 60 @@ -265,8 +273,8 @@ droite Relation - 810 - 250 + 820 + 280 230 60 @@ -276,8 +284,8 @@ droite Relation - 520 - 310 + 530 + 340 30 60 @@ -287,8 +295,8 @@ droite Relation - 450 - 340 + 460 + 370 170 50 @@ -298,46 +306,46 @@ droite Relation - 560 - 60 - 580 - 470 + 620 + 90 + 530 + 480 lt=<- exp - 130.0;10.0;540.0;10.0;540.0;450.0;10.0;450.0 + 100.0;10.0;490.0;10.0;490.0;460.0;10.0;460.0 Relation - 60 - 430 - 430 - 60 + 70 + 480 + 480 + 50 lt=- - 10.0;40.0;10.0;10.0;410.0;10.0;410.0;40.0 + 10.0;30.0;10.0;10.0;460.0;10.0;460.0;30.0 Relation - 320 - 310 + 330 + 340 30 - 150 + 170 lt=<<- - 10.0;10.0;10.0;130.0 + 10.0;10.0;10.0;150.0 Relation - 250 - 430 + 290 + 480 30 - 60 + 50 lt=- - 10.0;10.0;10.0;40.0 + 10.0;10.0;10.0;30.0 -- 2.34.1