SOURCE FILE: driver.cpp



/*

Programmer:	John Sarraille
Date:		August, 2000

*/

#include <iostream.h>
#include "stack.h"

void PushCom    (stackClass & theStack)  ;
void PopCom     (stackClass & theStack)  ;
void TellCom    (stackClass & theStack)  ;
void PopTellCom (stackClass & theStack)  ;
void EmptyTest  (stackClass & theStack)  ;
void FullTest   (stackClass & theStack)  ;
void PrintCom   (stackClass & theStack)  ;

/*

STACK-DRIVER PROGRAM

PURPOSE:

This program provides the user with the means to test a stack
data structure by calling for the execution of arbitrary stack
operations in an arbitrary sequence.

INPUT:

The user of this program inputs one-letter commands in response
to prompts from a menu.  The commands have the effect of
stipulating which operation to execute next and with which
parameter.

OUTPUT:

The program writes a menu to prompt for input.  Generally the
program writes terse messages in response to inputs, letting
the user know whether the stack operation just requested
executed successfully.  One command allows the user to print
the contents of the stack in a column (top above, bottom below).

PRECONDITIONS and POSTCONDITIONS:

The stack data structure must be created before running this
program and this source file must be compiled along with the
stack code.  The stack.cpp file will probably need include
statements like those above.

Sample compile command:

g++ stack.cpp driver.cpp

*/

int main () 
{

  stackClass theStack;
  char theChar;
  bool success;
  char command;

  cout << endl ;
  do 
  {
    cout << "u: Push,          p: Pop,            t: Tell," << endl ;
    cout << "o: Pop and Tell,  e: Empty Test,     f: Full Test," << endl ;
    cout << "w: Print Stack,   q: Quit Program."  << endl << endl ;
    cout << "Make your choice: " ;
    cin >> command ; 
    cout << endl ;

    switch (command) 
    {
      case 'u' : PushCom    (theStack) ;  break ;
      case 'p' : PopCom     (theStack) ;  break ;
      case 't' : TellCom    (theStack) ;  break ;
      case 'o' : PopTellCom (theStack) ;  break ;
      case 'e' : EmptyTest  (theStack) ;  break ;
      case 'f' : FullTest   (theStack) ;  break ;
      case 'w' : PrintCom   (theStack) ;  break ;
      case 'q' : cout << "Bye." ;         break ;
      default: cout << "Bad Command!" ; 
    }
    cout << endl << endl ;
  }
  while ((command != 'q') && (command != 'Q')) ;

  return 0 ;
}
       /* ######################################## */        
       /*               PushCom()                  */
       /* ######################################## */
/*

    This function provides an interface to the push operation
    of the stack data structure.  It obtains the parameter from
    the user, checks for errors, when appropriate makes a call
    to the push function, and does the necessary reporting of
    the outcome of the call.

*/
void PushCom(stackClass & theStack) 
{
  char theChar; 
  bool success;

  cout << "type the lower-case letter to push: " ;
  cin >> theChar ; cout << endl ;
  if ( (theChar >= 'a') && (theChar <= 'z') )
  {
    theStack.Push(theChar, success) ;
    if (!success) cout << "PUSH failure!" ;
    else cout << "The letter " << theChar 
              << " has been pushed onto the stack." ;
  }
  else cout << "Illegal Character!" ;
}

       /* ######################################## */        
       /*                PopCom()                  */
       /* ######################################## */
/*

    This function provides an interface to the pop operation of
    the stack data structure (the one that does not return the
    top of the stack).  It makes a call to the function, and
    does the necessary reporting of the outcome of the call.

*/
void PopCom(stackClass & theStack) 
{
  bool success ;

  theStack.Pop(success) ;
  if   (!success) cout << "POP failure!" ;
  else cout << "An element has been popped off the stack." ;
}

       /* ######################################## */        
       /*                TellCom()                 */
       /* ######################################## */
/*

    This function provides an interface to the GetStackTop
    operation of the stack data structure.  It makes a call to
    the function, and does the necessary reporting of the
    outcome of the call.

*/
void TellCom(stackClass & theStack) 
{
  char theChar; 
  bool success ;

  theStack.GetStackTop(theChar, success) ;
  if (!success) cout << "POP failure!" ;
  else cout << "The top element is: " << theChar ;
}

       /* ######################################## */        
       /*             PopTellCom()                 */
       /* ######################################## */
/*

    This function provides an interface to the pop operation of
    the stack data structure (the one that returns the top of
    the stack).  It makes the call to the function, and does
    the necessary reporting of the outcome of the call.

*/
void PopTellCom(stackClass & theStack) 
{
  char theChar; 
  bool success ;

  theStack.Pop(theChar, success) ;
  if (!success) cout << "POP failure!" ;
  else cout << "The top element was: " << theChar ;
}

       /* ######################################## */        
       /*              EmptyTest()                 */
       /* ######################################## */
/*

    This function provides an interface to the StackIsEmpty
    operation of the stack data structure.  It makes the call
    to the function, and does the necessary reporting of the
    outcome.

*/
void EmptyTest(stackClass & theStack) 
{
  if   ( theStack.StackIsEmpty() ) 
  cout << "The Stack is Empty." ;
  else cout << "The Stack is NOT Empty." ;
}

       /* ######################################## */        
       /*               FullTest()                 */
       /* ######################################## */
/*

    This function provides an interface to the StackIsFull
    operation of the stack data structure.  It makes the call
    to the function, and does the necessary reporting of the
    outcome.

*/
void FullTest(stackClass & theStack) 
{
  if   ( theStack.StackIsFull() ) 
  cout << "The Stack is Full." ;
  else cout << "The Stack is NOT Full." ;
}

       /* ######################################## */        
       /*               PrintCom()                 */
       /* ######################################## */
/*

    This function provides an interface to the StackPrint
    operation of the stack data structure.  It prints a message
    indicative of whether the stack is empty.  Then if the
    stack is not empty it makes a call to StackPrint.
*/
void PrintCom(stackClass & theStack) 
{
  if   ( theStack.StackIsEmpty() ) 
       cout << "The stack is empty.  There is nothing to print." ;
  else 
  {  
    cout << "The stack contains: " << endl << endl ;
    theStack.StackPrint() ;
  }
}