In this assignment, students will write a program to solve arithmetic expressions. When the program runs, it reads in an expression (on a single line) and prints out the corresponding answer in the specified format.
Input expressions contain multiple real numbers with arithmetic operators in between. For simplicity, only + * and / are used in the expressions, there is no parenthesis ( ) nor advanced mathematics like power or square root. Expressions are basically interpreted from left to right. However, the program should also take the precedence rules into consideration. In other words, if the expression contains multiply/divide, it must be handled before add and subtract.
To simplify the problem further, we only consider expressions with TWO or THREE numeric operands:
- Example (2 operands): 2 + 2.6 =
- Example (3 operands): 2 * 3.4 + 5 =
The program should detect the number of operands and solve accordingly.
Submitted program will be tested repeatedly by PASS, each time with different input test case. (Note: The program only needs to read in ONE expression, students do not need to create loops to handle the 2nd, 3rd expressionetc. It is automatically handled by PASS) Students may assume that the input expression is always valid and always ends with the equal (=) sign. Students can also assume that there are spaces between numbers, operands and the equal sign such that they will not stick together.
2 Program Behavior
When the program runs, it first prints out the message Please input the formula: on a single line. The program then read the whole expression from keyboard and calculate the corresponding answer (hint: you may detect the number of operands by checking whether the input character is = ). After evaluation, the program then prints a box (using /, , , and | characters), inside which it writes: The answer is <answer> .
The output number should be printed in exactly 2 decimal places. Note that the length of the bounding box also varies according to the magnitude and format of the final answer.
Students may assume that there will not be Division by Zero cases and that the magnitude of the answer is no greater than 1000000000.
Sample Input / Output: (for demonstration purpose, user input colored in blue)
Please input the formula: 1 + 2 * 3 = / | The answer is 7.00 | / | Please input the formula: 10 / 3 = / | The answer is 3.33 | / |
Please input the formula: 8.5 + 1.3 + 0.2 = / | The answer is 10.00 | / | Please input the formula: 1.5 * 0 = / | The answer is 0.00 | / |
3 Marking criteria
Submitted program will be tested repeatedly with PASS. Correctness marks will be graded objectively based on the number of correct outputs reported by PASS.
- If the program is not compilable, correctness mark will be zero
- Make sure that the output format (spelling, spacingetc) follows exactly the sample output above otherwise PASS will consider your answer incorrect. Note that the marker will make NO manual attempt to check or re-mark program output.
Unlike tutorial exercises, for assignment, PASS will NOT make ALL test cases visible online. (i.e. there are hidden test cases). You should not assume that the report generated from the Test button is your final result.
Note that the marking in PASS is automatic, therefore the following situations may lead to extremely low marks:
- Submission of non .cpp files (e.g. .exe or .zip files)
- Submission with the test function rather than the submit function
- Input/output does not match the requirements as defined.
(e.g. incorrect spacing, incorrect spelling, letter cases or punctuations)
Reviews
There are no reviews yet.