Lab Assignment #3

Due Tuesday, July 22 at the beginning of class

The assignment is to write a C++ program that implements a very rudimentary calculator. As you will see, the functionality will be simple. Basically, the user will type in an expression, then the program will calculate and display the result. Each expression will have 2 operators and 3 operands. Sample runs are shown below:

Enter an expression in the form: num op num op num (ex. 4 * 3 + 6) 5*2/4 The expression 5 * 2 / 4 evaluates to 2.5

Enter an expression in the form: num op num op num (ex. 4 * 3 + 6) 5 - 2 / 4 The expression 5 - 2 / 4 evaluates to 4.5

Enter an expression in the form: num op num op num (ex. 4 * 3 + 6) 8 - 4 / 0 ERROR! Attempting to divide by 0 The expression 8 - 4 / 0 evaluates to 8

In the first sample, the two operators are multiplication (*) and division (/), and the three operands are 5, 2, and 4. Evaluating the expression gives the result 2.5.

In the second sample, the two operators are subtraction (-) and division (/), and the three operands are the same. Evaluating the expression gives a result of 4.5. Notice that the division was performed first, even though the user typed it in after the subtraction. Your program must evaluate the expression according to the rules of operator precedence.

The third sample shows how to respond to a "divide by zero" error.

Program Specifications

This assignment exercises your knowledge of conditional statements and functions. As in all programs you write, you should use meaningful identifier names, and the code must be well commented and consistently formatted. Your functions must have appropriate declarations and the header comments must explain the purpose of the function, and describe any inputs and/or outputs.

How to submit your programs

Back to Outline