CMPT 212 Assignment 2
David Simpson (simpson@cs.sfu.ca)
Sat, 7 Feb 1998 12:24:01 -0800 (PST)
CMPT 212 Assignment 2
Spring 1998 ------------
Due date: 2:30pm, Monday, February 23, 1998
in the assignment drop-off boxes.
1. Goal
- To become familiar with C++ classes.
2. Overview
- The program will be a program to read, parse, display, and
evaluate two arithmetic expressions.
3. Description
3.1 Arithmetic Expressions
- An arithmetic expression is defined as in Assignment 1.
3.2 Program Execution
- The program will store two expressions. The user can
set, display, evaluate, or clear either expression.
The user can also combine the two expression using
addition, subtraction, multiplication, or division.
- Initially, the two expression are set to the constant 0.
- See the sample run below (Section 9) to see what menus,
prompts, and other information is displayed by the
program.
- The program will display this menu, then read in the
user's choice.
1. Enter expression
2. Display expression
3. Evaluate expression
4. Clear expression
5. Join expressions
6. Quit
3.2.1 "Enter expression"
- The program will display a prompt, read in an expression
string, ask the user which expression (the first or
the second) should be assigned to.
3.2.2 "Display expression"
- The program will ask the user what expression to display,
then display it.
3.2.3 "Evaluate expression"
- The program will ask the user what expression to display,
then evaluate it and display the result.
3.2.4 "Clear expression"
- The program will ask the user what expression to display,
then set that expression to the constant 0.
3.2.5 "Join expressions"
- The program will ask the user what operation to use to
join the expression, then the expressions will be
joined with this operation and assigned to the first
expression.
expr1 := expr1 op expr2
3.2.6 "Quit"
- The program releases the memory allocated to the two
expressions, then quits.
4. Design of the Program
4.1 Modules
- The program will consist of three modules: a "Parse" module,
a "Expression" module, and "Main" module.
4.2 The Parse Module
- This module defines the data types and functions to
parse a character string into an expression.
- The source files for this module will be called "parse.h" and
"parse.cpp".
4.2 The Expression Module
- This module defines the "expr" class.
- The source files for this module will be called "expr.h" and
"expr.cpp".
4.4 The Main Module
- The source file for this module will be called "asgn2.cpp".
- This module contains the main function of the program.
5. Design Features
5.1 Error Checking
- Very little error checking needs to be done.
- The program does not have to check for out-of-memory errors
or input errors.
5.2 Given Code
- The header file for the Parse module and the Expression
module are given to you. You must use them. You must not
change them.
- You must implement all of the functions defined in these
files.
- These files will be available on the class web page and also
in the Assignment Lab on the N: drive.
5.5 Bool Type
- If you are using a compiler which does not support the "bool"
type, you must define the "bool" type yourself in a header
file called "bool.h". Your program must be able to compile
without errors under the Borland C++ in the Assignment Lab.
5.6 I/O
- You must do all output using the "<<" operator.
- You may use "cin.getline()" to read in the input character
string.
5.7 Miscellaneous
- Use "const" wherever possible.
- Do not define any classes for this assignment.
- Use the smallest scope and lifetime for every variable,
constant, and function.
6. Source Code Comments
- Your source code must be fully commented.
- See the Sample Assignment on the web page for an example of
the expected commenting style.
http://www.cs.sfu.ca/CC/212/simpson/98-1/assignments/sample.html
7. Test Runs
- Run your program with this input:
- Display the first expression.
- Set the first expression to "10-3*2".
- Display the first expression.
- Evaluate the first expression.
- Set the second expression to "4.2*(2-5)".
- Display the second expression.
- Evaluate the second expression.
- Join the expressions using division.
- Display the first expression.
- Evaluate the first expression.
- Clear the second expression.
- Display the first expression.
- Evaluate the first expression.
- Display the second expression.
- Evaluate the second expression.
- Quit.
8. What to Hand In
- Title page containing:
a. Course number
b. Assignment number
c. Student name
d. Student number
e. Student email address
- Listing of the source code in this order:
a. main.cpp
b. parse.h
c. parse.cpp
d. expr.h
e. expr.cpp
f. bool.h (if written -- see Section 5.5 above)
- Scripts of the test run (see Section 7 above).
9. Sample Run
- Here is a sample run of the program:
1. Enter expression
2. Display expression
3. Evaluate expression
4. Clear expression
5. Join expressions
6. Quit
> 2
Which expression (1 or 2): 1
0
1. Enter expression
2. Display expression
3. Evaluate expression
4. Clear expression
5. Join expressions
6. Quit
> 1
Enter an expression: 1-2*3
Which expression (1 or 2): 1
1. Enter expression
2. Display expression
3. Evaluate expression
4. Clear expression
5. Join expressions
6. Quit
> 2
Which expression (1 or 2): 1
(- 1 (* 2 3))
1. Enter expression
2. Display expression
3. Evaluate expression
4. Clear expression
5. Join expressions
6. Quit
> 3
Which expression (1 or 2): 1
-5
1. Enter expression
2. Display expression
3. Evaluate expression
4. Clear expression
5. Join expressions
6. Quit
> 1
Enter an expression: 10*2/5
Which expression (1 or 2): 2
1. Enter expression
2. Display expression
3. Evaluate expression
4. Clear expression
5. Join expressions
6. Quit
> 2
Which expression (1 or 2): 2
(/ (* 10 2) 5)
1. Enter expression
2. Display expression
3. Evaluate expression
4. Clear expression
5. Join expressions
6. Quit
> 3
Which expression (1 or 2): 2
4
1. Enter expression
2. Display expression
3. Evaluate expression
4. Clear expression
5. Join expressions
6. Quit
> 5
Which operation?
1. Addition
2. Subtraction
3. Multiplication
4. Division
> 2
1. Enter expression
2. Display expression
3. Evaluate expression
4. Clear expression
5. Join expressions
6. Quit
> 2
Which expression (1 or 2): 1
(- (- 1 (* 2 3)) (/ (* 10 2) 5))
1. Enter expression
2. Display expression
3. Evaluate expression
4. Clear expression
5. Join expressions
6. Quit
> 3
Which expression (1 or 2): 1
-9
1. Enter expression
2. Display expression
3. Evaluate expression
4. Clear expression
5. Join expressions
6. Quit
> 4
Which expression (1 or 2): 2
1. Enter expression
2. Display expression
3. Evaluate expression
4. Clear expression
5. Join expressions
6. Quit
> 2
Which expression (1 or 2): 2
0
1. Enter expression
2. Display expression
3. Evaluate expression
4. Clear expression
5. Join expressions
6. Quit
> 2
Which expression (1 or 2): 1
(- (- 1 (* 2 3)) (/ (* 10 2) 5))
1. Enter expression
2. Display expression
3. Evaluate expression
4. Clear expression
5. Join expressions
6. Quit
> 6
(end of assignment)