CSCI 132 Data Structures--Spring 2014

    Home | | Syllabus | | Assignments | | Lecture Notes

    Assignment 3 Solutions

    Solution to testDeque.cc code:

    /**************************************************************
     * Filename: testDeque.cc
     * Author: Constance Royden
     * Date Created: 2/13/14
     * Course: CSCI132
     * Assignment: Homework 3
     * Purpose: Test deque functions
     **************************************************************/
    #include "deque.h"
    #include <iostream>
    
    using namespace std;
    
    char get_command();
    void help();
    bool do_command(char, Deque &);
    
    int main( )
    /* Post: Accepts commands from user as a menu-driven demonstration program
    for the class deque.
    Uses: The class deque and the functions introduction, get_command,
    and do_command. */
    {
      Deque test_deque;
      help( );
      while (do_command(get_command( ), test_deque));
    } // end main( )
    
    void help()
    /*
    Pre: None
    Post: A help screen for the program is printed, giving the meaning of each
    command that the user may enter.
    */
    {
      cout << endl
           << "This program allows the user to enter one command" << endl
           << "(but only one)on each input line." << endl
           << "For example, if the command S is entered, then" << endl
           << "the program will serve the front of the queue." << endl
           << endl
           << " The valid commands are:" << endl
           << "A - Append the next input character to the rear." << endl
           << "P - Push the next input character to the front." << endl
           << "S - Serve the front of the queue" << endl
           << "X - Extract the rear of the queue" << endl
           << "R - Retrieve and print the front entry." << endl
           << "W - Retrieve and write the rear entry." << endl
           << "H - This help screen" << endl
           << "Q - Quit" << endl
           << "Press <Enter> to continue." << flush;
    
      char c;
      do {
        cin.get(c);
      } while (c != '\n');
    }  //end help( )
    
    bool do_command(char c, Deque &test_queue)
    /*
    Pre: c represents a valid command.
    Post: Performs the given command c on the Deque test_queue.
    Returns false if c == ¼q¼, otherwise returns true.
    Uses: The class Deque.
    */
    {
      bool continue_input = true;
      Queue_entry x;
      switch (c){
        case 'w':
          if (test_queue.retrieve_rear(x)== underflow)
            cout << "Deque is empty." << endl << endl;
          else
            cout << "The last entry is: " << x
                 << endl << endl;
          break;
        case 'r':
          if (test_queue.retrieve_front(x)== underflow)
            cout << "Deque is empty." << endl << endl;
          else
            cout << "The first entry is: " << x
                 << endl << endl;
            break;
        case 'q':
          cout << "Deque demonstration finished." << endl;
          continue_input = false;
          break;
        case 'x':
          if (test_queue.serve_rear()== underflow)
            cout << "Serve failed, the Deque is empty." << endl
    	     << endl;
          else
    	    cout << "Successfully served the rear of the deque." << endl << endl;
          break;
        case 's':
          if (test_queue.serve_front()== underflow)
            cout << "Serve failed, the deque is empty." << endl
                 << endl;
          else
            cout << "Successfully served the front of the deque. " << endl << endl;
          break;
        case 'p':
          cout << "Enter new key to insert:" << flush;
          cin >> x;
          if (test_queue.append_front(x)!= success)
            cout << "Operation failed: because the deque is full" << endl << endl;
          else
    	cout << x << " appended to front of deque." << endl << endl;
          break;
        case 'a':
          cout << "Enter new key to insert:" << flush;
          cin >> x;
          if (test_queue.append_rear(x)!= success)
            cout << "Operation failed: because the deque is full" << endl << endl;
          else
    	cout << x << " appended to rear of deque. " << endl << endl;
          break;
        case 'h':
          help();
          break;
      }
      return continue_input;
    } //end do_command()
    
    char get_command()
    /*
    Post: Gets a valid command from the user and,
    after converting it to lower case if necessary,
    returns it.
    */
    {
      char c;
      cout << "Select command and press <Enter>:";
      cin >> c;
      c = tolower(c);
      while ((c != 'a') && (c != 'p') && (c != 's') && (c != 'x') &&
            (c != 'r') && (c != 'w') && (c != 'h') && (c != 'q')) {
        cout << "Please enter a valid command or H for help:" << endl;
        cin >> c;
        c = tolower(c);
      }
      return c;
    } //end get_command
    
    

    Home | | Syllabus | | Assignments | | Lecture Notes