View Javadoc
1   package calculator;
2   
3   import calculator.atoms.Atom;
4   import visitor.Counter;
5   import visitor.Evaluator;
6   import visitor.Printer;
7   
8   /**
9    * This class represents the core logic of a Calculator.
10   * It can be used to print and evaluate artihmetic expressions.
11   *
12   * @author tommens
13   */
14  public class Calculator {
15  
16  	/**
17  	 * Default constructor of the class.
18  	 * Does nothing since the class does not have any variables that need to be
19  	 * initialised.
20  	 */
21  	public Calculator() {
22  	}
23  
24  	/*
25  	 * For the moment the calculator only contains a print method and an eval method
26  	 * It would be useful to complete this with a read method, so that we would be
27  	 * able
28  	 * to implement a full REPL cycle (Read-Eval-Print loop) such as in Scheme,
29  	 * Python, R and other languages.
30  	 * To do so would require to implement a method with the following signature,
31  	 * converting an input string
32  	 * into an arithmetic expression:
33  	 * public Expression read(String s)
34  	 */
35  
36  	/**
37  	 * Prints an arithmetic expression provided as input parameter.
38  	 * 
39  	 * @param e the arithmetic Expression to be printed
40  	 * @see #printExpressionDetails(Expression)
41  	 */
42  	public void print(Expression e) {
43  		System.out.println("The result of evaluating expression " + format(e, Notation.INFIX));
44  		System.out.println("is: " + format(eval(e), Notation.INFIX) + ".");
45  		System.out.println();
46  	}
47  
48  	/**
49  	 * Prints verbose details of an arithmetic expression provided as input
50  	 * parameter.
51  	 * 
52  	 * @param e the arithmetic Expression to be printed
53  	 * @see #print(Expression)
54  	 */
55  	public void printExpressionDetails(Expression e) {
56  		print(e);
57  		Counter c = new Counter();
58  		e.accept(c);
59  		System.out.print("It contains " + c.getDepth() + " levels of nested expressions, ");
60  		System.out.print(c.getNbOps() + " operations");
61  		System.out.println(" and " + c.getNbNbs() + " numbers.");
62  		System.out.println();
63  	}
64  
65  	/**
66  	 * Evaluates an arithmetic expression and returns its result
67  	 * 
68  	 * @param e the arithmetic Expression to be evaluated
69  	 * @return The result of the evaluation
70  	 */
71  	public Atom eval(Expression e) {
72  		// create a new visitor to evaluate expressions
73  		Evaluator v = new Evaluator();
74  		// and ask the expression to accept this visitor to start the evaluation process
75  		e.accept(v);
76  		// and return the result of the evaluation at the end of the process
77  		return v.getResult();
78  	}
79  
80  	/**
81  	 * Formats an arithmetic expression in a given notation
82  	 * 
83  	 * @param e        the arithmetic Expression to be formatted
84  	 * @param notation the notation to be used for formatting
85  	 * @return The formatted expression as a String
86  	 */
87  	public String format(Expression e, Notation notation) {
88  		Printer p = new Printer(notation);
89  		e.accept(p);
90  		return p.getResult();
91  	}
92  
93  	/*
94  	 * We could also have other methods, e.g. to verify whether an expression is
95  	 * syntactically correct
96  	 * public Boolean validate(Expression e)
97  	 * or to simplify some expression
98  	 * public Expression simplify(Expression e)
99  	 */
100 }