/* Binary String Search - This program uses a modified version of the
binary search algorithm presented in chapter 8, so that it searches
an array of strings instead of an array of integers. */
#include "Utility.h"
string getName();
void selectionSort(string[], int);
void displaySortedList(const string[], int);
void binarySearch(const string[], const string, int);
int main()
{
const int NUM_NAMES = 20;
/* Array holding first and last names */
string names[NUM_NAMES] = { "Collins, Bill", "Smith, Bart", "Allen, Jim",
"Griffin, Jim", "Stamey, Marty", "Rose, Geri",
"Taylor, Terri", "Johnson, Jill", "Allison, Jeff",
"Looney, Joe", "Wolfe, Bill", "James, Jean",
"Weaver, Jim", "Pore, Bob", "Rutherford, Greg",
"Javens, Renee", "Harrison, Rose", "Setzer, Cathy",
"Pike, Gordon", "Holland, Beth" };
char again = ' ';
string searchName = " ";
/* Allows the user to search for names repeatedly */
do
{
/* Gets a name to be searched for */
searchName = getName();
/* Performs a selection sort on the array containing
the list of names */
selectionSort(names, NUM_NAMES);
/* Displays the sorted name list */
//displaySortedList(names, NUM_NAMES);
/* Searches and displays a message if the name entered exists
in the names array */
binarySearch(names, searchName, NUM_NAMES);
cout << "\n\t\tDo you wish to perform another search? ";
cin >> again;
if (again == 'N' || again == 'n')
{
cout << "\n\t\tThanks for using our database ...";
}
} while (!(again == 'N' || again == 'n'));
pauseSystem();
return 0;
}
/* **********************************************************
Definition: getName
This function gets a name from the user to be searched for
in the array names containing a list of first and last
names.
********************************************************** */
string getName()
{
string firstName = " ",
lastName = " ";
string name = " ";
cout << "\n\t\tName Search Database\n\n"
<< "\t\tEnter the last followed by the first name of\n"
<< "\t\tthe person you wish to look up in our database.\n"
<< "\t\tEx: Collins Bill\n\n";
cout << "\t\tEnter a name: ";
cin >> lastName >> firstName;
return name = (lastName + ", " + firstName);
}
/* **********************************************************
Definition: selectionSort
This function accepts names and NUM_Names, containing the
number of elements stored in the array, as its arguments.
It uses election sort algorithm is used to sort the names
in alphabetical order.
********************************************************** */
void selectionSort(string names[], int NUM_NAMES)
{
int startScan = 0,
minIndex = 0,
index = 0;
string minAlpha = " ";
for (startScan = 0; startScan < (NUM_NAMES - 1); startScan++)
{
minIndex = startScan;
minAlpha = names[startScan];
for (index = startScan + 1; index < NUM_NAMES; index++)
{
if (names[index] < minAlpha)
{
minAlpha = names[index];
minIndex = index;
}
}
names[minIndex] = names[startScan];
names[startScan] = minAlpha;
}
}
/* **********************************************************
Definition: binarySearch
This function accepts names, searchName, and NUM_NAMES
containing the number of elements stored in names as its
arguments. It searches for a name entered by the user in
the sorted name list. If the name exists, a message is
displayed informing the user that the name has been found.
Otherwise a message indicating that the name does is not
stored in the database is displayed.
********************************************************** */
void binarySearch(const string names[], const string searchName,
int NUM_NAMES)
{
int firstElem = 0,
lastElem = NUM_NAMES - 1,
midpoint = 0,
position = - 1;
bool nameFound = false;
while (!nameFound && firstElem <= lastElem)
{
/* Calculate the midpoint */
midpoint = (firstElem + lastElem) / 2;
names[midpoint] == searchName ? nameFound = true, position = midpoint :
names[midpoint] > searchName ? lastElem = midpoint - 1 :
firstElem = midpoint + 1;
}
nameFound ? cout << "\n\t\tThe name " << searchName
<< " has been found in our database\n" :
cout << "\n\t\tThe name " << searchName
<< " could not be found in our database ...\n";
}
/* **********************************************************
Definition: displaySortedList
This function accepts names and NUM_Names, containing the
number of elements stored in the array, as its arguments.
It verifies that the sorting function is working correctly
by displaying the list of names.
********************************************************** */
void displaySortedList(const string names[], int NUM_NAMES)
{
cout << "\n\t\tName Database - Sorted List:\n\n";
for (int index = 0; index < NUM_NAMES; index++)
{
cout << "\t\t" << names[index] << " \n";
}
cout << "\n";
}
Tuesday, March 7, 2017
Programming Challenge 8.7 - Binary String Search
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment