Introduction to Computer Science I by James Tam Return to the course web page

CPSC 231: Assignment 3

New concepts to be applied for this assignment

Assignment description

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:

External libraries that can be used

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.

Submitting your work:

  1. Assignments (source code/'dot-py' files) must be electronically submitted according to [the assignment submission requirements].
  2. As a reminder, you are not allowed to work in groups for this class. Copying the work of another student will be regarded as academic misconduct (cheating). For additional details about what is and is not okay for this class please refer to the following [link].
  3. Before you submit your assignment to help make sure that you haven't missed anything here is a [checklist] of items to be used in marking.