-
Notifications
You must be signed in to change notification settings - Fork 0
/
Social_Networking.c
1177 lines (1036 loc) · 36.9 KB
/
Social_Networking.c
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#define MAX_USERS 50
typedef struct {
int userID;
char name[100];
char password[20];
float frndscore;
} User;
typedef struct {
int numUsers;
User users[MAX_USERS];
bool adjacencyMatrix[MAX_USERS][MAX_USERS];
int followersOfUser[MAX_USERS];
} SocialNetwork;
// function declaration's
// before MENU data loadin...
void initSocialNetwork(SocialNetwork *network) ; //initizes the matrix with falsee [needed]
void readuserpass(SocialNetwork *network); //reads the user_pass.txt and stores it in network [needed]
void readData(SocialNetwork *network, const char *filename) ; // reads the data.txt and stores it in the network [needed]
void calculateNoOfFollowrs(SocialNetwork *network ); // find's the no of friends [needed]
// isdh
// menu
void menu(SocialNetwork *network);
void checkadmin(SocialNetwork *network);// op1 : check's if the user is admin or not [needed]
int addNewUser(SocialNetwork *network); //op2 : add's new user[needed]
void addUserToFile(const char *filename, const char *name, const char *friends); // helper : used to add new user details and passsword details into respective files [needed]
void checkuserIsExisting(SocialNetwork *network); //op3 : check's if the user is existing or not [needed]
// functions available for USER'S
void functionsofuser(SocialNetwork *network,char *userlogedin);
int addFriends(SocialNetwork *network , char *name); //op1: add new freinds [needed]
void addOrRemoveFriendship(SocialNetwork *network, int userID1, int userID2,bool add) ; // helper : add's / removes Edges between 2 users [needed]
void addOrRemoveFriendsFromFile(char *filename ,char *userName, char *friendslist , int add) ; // helper: update the data.txt by adding / removing the friends [needed]
void removeFriends(SocialNetwork *network,char *name); //op2 : rmeove old friends [needed]
void getCurrFriendsOfUser(SocialNetwork *network, char *name, char *finalFriends) ; // helper: gets the current friends of USER (after adding / deletin) [needed]
int followingg(SocialNetwork *network , char *name , char *show); // op3: display all the friends of particluar user. [needed]
int followers(SocialNetwork *network , char *name , char *show);
void displayUsersWithIDs(SocialNetwork *network) ; // helper : used to display all the user's with thier ID's [needed]
int areFriends(SocialNetwork *network,char *namez); // op4: used to checek if 2 users's are friends or not [needed]
void removeUserFromFile(SocialNetwork *network,char *namez) ; //op6 : used to DELETE an USER with all his friends from the "data.txt" file and his username and password is also removed from "user_pass.txt" file
void generateDotFile(SocialNetwork *network, const char *filename) ; //op7: used to create a graph.DOT file for graphviz to create a graph [needed]
char* generateColor(); // helper : to generate a random colour [needed]
void showthegraph(); // helper : Used to show the graph in chrome [needed]
void friendscoreOfUser(SocialNetwork *network , char *name); //op8:used to find the FriendScore of loggedin user [needed]
// functions available for ADMINN
void admin(SocialNetwork *network);
int findid(SocialNetwork *network,char nameiz[]);// helper : used to find the USER ID for a given name [needed]
void replaceFile(const char *oldFileName, const char *newFileName) ; //helper : used to handle renaming the new file and deleting the old file [needed]
void displaySocialNetwork(SocialNetwork *network) ; //op2: display's the adjMATRIX [needed]
void opendatafile(); //op7: opens the "data.txt" automatically [needed]
void openpassfile(); //op8: opens the "user_pass.txt" automatically [needed]
void friendscoreOfallUsers(SocialNetwork *network); //op9: used to find the FriendScore for all the user's [needed]
// main stuff
bool BFS(SocialNetwork *network,int src, int dest, int dist[MAX_USERS]);
void findMutualz(SocialNetwork *network , int s);
void searchbar(SocialNetwork *network,char *loggedin,char *addOrRem) ;
// function definitions.....
// before main module functions
void initSocialNetwork(SocialNetwork *network)
{
network->numUsers = 0;
for (int i = 0; i < MAX_USERS; i++)
{
for (int j = 0; j < MAX_USERS; j++)
{
network->adjacencyMatrix[i][j] = false; //set all values to false
}
network->followersOfUser[i] = 0; // set all ranks,ie no of freinds to 0
}
}
void readuserpass(SocialNetwork *network)
{
FILE *file = fopen("user_pass.txt", "r");
char line[100];
int indexx = 0;
while (fgets(line, sizeof(line), file))
{
char *username = strtok(line, ",");
char *password = strtok(NULL, ",");
if (username != NULL && password != NULL)
{
// Remove the newline character from the password, if it exists
int password_len = strlen(password);
if (password_len > 0 && password[password_len - 1] == '\n')
password[password_len - 1] = '\0';
indexx = findid(network,username);
strcpy(network->users[indexx].password, password);
}
}
fclose(file);
}
void readData(SocialNetwork *network, const char *filename)
{
FILE *file = fopen(filename, "r");
char line[256];
// First pass: Collect all users
while (fgets(line, sizeof(line), file) != NULL) // get each line here
{
int line_length = strlen(line);
if (line[line_length - 1] == '\n')
line[line_length - 1] = '\0'; // Remove the trailing newline
char *name = strtok(line, ","); // Get the user's name
int userID = network->numUsers; // Assign a unique user ID (assuming sequential)
// addd it to the network
network->users[userID].userID = userID;
strcpy(network->users[userID].name , name);
network->numUsers++;
}
// Return to the beginning of the file
fseek(file, 0, SEEK_SET);
// Second pass: Establish friendships
while (fgets(line, sizeof(line), file) != NULL) // gets each line
{
int line_length = strlen(line);
if (line[line_length - 1] == '\n')
line[line_length - 1] = '\0';
char *name = strtok(line, ","); // Get the user's name, this is the user so leave it
// Tokenize and process the friends list
char *friend = strtok(NULL, ","); //1st friend
while (friend != NULL)
{
int friend_length = strlen(friend);
if (friend[friend_length - 1] == '\n')
friend[friend_length - 1] = '\0'; // Remove the trailing newline from friend's name
int userID = findid(network,name);
int friendID = findid(network,friend);
if (userID != -1 && friendID != -1)
addOrRemoveFriendship(network, userID, friendID,true);
else
{
if(userID == -1)
printf("\nUser %s not found..\n" , name);
if(friendID == -1)
printf("\nUser %s not found..\n" , friend);
}
friend = strtok(NULL, ",");
}
}
fclose(file);
}
void calculateNoOfFollowrs(SocialNetwork *network )
{
int rank=0;
for (int i = 0; i < network->numUsers; i++)
{
rank = followers(network,network->users[i].name,NULL);
network->followersOfUser[i] = rank ;
}
}
// main module functions :
void checkadmin(SocialNetwork *network)
{
char password[10];
printf("Enter the Admin Password : ");
scanf("%s",password);
if (strcmp(password,"admin")== 0)
{
system("cls");
admin(network);
}
else
{
printf("\n\tWrong Password..\n\tReturning to Main Menu..\n\nEnter any key");
getchar();
getchar();
system("cls");
menu(network);
}
}
int addNewUser(SocialNetwork *network)
{
system("cls");
char name[100];
char friends[256];
char finalfreinds[256];
char password[20];
printf("Enter the new user's name: ");
scanf("%s", name);
int existingUserID = findid(network , name);
if (existingUserID==-1)
{
int userID = network->numUsers;
printf("Enter the new user's password: ");
scanf("%s", password);
int password_len = strlen(password);
if (password[password_len - 1] == '\n')
password[password_len - 1] = '\0';
strcpy(network->users[userID].password,password);
// addd it to the network
network->users[userID].userID = userID;
strcpy(network->users[userID].name , name);
network->numUsers++;
addUserToFile("user_pass.txt",name,password);
addUserToFile("data.txt", name,"");
printf("Add new friends to your network : \n");
searchbar(network,name,"add");
//addFriends(network,name);
printf("\nUser '%s' and their friends added to the network and data.txt.\n", name);
return userID;
}
else
{
printf("The User already exist's.\n");
return 0;
}
}
void addUserToFile(const char *filename, const char *name, const char *friends)
{
FILE *file = fopen(filename, "a");
fprintf(file, "%s,%s\n", name, friends);
fclose(file);
}
void checkuserIsExisting(SocialNetwork *network)
{
char password[10];
char username[50];
bool iscorrectpass = false;
printf("\t\t\t\tLOGINN!!!\n\n");
printf("\nEnter the Username : ");
scanf("%s",username);
int index = findid(network,username);
if(index==-1)
{
printf("\n\tUser does not exist\n\tReturning to Main Menu..\n\nEnter any key");
char ch;
getchar();
getchar();
menu(network);
}
else
{
printf("\nEnter the User Password : ");
scanf("%s",password);
int password_len = strlen(password);
if (password[password_len - 1] == '\n')
password[password_len - 1] = '\0';
if(strcmp(password,network->users[index].password)==0)
iscorrectpass=true;
}
if(iscorrectpass)
{
system("cls");
functionsofuser(network,username);
}
else
{
printf("\n\tWrong Password..\n\tReturning to Main Menu..\n\nEnter any key");
char ch;
getchar();
getchar();
system("cls");
menu(network);
}
}
// admin zfunctions...
int findid(SocialNetwork *network,char nameiz[])
{
for (int i = 0; i < network->numUsers; i++)
{
if (strcmp(network->users[i].name, nameiz) == 0)
return i;
}
return -1;
}
void displaySocialNetwork(SocialNetwork *network)
{
system("cls");
printf("Social Network Graph (Adjacency Matrix):\n\t");
for (int i = 0; i < network->numUsers; i++)
printf("%s\t",network->users[i].name);
printf("\n\n");
// Print the adjacency matrix
for (int i = 0; i < network->numUsers; i++)
{
printf("%s\t",network->users[i].name);// to display the 1st col ie names
for (int j = 0; j < network->numUsers; j++)
{
if (network->adjacencyMatrix[i][j])
printf("YES\t"); // "Yes" represents a friendship
else
printf("Noo\t"); // "No" represents no friendship
}
printf("\n");
}
}
void friendscoreOfallUsers(SocialNetwork *network)
{
float fsc = 0.0;
int i,j,n;
n = network->numUsers;
float scorearr[n];
int noOfFollowerz[n];
char namez[n][50];
for(int i=0;i<n;i++)
{
scorearr[i] = ((float)network->followersOfUser[i] / n)*100.0 ;
noOfFollowerz[i] = network->followersOfUser[i];
strcpy(namez[i], network->users[i].name);
}
// Sort the scorearr in descending order (bubble sort)
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (scorearr[j] < scorearr[j + 1])
{
// Swap the elements if they're out of order
float temps = scorearr[j];
scorearr[j] = scorearr[j + 1];
scorearr[j + 1] = temps;
int tempf = noOfFollowerz[j];
noOfFollowerz[j] = noOfFollowerz[j + 1];
noOfFollowerz[j + 1] = tempf;
char tempn[50] ;
strcpy(tempn, namez[j]);
strcpy(namez[j],namez[j + 1]);
strcpy(namez[j + 1], tempn);
}
}
}
// Display the sorted scores in descending order
printf("Total No of Users = %d \n",network->numUsers);
printf("Infulence Scores are:\n");
for (int i = 0; i < n; i++)
printf("%d\tName: %s\tFollower's: %d\tScore: %.2f\n",i+1 ,namez[i] ,noOfFollowerz[i],scorearr[i]);
}
void openpassfile()
{
system("cls");
const char *fileName = "user_pass.txt";
HINSTANCE result = ShellExecute(NULL, "open", fileName, NULL, NULL, SW_SHOWNORMAL );
// shell execute command
if ((int)result > 32)
printf("\n\n\t\tOpened \"%s\" successfully.\n", fileName);
else
printf("Opening \"%s\" failed.\n", fileName);
}
void opendatafile()
{
system("cls");
const char *fileName = "data.txt";
// handles the instance to a module , OS uses this value to handle exe when its loaded into mememory
// parent window , operation , filename ,
// Parameters passed to the application ,
// The working directory for the application ,
HINSTANCE result = ShellExecute(NULL, "open", fileName, NULL, NULL, SW_SHOWNORMAL );
// shell execute command
// ShellExecute function returns a value greater than 32 if it succeeds.
if ((int)result > 32)
printf("\n\n\t\tOpened \"%s\" successfully.\n", fileName);
else
printf("Opening \"%s\" failed.\n", fileName);
}
// user ufunctions...
void friendscoreOfUser(SocialNetwork *network , char *name)
{
float fsc = 0.0;
int id = findid(network,name);
fsc = ((float)network->followersOfUser[id] / network->numUsers)*100.0 ;
network->users[id].frndscore = fsc;
printf("Friend Score of %s isz %.2f %%\n",name,fsc);
}
void displayUsersWithIDs(SocialNetwork *network)
{
printf("User IDs:\n\n");
int columns = 3;
for (int i = 0; i < network->numUsers; i++)
{
if (i % columns == 0 && i != 0)
printf("\n"); // Start a new row
printf("ID: %d - Name: %s\t", network->users[i].userID, network->users[i].name);
}
printf("\n");
}
void addOrRemoveFriendship(SocialNetwork *network, int userID1, int userID2 , bool add)
{
if (userID1 < network->numUsers && userID2 < network->numUsers)
{
if(add==true)
{
network->adjacencyMatrix[userID1][userID2] = true;
printf("\nAdded %s as your friend.\n");
}
if(add==false)
{
if(network->adjacencyMatrix[userID1][userID2] == false)
printf("\nUser %s is not your friend !\n",network->users[userID2].name);
else
{
network->adjacencyMatrix[userID1][userID2] = false;
printf("\nRemoved %s from your friend's list.\n");
}
}
}
}
char* generateColor()
{
char* color = (char*)malloc(7);
int r = (rand() % 128) + 40; // R: 128-255 (brighter range)
int g = (rand() % 128) + 40; // G: 128-255 (brighter range)
int b = (rand() % 128) + 40; // B: 128-255 (brighter range)
snprintf(color, 7, "#%02X%02X%02X", r, g, b);
return color;
}
void generateDotFile(SocialNetwork *network, const char *filename)
{
FILE *file = fopen(filename, "w");
// Write the DOT file header
fprintf(file, "digraph SocialNetwork {\n");
fprintf(file, " rankdir=LR;\n"); // Horizontal layout
fprintf(file, " graph [size=\"15,20\"];\n");
fprintf(file, " edge [dir=single, arrowhead=vee];\n");
for (int i = 0; i < network->numUsers; i++)
fprintf(file, " %s [style=\"filled\", fillcolor=\"%s\"];\n", network->users[i].name, generateColor());
for (int i = 0; i < network->numUsers; i++)
{
for (int j = 0; j < network->numUsers; j++)
{
if (network->adjacencyMatrix[i][j])
fprintf(file, " %s -> %s [color=\"%s\"];\n", network->users[i].name, network->users[j].name,generateColor());
}
}
fprintf(file, "}\n");
fclose(file);
}
void showthegraph()
{
const char *cmdCommand = "dot -Tsvg -O graph.DOT";
int exitCode = system(cmdCommand);
// system command
if (exitCode == 0)
{
printf("Created the GRAPH successfully.\n\n\t\tOpening the GRAPH SVG File...\n\n");
const char *svgFilePath = "graph.DOT.svg";
// HINSTANCE is a data type used in Windows programming
HINSTANCE result = ShellExecute(NULL, "open", svgFilePath, NULL, NULL, SW_SHOWNORMAL);
}
else
printf("Graph creation failed.\n");
}
void replaceFile(const char *oldFileName, const char *newFileName)
{
// Attempt to delete the old file
if (remove(oldFileName) == 0)
{
// Attempt to rename the new file to the old file's name
if (rename(newFileName, oldFileName) != 0)
perror("Error renaming file");
}
else
perror("Error deleting file");
}
void addOrRemoveFriendsFromFile(char *filename , char *userName, char *friends , int add)
{
FILE *file = fopen(filename, "r");
FILE *tempFile = fopen("tempf.txt", "w");
char line[100];
while (fgets(line, sizeof(line), file))
{
char *username = strtok(line, ",");
char *friendList = strtok(NULL, "\n");
if (username != NULL)
{
if (strcmp(username, userName) == 0)
{
if(add==1 && friendList!=NULL)
{
strcat(friendList,",");
strcat(friendList,friends);
fprintf(tempFile, "%s,%s\n", userName, friendList);
}
if(add==1 && friendList==NULL)
{
fprintf(tempFile, "%s,%s\n",userName, friends);
}
if(add==0 && friendList!=NULL)
fprintf(tempFile, "%s,%s\n", userName, friends);
}
else
{
if(friendList==NULL)
fprintf(tempFile, "%s,\n", username);
else
fprintf(tempFile, "%s,%s\n", username, friendList);
}
}
}
// Close the files
fclose(file);
fclose(tempFile);
// Replace the original file with the temporary file
replaceFile(filename,"tempf.txt");
}
int addFriends(SocialNetwork *network , char *name)
{
char friends[256];
char finalfreinds[256];
int existingUserID = findid(network,name);
printf("Enter the user's friends (comma-separated): ");
scanf("%s", friends);
char *friend = strtok(friends, ",");
strcpy(finalfreinds,"");
while (friend != NULL)
{
int friendUserID = findid(network ,friend);
if (friendUserID != -1)
{
if(network->adjacencyMatrix[existingUserID][friendUserID])
printf("\nUser %s is already your friend..\n",friend);
else
{
if (strlen(finalfreinds) > 0)
strcat(finalfreinds, ",");
strcat(finalfreinds, friend);
addOrRemoveFriendship(network, existingUserID, friendUserID,true);
}
}
if(friendUserID == -1)
printf("\nUser %s not found..\n",friend);
friend = strtok(NULL, ",");
}
if(strcmp(finalfreinds,"")!=0)
addOrRemoveFriendsFromFile("data.txt",name,finalfreinds,1);
return 1;
}
int followers(SocialNetwork *network , char *name , char *show)
{
int userID = findid(network,name);
int n=0;
for (int j = 0; j < network->numUsers; j++)
if (network->adjacencyMatrix[j][userID])
n++;
if(show!=NULL)
{
printf("\n\nThe Follower's of %s are :\n\n",name);
for (int j = 0; j < network->numUsers; j++)
{
if (network->adjacencyMatrix[j][userID])
{
printf("\t%d %s\n",n,network->users[j].name);
n++;
}
}
printf("\nTotal [Follower's]: %d\n",n);
}
else
return n;
}
int followingg(SocialNetwork *network , char *name , char *show)
{
int userID = findid(network,name);
int n=1;
for (int j = 0; j < network->numUsers; j++)
if (network->adjacencyMatrix[userID][j])
n++;
if(show!=NULL)
{
n=1;
printf("\n\nUser %s follow's :\n\n",name);
for (int j = 0; j < network->numUsers; j++)
{
if (network->adjacencyMatrix[userID][j])
{
printf("\t%d %s\n",n,network->users[j].name);
n++;
}
}
printf("\nTotal [Following]: %d\n",n-1);
}
else
return n-1;
}
void getCurrFriendsOfUser(SocialNetwork *network, char *name, char *finalFriends)
{
int userID = findid(network, name);
int n = 1;
strcpy(finalFriends, "");
for (int j = 0; j < network->numUsers; j++)
{
if (network->adjacencyMatrix[userID][j] )
{
// Append the friend's name to finalFriends
if (strlen(finalFriends) > 0)
strcat(finalFriends, ",");
strcat(finalFriends, network->users[j].name);
n++;
}
}
}
int areFriends(SocialNetwork *network,char *namez)
{
//system("cls");
char user1[100];
char user2[100];
if(namez!=NULL)
strcpy(user1,namez);
else
{
printf("\nEnter the first user's name: ");
scanf("%s", user1);
}
int userID1 = findid(network,user1);
if(userID1 == -1)
{
printf("\nUser %s doesn't exist.\n", user1);
return -1;
}
printf("\nEnter the second user's name: ");
scanf("%s", user2);
int userID2 = findid(network,user2);
if(userID2 == -1)
{
printf("\nUser %s doesn't exist.\n", user2);
return -1;
}
if (network->adjacencyMatrix[userID1][userID2] && network->adjacencyMatrix[userID2][userID1])
printf("\n\n%s and %s are friends.\n", user1, user2);
else
printf("\n\n%s and %s are not friends.\n", user1, user2);
}
void removeUserFromFile(SocialNetwork *network,char *namez)
{
system("cls");
char userName[100];
strcpy(userName,namez);
int userIndex = findid(network , userName);
if(userIndex == -1)
printf("User '%s' not found in the network.\n", userName);
else
{
for (int i = 0; i < network->numUsers; i++)
{
addOrRemoveFriendship(network,userIndex,i,false);
addOrRemoveFriendship(network,i,userIndex,false);
}
// Move the last user in the array to the position of the removed user
if (userIndex < network->numUsers - 1)
{
network->users[userIndex] = network->users[network->numUsers - 1];
for (int i = 0; i < network->numUsers; i++)
{
network->adjacencyMatrix[i][userIndex] = network->adjacencyMatrix[i][network->numUsers - 1];
network->adjacencyMatrix[userIndex][i] = network->adjacencyMatrix[network->numUsers - 1][i];
}
}
network->numUsers--;
// update the data.txt file
FILE *file = fopen("data.txt", "r");
char tempFilename[] = "tempdeluser.txt";
FILE *tempFile = fopen(tempFilename, "w");
char line[256];
while (fgets(line, sizeof(line), file) != NULL)
{
int lineLength = strlen(line);
if (line[lineLength - 1] == '\n')
line[lineLength - 1] = '\0';
char *name = strtok(line, ",");
if (name != NULL && strcmp(name, userName) != 0)
{
fprintf(tempFile, "%s", name);
char *friendsList = strtok(NULL, ",");
while (friendsList != NULL)
{
if (strcmp(friendsList, userName) != 0)
fprintf(tempFile, ",%s", friendsList);
friendsList = strtok(NULL, ",");
}
fprintf(tempFile, "\n");
}
}
fclose(file);
fclose(tempFile);
replaceFile("data.txt",tempFilename);
printf("User '%s' removed from the network.\n", userName);
// update the user_pass.txt filee
addOrRemoveFriendsFromFile("user_pass.txt",userName,"",-1);
}
}
void removeFriends(SocialNetwork *network,char *name)
{
char friends[256];
char totalfreinds[256];
int userID = findid(network,name);
printf("Enter the list of friends' names to be deleted (comma-separated): ");
scanf(" %s", friends); // Read the input as a whole line
int friendID;
char *friendName = strtok(friends, ",");
while (friendName != NULL)
{
friendID = findid(network, friendName);
if (friendID != -1)
addOrRemoveFriendship(network,userID,friendID,false);
else
printf("Friend '%s' is not found in the network.\n", friendName);
friendName = strtok(NULL, ",");
}
getCurrFriendsOfUser(network,name,totalfreinds);
addOrRemoveFriendsFromFile("data.txt",name,totalfreinds,0);
}
// mainshiittt
bool BFS(SocialNetwork *network,int src, int dest,int dist[MAX_USERS])
{
int n = network->numUsers;
bool visited[MAX_USERS] = {false};
int q[n]; //Queue created
int f=0,r=-1; //Queue Initialized
visited[src] = true;
dist[src] = 0;
q[++r]=src; //Enqueue src
while(f<=r) //while queue is not empty
{
int u = q[f++]; //dequeue
for (int i = 0; i < n; i++)
{
if (network->adjacencyMatrix[u][i] && !visited[i])
{
visited[i] = true;
dist[i] = dist[u] + 1;
q[++r]=i; //Enqueue adj vertex
}
}
}
return visited[dest]; // Return true if a path to the destination exists
}
void findMutualz(SocialNetwork *network , int s)
{
if(s==-1)
{
printf("\nUser does not exits..\n");
return;
}
bool nofreindsz= true;
int dist[MAX_USERS]={0};
int n = network->numUsers;
for (int dest = 0; dest < network->numUsers; dest++)
{
if (dest!=s)
{
if (BFS(network ,s, dest, dist))
nofreindsz = false;
}
}
printf("Mutuals of %s are:\n", network->users[s].name);
if(nofreindsz==false)
{
for (int i = 0; i < n; i++)
if (dist[i] > 1)
printf("\t\t%s = %d\n", network->users[i].name, dist[i]);
}
if(nofreindsz==true)
{
int threshold=0.1*n;
printf("No mutual's found for %s\n", network->users[s].name);
for (int i = 0; i < n; i++)
{
if(network->followersOfUser[i]>threshold)
printf("\t\t%s\n", network->users[i].name);
}
}
}
void searchbar(SocialNetwork *network,char *loggedin,char *addOrRem)
{
char *namez;
char input[25];
bool found = false;
char username[100];
char choice[25];
do
{
printf("Enter the username to search (or 'q' to exit): ");
scanf("%s", input);
if (strcmp(input, "q") == 0)
break;
found = false;
for (int i = 0; i < network->numUsers; i++)
{
strcpy(username,network->users[i].name);
if (strstr(strlwr(username), strlwr(input)) != NULL)
{
found = true;
printf("\t\t%s\n", network->users[i].name);
}
}
if (!found)
printf("\n%s user does not exist. Try another name.\n", input);
if(found==true && loggedin!=NULL)
{
if(strcmp(addOrRem,"add")==0)
{
printf("\nDo u want to add friends (y/n) : ");
scanf("%s",choice);
if(strcmp(choice,"y")==0)
addFriends(network,loggedin);
}
if(strcmp(addOrRem,"remove")==0)
{
printf("\nDo u want to remove friends (y/n) : ");
scanf("%s",choice);
if(strcmp(choice,"y")==0)
removeFriends(network,loggedin);
}
}
}while (1);
}
// MAINN CONTROL FUNCTIONS
void functionsofuser(SocialNetwork *network,char *userlogedin)
{
int choice;
char ans[10];
do {
printf("\n\t\t\tWelcome %s !!!\n\n",userlogedin);
printf("\n\n\n");
printf("1. Show the graph\n");
printf("2. Search for someone on the network.\n");
printf("3. Show my Following\n");
printf("4. Show my Follower's\n");
printf("5. Find my Influence Score \n");
printf("6. Add friend's\n");
printf("7. Remove friend's\n");
printf("8. Show my Mutual's.\n");
printf("9. Check if you're buddies with someone.\n");
printf("10. Delete my Account\n");
printf("11. Back\n");
printf("\n\n\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
// Show the Graph
system("cls");
generateDotFile(network, "graph.DOT");
showthegraph();
break;
case 2:
searchbar(network,NULL,NULL);
case 3:
followingg(network,userlogedin,"show");
break;
case 4:
followers(network,userlogedin,"show");
break;
case 5:
calculateNoOfFollowrs(network);
friendscoreOfUser(network,userlogedin);
break;
case 6:
searchbar(network,userlogedin,"add");
//addFriends(network,userlogedin);
break;
case 7:
searchbar(network,userlogedin,"remove");
//removeFriends(network,userlogedin);
break;
case 8:
// find mutaulzz
findMutualz(network , findid(network,userlogedin));
break;
case 9:
// check friednshipp
searchbar(network,NULL,NULL);
areFriends(network,userlogedin);