CS2310-Assignment 1 Program that Solves Arithmetic Expressions Solved
In this 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:
n Example (2 operands): 1.2 + 2.6 =
n 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 expression…etc. 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: