Skip to content
Snippets Groups Projects
Commit f2e698b6 authored by SAADI Nesrine's avatar SAADI Nesrine
Browse files

Exo 1 TP4

parent 2d67564c
No related branches found
No related tags found
No related merge requests found
...@@ -3,51 +3,42 @@ package formula; ...@@ -3,51 +3,42 @@ package formula;
import java.util.Stack; import java.util.Stack;
public class Calculator { public class Calculator {
String[] formule; AbstractFormulaFactory factory;
Stack<Formula> stackFormula; public Calculator(AbstractFormulaFactory factory){
public Calculator(String... formule){ this.factory = factory;
this.formule = formule;
calculate();
System.out.println(stackFormula.peek().asValue());
} }
Formula analyze(String... tokens){
boolean isDouble(String string){ Stack<Formula> stack = new Stack<>();
try { for (String token : tokens){
Double.parseDouble(string); analyzeToken(token,stack);
return true;
} }
catch (NumberFormatException e){return false;} return stack.pop();
} }
void calculateProduct(){ void analyzeToken(String token, Stack<Formula> stack){
Formula[] variables = new Formula[2]; if (token == "*")
variables[0] = stackFormula.pop(); analyzeProduct(stack);
variables[1] = stackFormula.pop(); else if (token == "+")
analyzeSum(stack);
else analyzeInt(token,stack);
}
Formula product = new Product(variables); void analyzeSum(Stack<Formula> stack){
Formula var1 = stack.pop();
Formula var2 = stack.pop();
stack.push(factory.createSum(var1,var2));
};
void analyzeProduct(Stack<Formula> stack){
Formula var1 = stack.pop();
Formula var2 = stack.pop();
stack.push(factory.createProduct(var1,var2));
};
stackFormula.push(product); void analyzeInt(String token, Stack<Formula> stack){
stack.push(factory.createConstant(Double.parseDouble(token)));
};
} }
void calculateSum(){
Formula[] variables = new Formula[2];
variables[0] = stackFormula.pop();
variables[1] = stackFormula.pop();
Formula sum = new Sum(variables);
stackFormula.push(sum);
}
public void calculate(){
for (String s : formule) {
if (isDouble(s)) {
Formula var = new Constant(Double.parseDouble(s));
stackFormula.push(var);}
if (s.equals("*")) calculateProduct();
if (s.equals("+")) calculateSum();
}
}
}
package formula; package formula;
public class Product extends VariadicOperator{ public class Product implements Operator{
public Product(Formula[] formulas){
super(formulas);
}
@Override @Override
public String symbol() { public String symbol() {
return "*"; return "*";
...@@ -16,7 +14,8 @@ public class Product extends VariadicOperator{ ...@@ -16,7 +14,8 @@ public class Product extends VariadicOperator{
} }
@Override @Override
public double cumulativeValue(double accumulator, double value) { public double cumulativeValue(double value, double accumulator) {
return accumulator*value; return value*accumulator;
} }
} }
package formula; package formula;
public class Sum extends VariadicOperator{ public class Sum implements Operator{
public Sum(Formula... formulas){
super(formulas);
}
@Override @Override
public String symbol() { public String symbol() {
return "+"; return "+";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment