-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
395 lines (354 loc) · 10.2 KB
/
Copy pathmain.cpp
File metadata and controls
395 lines (354 loc) · 10.2 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include <iostream>
#include <map>
#include <set>
#include <unordered_map>
#ifdef _WIN32
#include <windows.h>
#endif
using namespace std;
#define PHONE_SIZE 5
#define ANSI_RESET "\033[0m"
#define ANSI_COLOR_RED "\033[31m"
#define ANSI_COLOR_GREEN "\033[32m"
#define ANSI_COLOR_YELLOW "\033[33m"
#define ANSI_COLOR_BLUE "\033[34m"
#define ANSI_COLOR_MAGENTA "\033[35m"
#define ANSI_COLOR_CYAN "\033[36m"
#define ANSI_COLOR_WHITE "\033[37m"
void SetColor(int value){
// 2, 6, 7, 8, 10, 12, 15
#ifdef _WIN32
// Code that works on Windows operating system
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), value);
#else
// Code that works on Mac/Unix operating system
switch (value) {
case 2:
std::cout << ANSI_COLOR_RED;
break;
case 6:
std::cout << ANSI_COLOR_GREEN;
break;
case 7:
std::cout << ANSI_COLOR_MAGENTA;
break;
case 8:
std::cout << ANSI_COLOR_CYAN;
break;
case 10:
std::cout << ANSI_COLOR_YELLOW;
break;
case 12:
std::cout << ANSI_COLOR_BLUE;
break;
case 15:
std::cout << ANSI_COLOR_WHITE;
break;
default:
// Reset to default color if an invalid code is provided
std::cout << ANSI_RESET;
break;
}
#endif
}
class Trie{
private:
struct TrieNode{
map <char, TrieNode*> children;
bool endOfWord;
};
typedef TrieNode* TN;
TN root;
public:
Trie(){
root = new TrieNode();
root->endOfWord = false;
}
void Insert(string word){
TN current = root;
for(int i=0;i<word.length();i++){
char c = word[i];
TN node = current->children[c];
if(node == nullptr){
node = new TrieNode();
current->children[c] = node;
}
current = node;
}
current->endOfWord = true;
}
bool Search(string word){
TN current = root;
for(int i=0;i<word.length();i++){
char ch = word[i];
TN node = current->children[ch];
if(node == nullptr){
return false;
}
current = node;
}
return current->endOfWord;
}
void Delete(string word){
deleteUtil(root, word, 0);
}
bool deleteUtil(TN current, string word, int index){
if(index == word.length()){
if(!current->endOfWord){
return false;
}
current->endOfWord = false;
return (current->children.size() == 0);
}
char ch = word[index];
TN node = current->children[ch];
if(node == nullptr){
return false;
}
bool shouldDeleteCurrentNode = deleteUtil(node, word, index+1);
if(shouldDeleteCurrentNode){
current->children.erase(ch);
//current->children.erase(current->children.find(ch));
return (current->children.size() == 0);
}
return false;
}
// auto complete
void findwordUtil(TN curr, string temp, set<string> &l){
if(curr->endOfWord){
l.insert(temp);
}
for(int i=0;i<10;i++){
char c = i+'0';
if(curr->children[c] != nullptr){
//cout << "test : " << temp + c << endl;
findwordUtil(curr->children[c], temp + c, l);
}
}
}
void findwordUtilName(TN curr, string temp, set<string> &l){
if(curr->endOfWord){
l.insert(temp);
}
for(int i=0;i<26;i++){
char c = i+'a';
if(curr->children[c] != nullptr){
//cout << "test : " << temp + c << endl;
findwordUtilName(curr->children[c], temp + c, l);
}
}
}
void findwords(TN curr, string str, set<string> &l){
string temp = "";
for(int i=0;i<str.length();i++){
char c = str[i];
temp += c;
if(curr->children[c] == nullptr){
return;
}
curr = curr->children[c];
}
//cout << temp << endl;
findwordUtil(curr, temp, l);
for(auto w : l){
w = temp + w;
}
}
void findwordsName(TN curr, string str, set<string> &l){
string temp = "";
for(int i=0;i<str.length();i++){
char c = str[i];
temp += c;
if(curr->children[c] == nullptr){
return;
}
curr = curr->children[c];
}
//cout << temp << endl;
findwordUtilName(curr, temp, l);
for(auto w : l){
w = temp + w;
}
}
set<string> autocomplete(string word){
set<string> l;
findwords(root,word, l);
return l;
}
set<string> autocompleteName(string word){
set<string> l;
findwordsName(root,word,l);
return l;
}
};
class PhoneDirectory{
Trie t,t_name;
unordered_map <string, string> m;
unordered_map <string, string> m_inverse;
public:
bool Insert(string name, string phone){
if(!t.Search(phone)){
m.insert(make_pair(phone, name));
m_inverse.insert(make_pair(name, phone));
t.Insert(phone);
t_name.Insert(name);
return true;
}
return false;
}
bool isAvail(string phone){
return t.Search(phone);
}
void aCompletePrint(string ph){
set<string> l;
l = t.autocomplete(ph);
for(auto ph : l){
cout << ph << endl;
}
}
void aCompletePrintName(string name){
set<string> n;
n = t.autocompleteName(name);
for(auto nam : n){
cout << nam << endl;
}
}
void del(string ph){
if(t.Search(ph)){
t.Delete(ph);
SetColor(6);
cout << "Number has been removed from phone directory\n";
SetColor(7);
}else{
SetColor(6);
cout << "Number not found in the phone directory\n";
SetColor(7);
}
}
bool findPrint(string ph){
set<string> l = t.autocomplete(ph);
bool found = false;
for(auto pNo : l){
if(m.find(pNo) != m.end()){
cout << pNo << " : " << m[pNo] << endl;
found = true;
}
}
return found;
}
bool findPrintName(string name){
set<string> l = t_name.autocompleteName(name);
bool found = false;
for(auto nam : l){
//cout << nam << endl;
if(m_inverse.find(nam) != m_inverse.end()){
cout << nam << " : " << m_inverse[nam] << endl;
found = true;
}
}
return found;
}
};
bool isValidPhone(string p){
if(p.size() == PHONE_SIZE){
for(int i=0;i<p.size();i++){
if(p[i] >= '0' && p[i] <= '9'){
continue;
}else{
return false;
}
}
return true;
}else{
return false;
}
}
int main()
{
PhoneDirectory pd;
SetColor(10);
cout << "Welcome to Phone Directory 1.0\n";
cout << "Press escape/e key anytime to exit the program\n\n";
SetColor(8);
cout << "Enter i to insert \n";
cout << "Enter d to delete \n";
cout << "Enter f to find by Phone\n";
cout << "Enter F to find by Name\n";
cout << "\n";
SetColor(7);
while(1){
cout << endl;
char c;
SetColor(4);
cout << "Enter input : ";
SetColor(12);
cin >> c;
SetColor(7);
if(c == 'e' || c == 'E'){
break;
}
if(c == 'i' || c == 'I'){
string n, p;
cout << "Enter name : " << flush;
SetColor(15);
cin.ignore();
getline(cin, n, '\n');
SetColor(7);
while(1){
while(1){
cout << "Enter " << PHONE_SIZE << " digit phone number : ";
SetColor(15);
cin >> p;
SetColor(7);
if(!isValidPhone(p)){
SetColor(2);
cout << "Enter a valid phone number \n";
SetColor(7);
}else{
break;
}
}
if(pd.Insert(n, p)){
SetColor(6);
cout << "Number added to the phone directory\n";
SetColor(7);
break;
}else{
SetColor(2);
cout << "Number already exists in phone directory\n";
SetColor(7);
}
}
}else if(c == 'f'){
string f;
cout << "Enter the number to find : ";
SetColor(15);
cin >> f;
if(!pd.findPrint(f)){
SetColor(2);
cout << "No Matching number\n";
SetColor(7);
}
SetColor(7);
}else if(c == 'd' || c == 'D'){
string d;
cout << "Enter a number to delete : ";
SetColor(15);
cin >> d;
SetColor(7);
pd.del(d);
}else if(c == 'F'){
string f;
cout << "Enter the name to find : ";
SetColor(15);
cin >> f;
if(!pd.findPrintName(f)){
SetColor(2);
cout << "No Matching name\n";
SetColor(7);
}
SetColor(7);
}
}
return 0;
}