CS 210 - Fundamentals of Programming I
Spring 2006 - In-class exercise for 2/21/06
10 points
This exercise should be completed in pairs. The purpose of
this exercise is to work with the case selection construct. Here is a
problem statement, analysis, and design for today's in-class exercise.
Write a simulator for a simple calculator. It should repeatedly
perform a computation until the user wants to quit.
- Analysis
| Objects |
Type |
Kind |
Name |
| Operation choice |
char |
varying |
choice |
| User continuation answer |
char |
varying |
answer |
| First operand |
double |
varying |
operand1 |
| Second operand |
double |
varying |
operand2 |
| Result |
double |
varying |
result |
- Design
- Do
- 1.1
- Print the operation menu using PrintMenu()
- 1.2
- Read in choice
- 1.3
- Read in operand1, operand2
- 1.4
- Select on choice
- 1.4.1
- Case '+'
- 1.4.1.1 Compute result = operand1 + operand2;
- 1.4.2
- Case '-'
- 1.4.2.1 Compute result = operand1 - operand2;
- 1.4.3
- Default case
- 1.4.3.1 Display error message
- 1.4.3.2 Set result to 0
- 1.5
- Display result
- 1.6
- Ask the user if want to do another
- 1.7
- Read in answer
- While answer is 'Y' or 'y'
- Analysis - no objects
- Design
- Display a menu of choices
1. (4 points) Create a new project, then download the program
inclass12.cpp from the course webpage and add it to your
project. Build and run this program. Answer the following questions:
- a.
- What is the boolean expression in this program?
- b.
- What is the selection expression in the switch statement?
- c.
- Why does each case in the switch statement end with
a break statement?
- d.
- When does the default case of the switch statement
execute?
2. (6 points) Users of the calculator would like to be able to
multiply and divide. Modify your program in the following way:
- Add two new menu items ``* : multiplication'' and ``/ : division''
- Add a case for selection choices '*' and '/'
in the case construct that computes the result for multiplication and
division, respectively. Note that division by 0 is an illegal
operation, so your program should handle this situation by printing
out an error message and setting the result to 0.
Save your program, then rebuild and run it. Test it until you are
satisfied that it works.
When you have completed this exercise, print out your program
file and turn it in with one copy of this exercise sheet with your
answers to the questions.
Converted using latex2html on Tue Feb 21 00:42:06 CST 2006