Sunday, February 25, 2018

Programming Challenge 16.9 - SearchableVector Modification

Example Files: SearchableVectorModification.7z

Notice: Since no changes have been made to the SimpleVector class, it is included in the Example Files archive, but not republished here. Most of the code for this project is taken from pp. 1020 through 1022 in the 9th Edition, or 1002 through 1004 in the 8th Edition of this book.

SearchableVector.h


#ifndef SEARCHABLE_VECTOR_H_
#define SEARCHABLE_VECTOR_H_

#include "SimpleVector.h"

template <class T>
class SearchableVector : public SimpleVector<T>
{
    public:
        // Constructors
        SearchableVector() : SimpleVector<T>()
        { }

        SearchableVector(int size) : SimpleVector<T>(size)
        { }

        SearchableVector(const SearchableVector &);

        // Accessor function
        int findItem(const T &);
};

/* **********************************************************
            SearchableVector<T>::SearchableVector()
         - Copy Constructor
   ********************************************************** */

template <class T>
SearchableVector<T>::SearchableVector(const SearchableVector &obj) :
                             SimpleVector<T>(obj.getArraySize())
{
    for (int count = 0; count < this->getArraySize(); count++)
    {
        this->operator[](count) = obj[count];
    }
}

/* **********************************************************
            SearchableVector<T>::findItem() : const T
    This function uses a binary search algorithm to find an
    item. If the item is found the subscript is returned.
    Otherwise -1 is returned.
   ********************************************************** */

template <class T>
int SearchableVector<T>::findItem(const T &item)
{
    int  firstElem = 0;
    int  midPoint = 0;
    int  lastElem = this->getArraySize() - 1;
    int  position = -1;
    bool status = false;

    while (status == false && firstElem <= lastElem)
    {
        midPoint = (firstElem + lastElem) / 2;

        if (this->operator[](midPoint) == item)
        {
            position = midPoint;
           status = true;
        }
       
        this->operator[](midPoint) > item ? lastElem = midPoint - 1 :
                                                        firstElem = midPoint + 1;
    }

    return position;
}

#endif

SearchableVectorMod.cpp


#include "SearchableVector.h"

#include <iostream>
using std::cin;
using std::cout;

#include <string>
using std::string;


/* **********************************************************
            findVal() : const <T> &obj, const T &, const string
    This function searches for a value in the object's array.
    If -1 is returned from the findItem() function, a message
    is output to indicate the fact that the value does not
    exist. Otherwise the value and subscript it was found at
    is output.
   ********************************************************** */

template <class T>
void findVal(SearchableVector<T> &obj, const T &val)
{
    int result = 0;

    result = obj.findItem(val);

    if (result == -1)
    {
        cout << val << " does not exist ...\n\n";
    }
    else
    {
        cout << val << " was found at subscript position "
              << result << "\n\n";
    }
}

/* **********************************************************
            print() : const <T> &obj, const string
    Outputs the object's current contents to screen.
    ********************************************************** */

template <class T>
void print(const SearchableVector<T> &obj, const string objName)
{
    cout << "These values are currently in your " << objName << ":\n";
    cout << obj << "\n\n";
}

/* **********************************************************
                            Function Definition
   ********************************************************** */

void printIntro();
void printOptions(const string *);

/* **********************************************************
                                        main()
   ********************************************************** */

int main()
{
    const int SIZE = 12;
    int       intVal = 0;
    int       choice = ' ';
    double    doubleVal = 0.0;
    char      charVal = ' ';
   
    const enum   options { INT = 1, DOUBLE = 2, CHAR = 3, PRINT = 4, QUIT = 5 };
    const string objNames[] = { "intTable", "doubleTable", "charTable" };

    SearchableVector<int>     intTable(SIZE);
    SearchableVector<double> doubleTable(SIZE);
    SearchableVector<char>   charTable(SIZE);

    printIntro();

    for (int count = 0; count < SIZE; count++)
    {
        intTable[count] = (count * 2);
        doubleTable[count] = (count * 2.14);
        charTable[count] = ((char)count - 191);  
    }

    do
    {
        printOptions(objNames);
        cout << "Choice: ";
        cin >> choice;

        while (choice < INT || choice > QUIT)
        {
            cout << "Choice: ";
            cin >> choice;
        }
        cout << "\n";

        switch (choice)
        {
            case INT:
            {
                cout << "Enter an integer value to search for: ";
                cin >> intVal;

                findVal(intTable, intVal);
            } break;

            case DOUBLE:
            {
                cout << "Enter an floating-point value to search for: ";
                cin >> doubleVal;

                findVal(doubleTable, doubleVal);
            } break;

            case CHAR:
            {
                cout << "Enter a character to search for: ";
                cin >> charVal;

                findVal(charTable, charVal);
            } break;

            case PRINT:
            {
                print(intTable, objNames[0]);
                print(doubleTable, objNames[1]);
                print(charTable, objNames[2]);
            } break;

            case QUIT:
            {
                cout << "Thank you for trying this program! Have a nice day!";
            } break;
        }
    } while (choice != QUIT);

    cin.get();
    cin.ignore();
   return 0;
}

/* **********************************************************
            printIntro() : const int
    Outputs information about the program's purpose and
    functionality.
   ********************************************************** */

void printIntro()
{
    cout << "SEARCHABLE VECTOR DEMO\n\n";
    cout << "This program let's you search for a value in an integer,\n"
           << "a floating-point, and a character array.\n\n";
    cout << "If the value exists, it will be output to screen. Otherwise\n"
          << "you will see a message telling you that the value does not exist.\n";
    cout << "You can repeat a search as many times as you wish to, or you\n"
          << "can quit the program and try again another time.\n\n";
}

/* **********************************************************
            printOptions() : const string *
    Outputs the available menu options.
   ********************************************************** */

void printOptions(const string *objNames)
{
    cout << "SEARCH TABLE:\n\n";
    cout << "1) " << objNames[0] << "\n";
    cout << "2) " << objNames[1] << "\n";
    cout << "3) " << objNames[2] << "\n";
    cout << "4) " << "Print Values\n";
    cout << "5) Quit\n\n";
}

Example Output:






No comments:

Post a Comment