Download the code, and compile all files. This is the most recent version of our calculator, as we discussed in the class on exceptions. (It shows errors with a nice error message and indicates the error position in the input.)

Make sure you understand the implementation of the power operator ^. You should understand how we achieved that the power operator is right-associative, while all other operators are left-associative.

Your task is to make a few improvements to the calculator. All changes must be made in the file calculator4.scala. You are not allowed to change token.scala and tokenizer.scala. (In any case, you will only upload calculator4.scala to the submission server.)

The test suite in checksuite.scala allows you to do some basic tests of your implementation:

$ fsc checksuite.scala $ scala org.scalatest.run CalculatorCheckSuite Run starting. Expected test count is: 5 CalculatorCheckSuite: - Constants *** FAILED *** 0.0 did not equal 3.141592653589793 +- 1.0E-13 (checksuite.scala:11) - Division by zero *** FAILED *** No error was reported in expression '4.5 / 0' (checksuite.scala:20) - Functions *** FAILED *** Could not parse expression 'sqrt(16)' (checksuite.scala:10) - Function syntax *** FAILED *** No error was reported in expression 'sqrt + 7' (checksuite.scala:20) - Sqrt argument *** FAILED *** Could not parse expression 'sqrt(-7)' (checksuite.scala:19) Run completed in 277 milliseconds. Total number of tests run: 5 Suites: completed 1, aborted 0 Tests: succeeded 0, failed 5, canceled 0, ignored 0, pending 0 *** 5 TESTS FAILED ***All tests will pass when you are done with the three tasks.

Implement the constants pi and e (in Scala, use math.Pi and math.E).

> pi ==> 3.141592653589793 > 2 * pi ==> 6.283185307179586 > e ==> 2.718281828459045 > e^2 ==> 7.3890560989306495 > 1 - e^-1 ==> 0.6321205588285577

When a division by zero is attempted, the calculator should stop evaluating the expression, and instead indicate a "Division by zero" error. The error indicator must point at the division operator.

> 4.5 / 0 Error: Division by zero 4.5 / 0 ^ > 1 + 2 + 3 + 4 / (10 - 2 * 5) Error: Division by zero 1 + 2 + 3 + 4 / (10 - 2 * 5) ^

Add the square root function sqrt to compute the square root of an expression, the sine function sin, and the cosine function cos. If the argument of sqrt is negative, report a "Square root of negative number" error.

> sqrt(16) ==> 4.0 > 3 + sqrt(81) ==> 12.0 > sin(pi/3) ==> 0.8660254037844386 > 1/sqrt(2) ==> 0.7071067811865475 > 1/sqrt(3) ==> 0.5773502691896258 > cos(pi/3) ==> 0.5000000000000001 > sqrt + 7 Error: expected '(' sqrt + 7 ^ > sqrt(13 + 9 Error: expected operator or ')' sqrt(13 + 9 ^ > 3 + sqrt(22 - 3 * 13) Error: Square root of negative number 3 + sqrt(22 - 3 * 13) ^Hint: Draw a new syntax diagram first, then implement the changes in this diagram.