Description
-
Functional Requirement:
-
The program evaluates the value of math expressions, and outputs an integer value or “invalid”.
-
In test A, each math expression includes (see the example below):
2.1 positive integers;
2.2 (no more than five) operators of “+” (addition), “-“ (subtraction), “*” (multiplication) and “/” (division).
-
In test A, all expressions are valid. The output is an integer value after rounding.
-
An example of input file for test A
Expected output file:
1+2
3
3+4/3+7
11
1*2*3*4+5
29
2*3+4*5+6/7
27
1/2-1*3/3
0
1/2-1*4/3
-1
…
…
-
In test B, each math expression includes (see the example below):
4.1 numbers (integers and doubles);
4.2 (no more than ten) operators of “+” (addition), “-“ (subtraction), “*” (multiplication) and “/” (division);
4.3 (no more than three) functions including “sin” (sine function), “cos” (cosine function), “tan” (tangent function) and “sqrt” (square root function).
4.4 “(“ and “)” (brackets);
4.5 possibly blank space.
-
In test B, the expression may not be valid. Correspondingly, the output is either the integer value after rounding if the expression is valid, or “invalid” if the expression is not valid.
-
An example of input file for test B
Expected output file:
1+2.0*sin(37+(25*3))
-1
(2+ 3.50)*4*sqrt(sin(1.5))
22
-3+4/ (2.5+3.7)
-2
(-3+4)/2.5+3.9
4
1.2-3.5*5.2-13.2
-30
1.2-3.5*5.2-13.7
-31
2.3*5*7 – 12*9/8
67
-sin(3.5-sqrt(4)) + cos(tan(2.5))
0
sqrt(-1)
invalid
2.5 4.0
invalid
…
…
Program Template:
Each submission is expected to strictly follow the template to implement the required function.
The main function is in TestMathExpr.java. In line 11 of TestMathExpr.java, an expression string is evaluated, which returns the value of the expression. Each submission is required to:
-
Modify the MathExpr.java and implement the parse() function;
-
If needed, new classes and functions can be added;
-
No modification of TestMathExpr.java is allowed.
The command used to run the program is: java TestMathExpr input.txt output.txt, where input.txt is the input file of expressions, and output.txt is the output file of values.
Appendix
-
TestMathExpr.java
-
MathExpr.java
-
inputA.txt
-
outputA.txt
-
inputB.txt
-
outputB.txt