1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#include <iostream>
using namespace std;
const int MAXSIZE=20;
void getData(string a[], int n)
{
n=0;
do
{
cout<<"Enter a string, quit to terminate:";
cin>>a[n];
}while((a[n++]!="quit")&&(n<MAXSIZE));
if(n<MAXSIZE)
{--n;}
}
void displayNames(string a[], int& n)
{
for(int x=0; x <= sizeof(n); x++)
{
cout << " The array enter is " << a[x] << endl;
}
}
char getMenuResponse()
{
char response;
cout << endl << "Make your selection" << endl
<< "(A)dd Record, (D)isplay name, (F)ind name, (S)wap names, (R)emove name , (Q)uit" << endl
<< "> ";
cin >> response;
cin.ignore(256, '\n');
return toupper(response);
}
int linSearch(int t, int a[], int n)
{
bool found=false;
for(int i=0; i<n; i++)
{
if(a[i]==t)
{
found=true;
return i;
}
}
if(!found)
return -1;
}
void bSort(string a[], int n)
{
for(int i=1; i<n; i++)
for(int j=0; j<n-i; j++)
if(a[j]>a[j+1])
swap(a[j],a[j+1]);
}
int bSearch(string t, string a[], int n)
{
bool found=false;
int lo=0,
hi=n;
int i;
while(lo<=hi)
{
i=(lo+hi)/2;
if(a[i]==t)
{
found=true;
return i;
}
else if(t>a[i])
lo=i+1;
else
hi=i-1;
}
if(!found)
return -1;
}
int main()
{
string arr[MAXSIZE];
int n;
string ch;
bool run = true;
do
{
switch (getMenuResponse())
{
case 'A': getData(arr,n); break;
case 'F': bSearch(ch, arr, n); break;
case 'D': displayNames(arr,n); break;
case 'S': bSort(arr,n); break;
case 'R': getData(arr,n); break;
case 'Q': run = false; break;
default : cout << "That is NOT a valid choice" << endl;
}
}while(run);
cout << endl << "Program Terminated" << endl;
}
|