-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearching_Sorting.cpp
117 lines (117 loc) · 2.9 KB
/
Searching_Sorting.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std;
typedef struct rec
{
char name[20];
char birthdt[20];
char phone[11];
}node;
node temp;
vector<node> rec;
vector<node>::iterator ptr;
bool compare(node &r1, node &r2)
{
if(strcmp(r1.name, r2.name) < 0)
{
return true;
}
else
{
return false;
}
}
void create()
{
int n, i;
cout << "How many elements do you want to insert: ";
cin >> n;
cout << "Enter the elements in the database:"<<endl;
for(i = 0; i < n; i++)
{
cout << "Name: ";
cin >> temp.name;
cout << "Birthdt(dd-mm-yy): ";
cin >> temp.birthdt;
cout << "Phone number: ";
cin >> temp.phone;
rec.push_back(temp);
}
}
void display()
{
cout << "\tThe contents of the database are:" << endl;
cout << "--------------------------------------------------" << endl;
cout << "NAME DATE OF BIRTH PHONE NUMBER" << endl;
cout << "------------------------------------------------------" << endl;
for(ptr = rec.begin(); ptr != rec.end(); ptr++)
{
cout << (*ptr).name << " " << (*ptr).birthdt << " " << (*ptr).phone << endl;
}
}
void searching()
{
char key[20];
int flag = 0;
cout << "Enter the name which you want to search: ";
cin >> key;
for(ptr = rec.begin(); ptr != rec.end(); ptr++)
{
if(strcmp((*ptr).name, key) == 0)
{
flag = 1;
break;
}
else
{
flag = 0;
}
}
if(flag == 1)
{
cout << "Desired element is present in the database:" << endl;
}
else
{
cout << "Desired element is not present in the database:" << endl;
}
}
void sorting()
{
sort(rec.begin(), rec.end(), compare);
cout << "Record is sorted!" << endl;
}
int main()
{
char ans = 'y';
int choice;
cout << "\t Program for searching and sorting" << endl;
do
{
cout << "Main Menu" << endl;
cout << "1. Create a database" << endl;
cout << "2. Display the database" << endl;
cout << "3. Search the element in the database" << endl;
cout << "4. Sort the database (Name)" << endl;
cout << "Enter the choice what do you want: ";
cin >> choice;
switch(choice)
{
case 1: create();
break;
case 2: display();
break;
case 3: searching();
break;
case 4: sorting();
display();
break;
}
cout << "Do you want to go back to the menu(y): ";
cin >> ans;
} while(ans == 'y');
return 0;
}