-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
628 lines (627 loc) · 19.8 KB
/
main.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
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
// Abdalla Fadl Shehata
// september , 2021
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
using namespace std;
//////////////////////////////////////////////
struct ListNode;
class TreeNode;
class FriendsBST;
class UserLL;
//////////////////////////////////////////////
class TreeNode
{
private:
int priority;
string username;
ListNode *user;
TreeNode *right;
TreeNode *left;
public:
TreeNode()
{
left = 0;
right = 0;
}
//////////////////////////////////////////////
TreeNode(string name, ListNode*user_object, TreeNode*r = 0, TreeNode*l = 0)
{
username = name;
user = user_object;
right = r;
left = l;
priority = rand()%100;
}
//////////////////////////////////////////////
TreeNode* getLeft()
{
return left;
}
TreeNode* getRight()
{
return right;
}
string getUsername()
{
return username;
}
ListNode* getUser()
{
return user;
}
int getPriority(){
return priority;
}
//////////////////////////////////////////////
void setLeft(TreeNode *l)
{
left = l;
}
void setRight(TreeNode *r)
{
right = r;
}
void setUsername(int name)
{
username = name;
}
void setUser(ListNode*user_object)
{
user = user_object;
}
};
class FriendsBST {
TreeNode *root;
public :
FriendsBST()
{
root = 0;
}
//////////////////////////////////////////////
void print()
{
inOrder(root);
}
//////////////////////////////////////////////
void inOrder(TreeNode *node)
{
if (node != 0){
inOrder( node->getLeft() );
cout << node->getUsername()<<" " ;
inOrder( node->getRight() );
}
}
//////////////////////////////////////////////
//left rotation
TreeNode *leftRotate(TreeNode *x)
{
TreeNode *y = x->getRight(), *T2 = y->getLeft();
// Perform rotation
y->setLeft(x);
x->setRight(T2);
// Return new root
return y;
}
//////////////////////////////////////////////
//right rotation
TreeNode *rightRotate(TreeNode *y)
{
TreeNode *x = y->getLeft(), *T2 = x->getRight();
// Perform rotation
x->setRight(y);
y->setLeft(T2);
// Return new root
return x;
}
//////////////////////////////////////////////
void addUser(string name, ListNode*user_object){
root = insert(root,name,user_object);
}
//////////////////////////////////////////////
TreeNode* insert(TreeNode*r, string name, ListNode*user_object)
{
if (r == 0){
return new TreeNode(name, user_object);
}
////////////////////////////////////////////////////
// insert right
if (name > r->getUsername() ){
r->setRight(insert(r->getRight(),name,user_object));
// balancing
if (r->getRight()->getPriority() > r->getPriority()){
r = leftRotate(r);
}
}
///////////////////////////////////////////////////////
// insert left
else{
r->setLeft(insert(r->getLeft(),name,user_object));
// balancing
if (r->getLeft()->getPriority() > r->getPriority()){
r = rightRotate(r);
}
}
return r;
}
////////////////////////////////////////////////
ListNode* Find(string name){
TreeNode *temp1= search(root,name);
if (temp1 != nullptr){
ListNode *temp2 = temp1->getUser();
return temp2;
}
return nullptr;
}
//////////////////////////////////////////////
TreeNode* search(TreeNode *root, string name)
{
if (root == 0){
return nullptr ;
}
///////////////////////////////////////////////
if (name==root->getUsername()){
return root;
}
//////////////////////////////////////////////
else if ( root->getLeft() != 0 && name < root->getUsername()){
return search( root->getLeft(),name);
}
//////////////////////////////////////////////
else if ( root->getRight() != 0 && name > root->getUsername()){
return search(root->getRight(),name);
}
////////////////////////////////////////////
else {
return nullptr;
}
}
//////////////////////////////////////////////////////////////////
void deleteUser(string s)
{
root = deleteNode(root, s);
}
/////////////////////////////////////////////////////////////////////
TreeNode* deleteNode(TreeNode* root, string name){
if (root == 0){
return root;
}
if (name < root->getUsername()){
root->setLeft(deleteNode(root->getLeft(),name));
}
else if (name > root->getUsername()){
root->setRight(deleteNode(root->getRight(),name));
}
// IF USER IS A ROOT
//////////////////////////////////////////////////////
// If left is NULL
else if (root->getLeft() == 0)
{
TreeNode *temp = root->getRight();
delete(root);
root = temp; // Make right child as root
}
//////////////////////////////////////////////////////
// If right is NULL
else if (root->getRight() == 0)
{
TreeNode *temp = root->getLeft();
delete(root);
root = temp; // Make left child as root
}
// If user is a root and both left and right are not NULL
else if (root->getLeft()->getPriority() < root->getRight()->getPriority())
{
root = leftRotate(root);
root->setLeft(deleteNode(root->getLeft(),name));
}
else
{
root = rightRotate(root);
root->setRight(deleteNode(root->getRight(), name));
}
return root;
}
/////////////////////////////////////////////
~FriendsBST(){
Clear(root);
}
/////////////////////////////////////////////
void Clear(TreeNode *root)
{
if(root != 0) {
if( (root->getLeft() == 0) && (root->getRight() == 0) ) {
delete root;
root = 0;
} else {
Clear(root->getLeft());
Clear(root->getRight());
}
}
return;
}
/////////////////////////////////////////////
};
//////////////////////////////////////////////
struct ListNode
{
string userName;
string name;
string email;
FriendsBST friends;
ListNode* next;
};
//////////////////////////////////////////////
class UserLL {
private:
ListNode *head , *tail;
public:
UserLL(){
head=nullptr;
tail=nullptr;
}
ListNode* begin (){
return (this->head);
}
void push_back(const string& username , const string& name1 , const string& email1 )
{
ListNode *tmp=new ListNode;
tmp->name=name1;
tmp->userName=username;
tmp->email=email1;
tmp->next=nullptr;
if(this->head==nullptr)
{
this->head=tmp;
this->tail=tmp;
tmp=nullptr;
}
else
{
this->tail->next=tmp;
this->tail=tmp;
}
}
//////////////////////////////////////////////
ListNode *returnNode(string x){
ListNode *n = head;
while (n != nullptr)
{
if (n->userName == x){
return n ;
}
n=n->next;
}
return nullptr;
}
//////////////////////////////////////////////
void addUsers(fstream&file, const string&fileName)
{
string username,name,email;
file.open(fileName, ios::in);
if(!file) {
cout<<"Error, File Not Found"<<endl;
}
file.seekg(0,ios::beg);
while(!file.eof()){
getline(file,username,',');
getline(file,name,',');
getline(file,email);
this->push_back(username , name , email);
file.peek();
}
file.close();
//cout<<"File Opened"<<endl;
}
//////////////////////////////////////////////
void addFriends(fstream &file,const string &fileName){
string name1,name2,temp;
ListNode *temp_node;
file.open(fileName, ios::in);
if(!file) {
cout<<"Error, File Not Found"<<endl;
}
file.seekg(0,ios::beg);
while(!file.eof()){
getline(file,name1,',');
getline(file,temp);
// remove first character (space) from second username
for (int i =1;i<temp.size() ;i++){
name2.push_back(temp[i]);
}
///////////////////////////////////////////////
// add left name as friend to right name
ListNode *n = head ;
while (n != nullptr)
{
if (n->userName == name2){
temp_node = n;
break;
}
else {
n=n->next;
}
}
n = head ;
while (n != nullptr)
{
if (n->userName == name1){
n->friends.addUser(name2,temp_node);
break;
}
else {
n=n->next;
}
}
////////////////////////////////////////////////
// add right name as friend to left name
n = head ;
while (n != nullptr)
{
if (n->userName == name1){
temp_node = n;
break;
}
else {
n=n->next;
}
}
n = head ;
while (n != nullptr)
{
if (n->userName == name2){
n->friends.addUser(name1,temp_node);
break;
}
else {
n=n->next;
}
}
////////////////////////////////////////////////
name2 = "";
file.peek();
}
file.close();
// cout<<"File Opened"<<endl;
}
//////////////////////////////////////////////////////////////
void addFriend(string s1,string s2){
if (s1== s2){
cout<<"**********************************"<<endl;
cout<<" You can't add yourself "<<endl;
cout<<"**********************************"<<endl;
return;
}
ListNode *tmp_node1 = returnNode(s1);
ListNode *tmp_node2 = returnNode(s2);
ListNode*tmp_node3 = tmp_node1->friends.Find(s2);
if (tmp_node3 == nullptr ){
tmp_node1->friends.addUser(s2,tmp_node2);
tmp_node2->friends.addUser(s1,tmp_node1);
cout<<"**********************************"<<endl;
cout<<" You are now friends "<<endl;
cout<<"**********************************"<<endl;
}
else {
cout<<"**********************************"<<endl;
cout<<"You are already friends"<<endl;
cout<<"**********************************"<<endl;
}
}
///////////////////////////////////////////////////////////////
void removeFriend(string s1,string s2){
ListNode *tmp_node1 = returnNode(s1);
ListNode *tmp_node2 = returnNode(s2);
ListNode*tmp_node3 = tmp_node1->friends.Find(s2);
if (tmp_node3 == nullptr){
cout<<"**********************************"<<endl;
cout<<"Friend not found "<<endl;
cout<<"**********************************"<<endl;
}
else {
tmp_node1->friends.deleteUser(s2);
tmp_node2->friends.deleteUser(s1);
cout<<"**********************************"<<endl;
cout<<"Removed Done"<<endl;
cout<<"**********************************"<<endl;
}
}
//////////////////////////////////////////////////////////////
~UserLL(){
while(head != tail){
ListNode *cur=new ListNode;
ListNode *pre=new ListNode;
cur=head;
while(cur->next != nullptr)
{
pre=cur;
cur=cur->next;
}
delete cur;
tail=pre;
pre->next=nullptr;
}
delete head;
delete tail;
}
///////////////////////////////////////////////////
};
//////////////////////////////////////////////////////////////////////
void peopleYouMayKnow(string name,UserLL &l1){
int index = 0 ;
ListNode *people[5];
ListNode *n1= l1.begin();
ListNode* userNode = l1.returnNode(name);
///////////////////////////////////////////////
while(index<5 && n1 != nullptr){
if(n1->userName != userNode->userName && userNode->friends.Find(n1->userName) == nullptr){
people[index] = n1;
index++;
}
n1=n1->next;
}
/////////////////////////////////////////////
if(index == 0){
cout<<"**********************************"<<endl;
cout<<"There is no match people for you"<<endl;
cout<<"**********************************"<<endl;
}
else{
cout<<"**********************************"<<endl;
cout<<"The People You May Know"<<endl;
for(int i=0;i<index;i++){
cout<<(i+1)<<") "<<people[i]->userName<<", "<<people[i]->name<<endl;
}
cout<<"**********************************"<<endl;
}
///////////////////////////////////////////////////////////////
}
/////////////////////////////////////////////////////////
void userMenu()
{
cout<<"1- List all friends "<<endl;
cout<<"2- Search by username "<<endl;
cout<<"3- Add friend "<<endl;
cout<<"4- Remove friend "<<endl;
cout<<"5- People you may know "<<endl;
cout<<"0- Log-out "<<endl;
}
///////////////////////////////////////////////
int main() {
UserLL l1 ;
fstream file;
l1.addUsers(file,"all-users.in");
l1.addFriends(file,"all-users-relations.in");
///////////////////////////////////////////////////////////////////////////
int fChoice=1 ;
while (fChoice){
cout<<"1- Log in."<<endl;
cout<<"0- Exit."<<endl;
cin>>fChoice;
if (fChoice==1){
cout<<"Enter your user-name: ";
string user_name; cin>>user_name;
ListNode *n1= l1.begin(); bool found= false;
while (n1!=nullptr && !found)
{
if (user_name==n1->userName)
{
found= true;
}
n1=n1->next;
}
if (found){
cout<<"welcome "<<user_name<<endl;
int sChoice=1;
while (sChoice){
userMenu();
cin>>sChoice;
if (sChoice==1){
/////////////////////////////////////////////////////
ListNode* node = l1.returnNode(user_name);
cout<<"**********************************"<<endl;
node->friends.print();
cout<<endl;
cout<<"**********************************"<<endl;
cout<<endl;
/////////////////////////////////////////////////////
}
else if (sChoice==2){
//////////////////////////////////////////////
bool b = true;
string user_name_friend;
cout<<"**********************************"<<endl;
cout<<"Enter friend name : ";
cin>> user_name_friend;
////////////////////////////////////////////////////////////
//check user_name_friend there are in linked list or not
ListNode *node = l1.returnNode(user_name_friend);
if (node==nullptr){
b = false ;
cout<<"**********************************"<<endl;
cout<<"friend name not found"<<endl;
cout<<"**********************************"<<endl;
}
if ( b == true){
ListNode *node1 = l1.returnNode(user_name);
ListNode *node2 = node1->friends.Find(user_name_friend);
if (node2 == nullptr){
cout<<"**********************************"<<endl;
cout<<"friend not found"<<endl;
cout<<"**********************************"<<endl;
}
else {
cout<<"**********************************"<<endl;
cout<<node2->userName<<","<<node2->name<<","<<node2->email<<endl;
cout<<"**********************************"<<endl;
}
}
/////////////////////////////////////////////////////////////
}
else if (sChoice==3){
////////////////////////////////////////////////////////
string friend_name;
bool b = false ;
cout<<"**********************************"<<endl;
cout<<"Enter name of friend : ";
cin>> friend_name;
ListNode *node = l1.returnNode(friend_name);
if (node != nullptr){
b = true;
}
if (b){
l1.addFriend(user_name,friend_name);
}
else {
cout<<"**********************************"<<endl;
cout<<"friend name not found"<<endl;
cout<<"**********************************"<<endl;
}
///////////////////////////////////////////////////////
}
else if (sChoice==4){
//////////////////////////////////////////////////////////////////
string friend_name;
bool b = false ;
cout<<"**********************************"<<endl;
cout<<"Enter name of friend : ";
cin>> friend_name;
ListNode *node = l1.returnNode(friend_name);
if (node != nullptr){
b = true;
}
if (b){
l1.removeFriend(user_name,friend_name);
}
else {
cout<<"**********************************"<<endl;
cout<<"friend name not found"<<endl;
cout<<"**********************************"<<endl;
}
///////////////////////////////////////////////////////////////////
}
else if (sChoice==5){
peopleYouMayKnow(user_name,l1);
}
else if (sChoice==0){
user_name="";
cout<<"**********************************"<<endl;
cout<<"log-out successfully."<<endl;
cout<<"**********************************"<<endl;
}
}
}
else {
cout<<"**********************************"<<endl;
cout<<"user name is not found, please try to log-in again."<<endl;
cout<<"**********************************"<<endl;
}
}
else if (fChoice==0)
{
exit(0);
}
}
return 0;
}