CMSC 202 Fall 2002
Project 3 Solutions
The solutions presented here are only "partial" solutions to
project 3. These solutions are versions of Proj3.C
that show different ways to handle the processing of the many
commands from the command file by calling functions from the
Library. In each instance, all file input is done in main()
or functions in Proj3.C called from main().
Please read the comments at the beginning of each file for
details.
Solution 1
Solution 1 shows the basic if/else
structure with lots of in-line code that most of you used in your project.
It's acceptable for this course, but not in the real world.
Large if/else decision structures like this (especailly without
functions) are too hard to read and require hard-coded command strings
in the code.
Solution 2
Solution 2 uses a switch statement and
functions for each command instead
of a large if/else statement with in-line code.
This is accomplished by creating an
array of const strings to hold the commands. This array is searched
until the command read from the file is found. The array index at which
the command is found is then used in a switch statement that has a
case for each command. Each case statement calls
the appropriate function to process the command.
Note that the strings themselves may not be used as cases in a switch
statement since they're not constants.
Solution 3
Solution 3 is an "advanced" solution that uses
a "jump table". The jump table is an array of function pointers that
parallels the array of command string introduced in solution 2.
When the command read from the file is found, the index at which the
the command is found is used as an index into the array of functions
pointers to call the function to process the command.