Introduction to Computer Science I by James Tam | Return to the course web page |
You are to write a simple calculator. The program will prompt the user to enter a mathematical expression. It will then display the original expression and the result. The expression will be entered in the following format:
<operand> <operation> <operand> OR <operand> <operation> <operand> <operation> <operand>
The program should check that exactly one of the four allowable operations has been entered and that it is entered between two operands. The program doesn’t have to handle unary operators (a case such as -5 * 2) nor do brackets have to be considered. However it does need to properly handle operator precedence (multiplication and division before addition and subtraction, left to right evaluation otherwise).
Numbers can be preceded or followed by any number of white spaces. Also they can consist of integers or real numbers. The program needs to check the format of real numbers. They cannot begin or end with a decimal dot e.g., ‘1.’ or ‘.5’ is not allowed. Of course multiple decimal points should not be allowed. Other than the decimal point for real numbers the program should disallow non-numeric information for the operands.
If an expression violates the required format then your program should provide clear and helpful error messages:
In addition to text-based input-output functions, functions that convert types e.g., int(), you are free to use the built in String operators and functions. Unless otherwise told you should not employ other pre-created functions. Of course the four basic mathematical operators can be used.
Also your program MUST be decomposed into functions. As part of the style requirement functions MUST be implemented according to the principles of good design: implement one well-defined task, not exceed a screen in length and good naming conventions should be used (self-descriptive, action-oriented names (verbs over nouns)). Global variables should not be used.