-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
638 lines (616 loc) · 17.8 KB
/
Copy pathmain.cpp
File metadata and controls
638 lines (616 loc) · 17.8 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
#include<bits/stdc++.h>
using namespace std;
struct contact{
string fname,lname,num,email,job;
};
int Num; //used to read number of contact from file
int LEFT=0,RIGHT=0; //used in searching algorithm
contact h; //used to store data
vector <contact> v; //used to create a vector of contact
string VNum; //used to change integer Num variable to string variable
vector<pair<contact,int> >tmp; //used to create vector of pair
//contact in first and position in second
//to use in delete and update function
//Function to help in sorting struct with first name
bool FIRST(const contact &i, const contact &j){
return i.fname < j.fname;
}
//Function to get Number of Contact from file
int vector_Number(){
ifstream file("vector_number.txt");
getline(file,VNum);
stringstream VecNum(VNum);
VecNum >> Num;
file.close();
return Num;
}
//Function to Update Number of contact in file
void UpdateVec_Number(int New_vecNum){
ofstream file("vector_number.txt");
file<< New_vecNum <<endl;
file.close();
}
//function to update contact data with a good layout
void layout(vector<contact>&v){
ofstream file("Vector_Print_Data.txt");
for(int i=0;i<Num;i++){
char ch[21]=" *****************";
file<<" First Name: "<<v[i].fname<<"\n"
<<" Last Name: "<<v[i].lname<<"\n"
<<" Mobile Number: "<<v[i].num<<"\n"
<<" Email: "<<v[i].email<<"\n"
<<" Job: "<<v[i].job<<"\n";
file<<ch<<endl;
}file.close();
}
//Function to update contact data in a sorted way
void sorted_Data(vector<contact>&v){
ofstream file("Vector_Data.txt");
for(int i=0; i<Num; ++i)
{
file<<v[i].fname<<"\n"
<<v[i].lname<<"\n"
<<v[i].num<<"\n"
<<v[i].email<<"\n"
<<v[i].job<<endl;
}
file.close();
}
//function to store contact data
void Update_Data(string a,string b,string c,string d,string e){
ofstream file("Data.txt",ofstream::app);
file<<a<<"\n"
<<b<<"\n"
<<c<<"\n"
<<d<<"\n"
<<e<<endl;
file.close();
}
//function to get contact data from file when open project
void ReadVector_Data(){
v.clear();
ifstream file("Data.txt");
vector_Number();
contact temp;
string a,b,c,d,e;
for(int i=0; i<Num; i++)
{
getline(file,a);
temp.fname=a;
getline(file,b);
temp.lname=b;
getline(file,c);
temp.num=c;
getline(file,d);
temp.email=d;
getline(file,e);
temp.job=e;
v.push_back(temp);
}
file.close();
sort(v.begin(),v.end(),FIRST);
sorted_Data(v);
layout(v);
}
bool Check_Number(string x){
bool t=false;
for(int i=0;i<x.size();++i){
if(isdigit(x[i])||x.size()>50){
t=true;
}
}
return t;
}
bool Check_alpha(string x){
bool t=false;
for(int i=0;i<x.size();++i){
if(isalpha(x[i]))
t=true;
}
return t;
}
bool Check_Email(string x){
bool t=false;
int s=x.find('@');
int ss=x.find(".com");
if(s<1||s+1>=ss){
t=true;
}
return t;
}
//Function to add contact
void add(vector<contact>&v,contact h){
bool t=false;
do{
t=false;
cout<<"\nContact first name: ";
cin>>h.fname;
if(Check_Number(h.fname))
cout<<"\n\tPlease enter a valid name.\n\n";
else t=true;
}while(!t);
do{
t=false;
cout<<"\nContact last name: ";
cin>>h.lname;
if(Check_Number(h.lname))
cout<<"\n\tPlease enter a valid name.\n\n";
else t=true;
}while(!t);
do{
t=false;
cout<<"\nContact mobile number: ";
cin>>h.num;
if(h.num.size()!=11 || Check_alpha(h.num))
cout<<"\n\tPlease enter a valid mobile number.\n\n";
else t=true;
}while(!t);
do{
t=false;
cout<<"\nContact email: ";
cin>>h.email;
if(Check_Email(h.email))
cout<<"\n\tPlease enter a valid email.\n\n";
else t=true;
}while(!t);
do{
t=false;
cout<<"\nContact job: ";
cin>>h.job;
if(Check_Number(h.job))
cout<<"\n\tPlease enter a valid job name.\n\n";
else t=true;
}while(!t);
Update_Data(h.fname,
h.lname,
h.num,
h.email,
h.job);
vector_Number();
Num++;
UpdateVec_Number(Num);
ReadVector_Data();
}
//function to print contact
void view_all(){
string line;
ifstream file("Vector_Print_Data.txt");
while(getline(file,line))
cout<<line<<endl;
}
//Binary Search Upper bound function to get all right equal contact
int Upper_bound(vector<contact>&v,string s,int n){
int lo = 0, hi = n;
while (lo < hi) {
int mid = lo + (hi - lo)/2;
if (v[mid].fname > s)
hi = mid;
else
lo = mid + 1;
}
return lo;
}
//Binary Search Lower bound function to get all left equal contact
int Lower_bound(vector<contact>&v,string s,int n){
int lo = 0, hi = n;
while (lo < hi) {
int mid = lo + (hi - lo)/2;
if (v[mid].fname < s)
lo = mid + 1;
else
hi = mid;
}
return lo;
}
//function to Binary search on first name
//because it is sorted by first name
void Binary_search_F(vector<contact>&v,string s,int n){
tmp.clear();
if(!v.empty()){
LEFT=Lower_bound(v,s,n);
RIGHT=Upper_bound(v,s,n);
for(int i=LEFT;i<RIGHT;i++){
cout<<"\nFound:"
<<"\nFirst Name:\t"<<v[i].fname
<<"\nLast Name:\t"<<v[i].lname
<<"\nMobile Number:\t"<<v[i].num
<<"\nEmail:\t"<<v[i].email
<<"\nJob:\t"<<v[i].job
<<"\nAt Position\t"<<i+1
<<"\n******************\n";
tmp.push_back(make_pair(v[i],i));
}
if(tmp.size()==0)
cout<<"Not Found"<<endl;
}
else {
cout<<"Contact is Empty"<<endl;
}
}
//function to linear search on last name,number,email or job
vector<pair<contact,int> > Linear_search(int P,vector<contact>&v,string s,int n){
int pos=0;
tmp.clear();
if(!v.empty()){
for(int i=0;i<n;++i){
if(P=='L' || P=='l'){
if(v[i].lname==s){
pos=i;
tmp.push_back(make_pair(v[i],pos));
}
}
else if(P=='N'||P=='n'){
if(v[i].num==s){
pos=i;
tmp.push_back(make_pair(v[i],pos));
}
}
else if(P=='E'||P=='e'){
if(v[i].email==s){
pos=i;
tmp.push_back(make_pair(v[i],pos));
}
}
else if(P=='J'||P=='j'){
if(v[i].job==s){
pos=i;
tmp.push_back(make_pair(v[i],pos));
}
}
}
return tmp;
}else{cout<<"Contact is Empty"<<endl;}
}
//function to delete contact
void Delete(vector<contact>&v,string s,int m){
int pos;
bool t=false;
Binary_search_F(v,s,m);
do{
cout<<"Chose by position which one you need to delete"<<endl;
cin>>pos;
if(pos<LEFT || pos>RIGHT)
cout<<"Enter right Position"<<endl;
else{ t=true;
v.erase(v.begin()+pos-1);
cout<<"Contact Deleted"<<endl;
}
}while(!t);
UpdateVec_Number(Num-1);
ofstream file("Data.txt");
vector_Number();
for(int i=0; i<Num; ++i){
file<<v[i].fname<<"\n"
<<v[i].lname<<"\n"
<<v[i].num<<"\n"
<<v[i].email<<"\n"
<<v[i].job<<endl;
}
file.close();
ReadVector_Data();
}
//function to edit contact
void Edit(vector<contact>&v,string d,int m){
int pos;
bool t=false;
Binary_search_F(v,d,m);
do{
cout<<"Chose by position which one you need to Edit"<<endl;
cin>>pos;
if(pos<LEFT || pos>RIGHT)
cout<<"Enter right Position"<<endl;
else{ t=true;
char op;
bool t=false;
do{
cout<<"F To Edit in First name\n"
<<"L To Edit in Last name\n"
<<"N To Edit in number\n"
<<"E To Edit in email\n"
<<"J To Edit in job\n"
<<"B To Back to menu\n";
cin>>op;
switch(op){
case 'F':
case 'f':{
string name;
do{
t=false;
cout<<"\nContact new first name: ";
cin>>name;
if(Check_Number(name))
cout<<"\n\tPlease enter a valid name.\n\n";
else t=true;
}while(!t);
v[pos-1].fname=name;
break;
}
case 'L':
case 'l':{
string name;
do{
t=false;
cout<<"\nContact new last name: ";
cin>>name;
if(Check_Number(name))
cout<<"\n\tPlease enter a valid name.\n\n";
else t=true;
}while(!t);
v[pos-1].lname=name;
break;
}
case 'N':
case 'n':{
string number;
do{
t=false;
cout<<"\nContact new mobile number: ";
cin>>number;
if(Check_alpha(number)||number.size()!=11){
cout<<"\n\tPlease enter a valid mobile number.\n\n";
}else t=true;
}while(!t);
v[pos-1].num=number;
break;
}
case 'E':
case 'e':{
string email;
bool t=false;
do{
cout<<"\nContact new email: ";
cin>>email;
if(Check_Email(email))
cout<<"\n\tPlease enter a valid email.\n\n";
else t=true;
}while(!t);
v[pos-1].email=email;
break;
}
case 'J':
case 'j':{
string job;
do{
t=false;
cout<<"\nContact New job: ";
cin>>job;
if(Check_Number(job))
cout<<"\n\tPlease enter a valid name.\n\n";
else t=true;
}while(!t);
v[pos-1].job=job;
break;
}
default:{
if(op=='B'|| op=='b')
continue;
else{
cout << "ERROR: Wrong Option answer\n" << endl;
break;
}
}
}}while(op!='B' && op!='b');
}}while(!t);
ofstream file("Data.txt");
vector_Number();
for(int i=0; i<Num; ++i){
file<<v[i].fname<<"\n"
<<v[i].lname<<"\n"
<<v[i].num<<"\n"
<<v[i].email<<"\n"
<<v[i].job<<endl;
}
file.close();
ReadVector_Data();
}
int main(){
char Op; //to choose option user will do
do{
string c,z;
cout<<"\n\tA to Add contact"
<<"\n\tP to Print all contacts"
<<"\n\tS to Search for a contact"
<<"\n\tD to Delete contact"
<<"\n\tU to Update contact"
<<"\n\tQ to Quit\n";
cin>>Op;
system("CLS"); //to Clear Console
switch(Op){
case 'A':
case 'a':{
add(v,h);
break;
}
case 'P':
case 'p':{
view_all();
break;
}
case 'S':
case 's':{
bool t=false; //to keep in program if user put wrong option
char sm;
do{
cout<<"How you need to search?\n"
<<"By First name press 'F'\n"
<<"By Last name press 'L'\n"
<<"By Number press 'N'\n"
<<"By Email press 'E'\n"
<<"By Job press 'J'\n"
<<"To Back to menu press 'B'\n";
cin>>sm;
system("CLS");
switch(sm){
case 'F':
case 'f':{
string s;
do{
t=false;
cout<<"Enter First name You Want To Search\n";
cin>>s;
if(Check_Number(s)){
cout<<"\n\tPlease enter a valid name.\n\n";
}
else{
t=true;
ReadVector_Data();
Binary_search_F(v,s,v.size());
}
}while(!t);
break;
}
case 'L':
case 'l':{
string s;
do{
t=false;
cout<<"Enter Last name You Want To Search\n";
cin>>s;
if(Check_Number(s))
cout<<"\n\tPlease enter a valid name.\n\n";
else{ t=true;
ReadVector_Data();
Linear_search(sm,v,s,v.size());
if(tmp.size()==0)
cout<<"Not Found"<<endl;
else
for(int i=0;i<tmp.size();++i){
cout<<"\nFound:"
<<"\nFirst Name:\t"<<tmp[i].first.fname
<<"\nLast Name:\t"<<tmp[i].first.lname
<<"\nMobile Number:\t"<<tmp[i].first.num
<<"\nEmail:\t"<<tmp[i].first.email
<<"\nJob:\t"<<tmp[i].first.job
<<"\nAt Position\t"<<tmp[i].second
<<"\n******************\n";
}
}
}while(!t);
break;
}
case 'N':
case 'n':{
string s;
do{
t=false;
cout<<"Enter Number You Want To Search\n";
cin>>s;
if(Check_alpha(s)||s.size()!=11)
cout<<"\n\tPlease enter a valid mobile number.\n\n";
else
t=true;
if(t){
ReadVector_Data();
Linear_search(sm,v,s,v.size());
if(tmp.size()==0)
cout<<"Not Found"<<endl;
else
for(int i=0;i<tmp.size();++i){
cout<<"\nFound:"
<<"\nFirst Name:\t"<<tmp[i].first.fname
<<"\nLast Name:\t"<<tmp[i].first.lname
<<"\nMobile Number:\t"<<tmp[i].first.num
<<"\nEmail:\t"<<tmp[i].first.email
<<"\nJob:\t"<<tmp[i].first.job
<<"\nAt Position\t"<<tmp[i].second
<<"\n******************\n";
}
}
}while(!t);
break;
}
case 'E':
case 'e':{
string s;
do{
t=false;
cout<<"Enter Email You Want To Search\n";
cin>>s;
if(Check_Email(s))
cout<<"\n\tPlease enter a valid email.\n\n";
else {t=true;
ReadVector_Data();
Linear_search(sm,v,s,v.size());
if(tmp.size()==0)
cout<<"Not Found"<<endl;
else
for(int i=0;i<tmp.size();++i){
cout<<"\nFound:"
<<"\nFirst Name:\t"<<tmp[i].first.fname
<<"\nLast Name:\t"<<tmp[i].first.lname
<<"\nMobile Number:\t"<<tmp[i].first.num
<<"\nEmail:\t"<<tmp[i].first.email
<<"\nJob:\t"<<tmp[i].first.job
<<"\nAt Position\t"<<tmp[i].second
<<"\n******************\n";
}
}
}while(!t);
break;
}
case 'J':
case 'j':{
string s;
do{
t=false;
cout<<"Enter Job You Want To Search\n";
cin>>s;
if(Check_Number(s))
cout<<"\n\tPlease enter a valid Job name.\n\n";
else{ t=true;
ReadVector_Data();
Linear_search(sm,v,s,v.size());}
if(tmp.size()==0)
cout<<"Not Found"<<endl;
else
for(int i=0;i<tmp.size();++i){
cout<<"\nFound:"
<<"\nFirst Name:\t"<<tmp[i].first.fname
<<"\nLast Name:\t"<<tmp[i].first.lname
<<"\nMobile Number:\t"<<tmp[i].first.num
<<"\nEmail:\t"<<tmp[i].first.email
<<"\nJob:\t"<<tmp[i].first.job
<<"\nAt Position\t"<<tmp[i].second
<<"\n******************\n";
}
}while(!t);
break;
}
case 'B':
case 'b':{
break;
}
default:
cout << "ERROR: Wrong Option answer\n" << endl;
break;
}
}while(sm!='B'&&sm!='b');
break;
}
case 'D':
case 'd':{
cout<<"Enter contact first name you want to delete\n";
cin>>c;
ReadVector_Data();
Delete(v,c,v.size());
break;
}
case 'U':
case 'u':{
cout<<"Enter contact first name you want to edit\n";
cin>>z;
ReadVector_Data();
Edit(v,z,v.size());
break;
}
default:{
if(Op=='Q'|| Op=='q')
cout<<"Phone System Ended"<<endl;
else{
cout << "ERROR: Wrong Option answer\n" << endl;
break;}
}
}
}while(Op!='Q' && Op!='q');
return 0;
}