#include "wrapper.h"
using namespace std; |
The #include Section
- Always include the "wrapper.h" file to get all the
standard wrapper classes for the built-in types.
- #include files that provide software components, e.g., Queue,
Stack, List, etc.
- #include files that provide functions that need to be called.
|
void displayMenu ( ... )
{
cout <<
"Legal Commands Are:" <<
endl;
cout <<
" ... " <<
endl;
...
cout <<
"Type 'q' to Quit" <<
endl;
} |
Display Menu Section
- It can be helpful to display a menu of legal commands for the
user.
- If the number of commands is large, however, this approach may
not work very well.
|
void doOp1 ( ... ) { ... } |
void doOp2 ( ... ) { ... } |
...
void doOpN ( ... ) { ... } |
|
The "Do" Operations Section
- Specifically written to handle a command.
- One "Do" Operation per command.
- A "Do" Operation might produce output to the user.
- They might cause a permanent change to a data structure.
- They might cause a change to the environment, e.g., a file to be
written to.
|
void dispatchCommand ( ... )
{
switch (command) {
case command_1:
doOp1 (...);
break;
case command_2:
...
default:
cout <<
"No such command" <<
endl;
} // end switch
} |
The Decoder/Dispatcher Section
- This example decodes the command and dispatches to the correct
"Do" Operation.
- If the commands are complex, then a separate decoder operation
might be required.
- A C++ switch statement can be used.
- if statements can be used.
- A default case can be supplied to handle incorrect commands
entered by the user.
|
void commandController ( ... )
{
Character command;
do {
displayMenu ( ... );
cin >> command;
dispatchCommand (command);
while (command != 'q');
} |
The commandController Section
- Displays the menu, if necessary
- Inputs a command
- Gives it to the Decoder/Dispather
- Keeps looping until user requests an exit
|
void main ( ... )
{
T x1, x2;
commandController (x1, x2, ... );
} |
The main Operation Section
- Declares objects that need to live the lifetime of the
program.
- In this example, type T would have come from one of the
#include files.
- Passes those objects to the commandController as parameters
so rest of program can use them.
- Calls commandController to begin looping.
|
|