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");
+ }
}
}
public abstract class Expression {
- abstract double evaluer();
+ abstract double evaluer() throws NotDigitException;
}
+import java.util.Map.Entry;
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<Integer, Double> 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<Integer, Double> e = x.getFirstEntry();
+ System.out.println("x = y = z = " + e.getValue() + " :");
+ System.out.println(exp + " = " + ArithmeticExpression.evaluer());
+ }
+ }
+ catch (Exception e) {}
}
}
CLASSES = \
NotCharacterException.java \
+ NotDigitException.java \
Expression.java \
Terme.java \
Opadd.java \
Opdiv.java \
Chiffre.java \
Variable.java \
- Parenthese.java \
+ ParentheseExp.java \
Main.java
#
--- /dev/null
+
+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
+ }
+
+}
super(g, d);
}
- public double evaluer() {
+ public double evaluer() throws NotDigitException {
return gauche.evaluer() / droite.evaluer();
}
}
super(g, d);
}
- public double evaluer() {
+ public double evaluer() throws NotDigitException {
return gauche.evaluer() - droite.evaluer();
}
}
super(g, d);
}
- public double evaluer() {
+ public double evaluer() throws NotDigitException {
return gauche.evaluer() * droite.evaluer();
}
}
super(g, d);
}
- public double evaluer() {
+ public double evaluer() throws NotDigitException {
return gauche.evaluer() + droite.evaluer();
}
}
+++ /dev/null
-
-class Parenthese extends Facteur {
-
- public double evaluer() {
- return 0.0;
- }
-}
--- /dev/null
+
+class ParentheseExp extends Facteur {
+ private Expression exp;
+
+ ParentheseExp(Expression e) {
+ exp = e;
+ }
+
+ public double evaluer() throws NotDigitException {
+ return exp.evaluer();
+ }
+}
import java.util.TreeMap;
+import java.util.Map.Entry;
class Variable extends Terme {
- TreeMap<Character, Double> v;
+ //TODO: TreeMap might look overkill
+ TreeMap<Integer, Double> v;
- Variable(Character c) throws NotCharacterException {
- v = new TreeMap<Character, Double>();
- //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<Integer, Double>();
+ 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<Integer, Double> getFirstEntry() {
+ return v.firstEntry();
+ }
+
+ public Integer size() {
+ return v.size();
}
public double evaluer() {
- return 0.0;
+ return v.pollFirstEntry().getValue();
}
}
<element>
<id>UMLClass</id>
<coordinates>
- <x>590</x>
- <y>60</y>
- <w>100</w>
+ <x>580</x>
+ <y>90</y>
+ <w>140</w>
<h>60</h>
</coordinates>
<panel_attributes>/Expression/
--
--
-/+evaluer():/
+/+evaluer():double/
</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>UMLClass</id>
<coordinates>
- <x>380</x>
- <y>200</y>
+ <x>390</x>
+ <y>230</y>
<w>100</w>
<h>30</h>
</coordinates>
<element>
<id>UMLClass</id>
<coordinates>
- <x>870</x>
- <y>200</y>
+ <x>880</x>
+ <y>230</y>
<w>100</w>
<h>30</h>
</coordinates>
- <panel_attributes>/Op-add/</panel_attributes>
+ <panel_attributes>/Opadd/</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>UMLClass</id>
<coordinates>
- <x>280</x>
- <y>290</y>
+ <x>290</x>
+ <y>320</y>
<w>100</w>
<h>30</h>
</coordinates>
<element>
<id>UMLClass</id>
<coordinates>
- <x>770</x>
- <y>290</y>
- <w>100</w>
- <h>50</h>
+ <x>760</x>
+ <y>320</y>
+ <w>150</w>
+ <h>80</h>
</coordinates>
- <panel_attributes>Op+
+ <panel_attributes>Opplus
--
--
-+evaluer():gauche.evaluer() + droite.evaluer()</panel_attributes>
++evaluer():
+gauche.evaluer() +
+droite.evaluer()</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>UMLClass</id>
<coordinates>
- <x>480</x>
- <y>290</y>
+ <x>490</x>
+ <y>320</y>
<w>100</w>
<h>30</h>
</coordinates>
- <panel_attributes>/Op-mul/</panel_attributes>
+ <panel_attributes>/Opmul/</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>UMLClass</id>
<coordinates>
- <x>970</x>
- <y>290</y>
- <w>100</w>
- <h>50</h>
+ <x>960</x>
+ <y>320</y>
+ <w>140</w>
+ <h>80</h>
</coordinates>
- <panel_attributes>Op-
+ <panel_attributes>Opminus
--
--
-+evaluer():gauche.evaluer() - droite.evaluer()</panel_attributes>
++evaluer():
+gauche.evaluer() -
+droite.evaluer()</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>UMLClass</id>
<coordinates>
<x>10</x>
- <y>470</y>
- <w>130</w>
+ <y>510</y>
+ <w>150</w>
<h>80</h>
</coordinates>
<panel_attributes>Chiffre
--
-c:int <- {0,...,9}
--
-+evaluer():int </panel_attributes>
++evaluer():double</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>UMLClass</id>
<coordinates>
- <x>170</x>
- <y>470</y>
- <w>180</w>
+ <x>190</x>
+ <y>510</y>
+ <w>220</w>
<h>80</h>
</coordinates>
<panel_attributes>Variable
--
--vMap:Map<key, value>
+-v:TreeMap<Integer,Double>
--
-+evaluer():char</panel_attributes>
++evaluer():double</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>UMLClass</id>
<coordinates>
- <x>380</x>
- <y>470</y>
+ <x>440</x>
+ <y>510</y>
<w>190</w>
<h>80</h>
</coordinates>
- <panel_attributes>Expression parenthesée
+ <panel_attributes>ParentheseExp
--
--
+evaluer():exp.evaluer()</panel_attributes>
<element>
<id>UMLClass</id>
<coordinates>
- <x>410</x>
- <y>370</y>
- <w>100</w>
- <h>50</h>
+ <x>400</x>
+ <y>400</y>
+ <w>140</w>
+ <h>80</h>
</coordinates>
- <panel_attributes>Op*
+ <panel_attributes>Opmulti
--
--
-+evaluer():gauche.evaluer() * droite.evaluer()</panel_attributes>
++evaluer():
+gauche.evaluer() *
+droite.evaluer()</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>UMLClass</id>
<coordinates>
<x>550</x>
- <y>370</y>
- <w>100</w>
- <h>50</h>
+ <y>400</y>
+ <w>140</w>
+ <h>80</h>
</coordinates>
- <panel_attributes>Op\
+ <panel_attributes>Opdiv
--
--
-+evaluer():gauche.evaluer() / droite.evaluer()</panel_attributes>
++evaluer():
+gauche.evaluer() /
+droite.evaluer()</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>Relation</id>
<coordinates>
- <x>630</x>
- <y>110</y>
+ <x>640</x>
+ <y>140</y>
<w>30</w>
<h>80</h>
</coordinates>
<element>
<id>Relation</id>
<coordinates>
- <x>420</x>
- <y>160</y>
+ <x>430</x>
+ <y>190</y>
<w>520</w>
<h>60</h>
</coordinates>
<element>
<id>Relation</id>
<coordinates>
- <x>680</x>
- <y>80</y>
- <w>390</w>
+ <x>710</x>
+ <y>110</y>
+ <w>370</w>
<h>150</h>
</coordinates>
<panel_attributes>lt=<-
droite</panel_attributes>
- <additional_attributes>10.0;10.0;330.0;10.0;330.0;130.0;290.0;130.0</additional_attributes>
+ <additional_attributes>10.0;10.0;310.0;10.0;310.0;130.0;270.0;130.0</additional_attributes>
</element>
<element>
<id>Relation</id>
<coordinates>
- <x>470</x>
- <y>190</y>
+ <x>480</x>
+ <y>220</y>
<w>420</w>
<h>40</h>
</coordinates>
<element>
<id>Relation</id>
<coordinates>
- <x>420</x>
- <y>220</y>
+ <x>430</x>
+ <y>250</y>
<w>30</w>
<h>60</h>
</coordinates>
<element>
<id>Relation</id>
<coordinates>
- <x>320</x>
- <y>250</y>
+ <x>330</x>
+ <y>280</y>
<w>230</w>
<h>60</h>
</coordinates>
<element>
<id>Relation</id>
<coordinates>
- <x>370</x>
- <y>280</y>
+ <x>380</x>
+ <y>310</y>
<w>130</w>
<h>40</h>
</coordinates>
<element>
<id>Relation</id>
<coordinates>
- <x>470</x>
- <y>210</y>
+ <x>480</x>
+ <y>240</y>
<w>210</w>
<h>110</h>
</coordinates>
<element>
<id>Relation</id>
<coordinates>
- <x>910</x>
- <y>220</y>
+ <x>920</x>
+ <y>250</y>
<w>30</w>
<h>60</h>
</coordinates>
<element>
<id>Relation</id>
<coordinates>
- <x>810</x>
- <y>250</y>
+ <x>820</x>
+ <y>280</y>
<w>230</w>
<h>60</h>
</coordinates>
<element>
<id>Relation</id>
<coordinates>
- <x>520</x>
- <y>310</y>
+ <x>530</x>
+ <y>340</y>
<w>30</w>
<h>60</h>
</coordinates>
<element>
<id>Relation</id>
<coordinates>
- <x>450</x>
- <y>340</y>
+ <x>460</x>
+ <y>370</y>
<w>170</w>
<h>50</h>
</coordinates>
<element>
<id>Relation</id>
<coordinates>
- <x>560</x>
- <y>60</y>
- <w>580</w>
- <h>470</h>
+ <x>620</x>
+ <y>90</y>
+ <w>530</w>
+ <h>480</h>
</coordinates>
<panel_attributes>lt=<-
exp</panel_attributes>
- <additional_attributes>130.0;10.0;540.0;10.0;540.0;450.0;10.0;450.0</additional_attributes>
+ <additional_attributes>100.0;10.0;490.0;10.0;490.0;460.0;10.0;460.0</additional_attributes>
</element>
<element>
<id>Relation</id>
<coordinates>
- <x>60</x>
- <y>430</y>
- <w>430</w>
- <h>60</h>
+ <x>70</x>
+ <y>480</y>
+ <w>480</w>
+ <h>50</h>
</coordinates>
<panel_attributes>lt=-</panel_attributes>
- <additional_attributes>10.0;40.0;10.0;10.0;410.0;10.0;410.0;40.0</additional_attributes>
+ <additional_attributes>10.0;30.0;10.0;10.0;460.0;10.0;460.0;30.0</additional_attributes>
</element>
<element>
<id>Relation</id>
<coordinates>
- <x>320</x>
- <y>310</y>
+ <x>330</x>
+ <y>340</y>
<w>30</w>
- <h>150</h>
+ <h>170</h>
</coordinates>
<panel_attributes>lt=<<-</panel_attributes>
- <additional_attributes>10.0;10.0;10.0;130.0</additional_attributes>
+ <additional_attributes>10.0;10.0;10.0;150.0</additional_attributes>
</element>
<element>
<id>Relation</id>
<coordinates>
- <x>250</x>
- <y>430</y>
+ <x>290</x>
+ <y>480</y>
<w>30</w>
- <h>60</h>
+ <h>50</h>
</coordinates>
<panel_attributes>lt=-</panel_attributes>
- <additional_attributes>10.0;10.0;10.0;40.0</additional_attributes>
+ <additional_attributes>10.0;10.0;10.0;30.0</additional_attributes>
</element>
</diagram>