-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab14.cpp
90 lines (86 loc) · 2.55 KB
/
Lab14.cpp
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
#include<iostream>
#include<iomanip>
using namespace std;
int getName(string names[],string tele[]){
int n=1;
cout<<"Enter a name or 'quit' to stop entry-> ";cin>>names[n];
while(names[n].compare("quit")!=0 && n<10){
cout << "Enter Telephone for " << names[n] << ": ";
cin >> tele[n];
cout << "--------------------\n";
n++;
if (n<10) {
cout << "Enter a name or 'quit' to stop entry-> ";
cin >> names[n];
}
};
return n-1;
}
int lookup (int n,string lookfor, string names[])
{
int first=1, last=n, middle;
bool found=false;
while (!found && first <= last)
{
middle = (first+last)/2;
if (names[middle].compare(lookfor)==0)
found = true;
else if (names[middle].compare(lookfor)<0) first = middle+1;
else last = middle-1;
}
if (found) return middle;
else return 0;
}
void sort(int n,string (&name)[10], string (&tele)[10]){
int i;
string ntemp,ttemp;
bool sorted = false;
while (!sorted)
{
sorted = true;
for(i=1;i<=n-1;i++){
if(name[i].compare(name[i+1])>0){
ntemp = name[i];
name[i] = name[i+1];
name[i+1] = ntemp;
ttemp = tele[i];
tele[i] = tele[i+1];
tele[i+1] = ttemp;
sorted = false;
}
}
}
}
void Display(int n,string name[],string tele[]){
string lookfor;
int i, location;
cout << "Sorted names \n";
cout<<setw(3)<<"ID"<<setw(10)<<"Name"<<setw(15)<<"Telephone"<<endl;
for (i=1;i <=n; i++) cout <<setw(3)<< i <<setw(10)<< name[i] <<setw(15)<<tele[i] <<endl;
cout << "SEARCH ROUTINE. ENTER quit TO STOP\n";
cout << "Enter a name to lookup telephone number: ";cin >> lookfor;
while (lookfor.compare("quit")!=0)
{
location = lookup (n, lookfor, name);
if (location >0){
cout << lookfor <<" found at location " << location << ", Telephone# " << tele[location] << endl;
cout<<"----------------------------\n";
}
else {
cout << "Name not in list! \n";
cout<<"----------------------------\n";
}
cout << "Enter a name to lookup telephone number: ";
cin >> lookfor;
}
}
int main(int argc, char const *argv[])
{
string names[10], tele[10];
int n;
n = getName(names, tele);
cout << "\n*******************************\n";
sort(n, names, tele);
Display( n, names, tele);
return 0;
}