-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAddfriends.c
91 lines (85 loc) · 3.31 KB
/
Addfriends.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
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "vector.h"
#include "user.h"
#include "priorityQueue.h"
extern long long int MASTER;
void AddF(user_list *list, long long a, long long b) //Adds b to friend list of a and a to the anti-friend list of b
{
add(b, &(list->array_of_users[a]->following));
add(a, &(list->array_of_users[b]->followers));
}
void AddMultipleFriends(user_list *list, long long a, long long count) //Called after recommending friends to new and old users
{
// adding multiple users as friends to a user
printf("would you like to add any of these users as your friends? (y/n): ");
char inp = ' ';
char temp[100];
Line:
scanf("%s" , temp);
if(strlen(temp)==1)
inp=temp[0];
inp = tolower(inp);
if(inp != 'y' && inp != 'n'){
printf("\nError! choose one of (y/n): ");
goto Line; //Takes user input again as user input was invalid
}
if(inp == 'y'){
long long x; // x is the number of friends to be added
printf("\nSelect the number of friends you want to add: ");
scanf("%lld", &x);
if(x > count){
x = count;
printf("We have only recommended %lld users\n" , count); //User cannot choose to add more friends than those recommended to him
}
printf("\nAdding %lld users...\n", x);
printf("\nplease provide the user IDs of friends you would like to add to user %lld ", a);
long long b;
for (int i = 0; i < x; i++) // taking user inputs and calling addfriends function
{
printf("\nEnter user ID: ");
scanf("%lld", &b);
if(b<=0 || b > MASTER || list->array_of_users[b] == NULL ) // condition to check whether the user ID is valid
{
printf("\n%lld is not a registered user !",b);
printf("\nPlease enter a valid user ID!\n");
i--;
continue;
}else if(b == a){ // User cannot add themselves as a friend
printf("\nyou can't add yourself as a friend!\n");
i--;
continue;
}
printf("\nAdding %lld as a friend of %lld...", b, a);
AddF(list, a, b); //Calls the AddF function to add b as friend of a and a in the followers list
}
printf("\nAdded all the friends!\n");
}
}
void AddSingleFriend(user_list *list)
{
//For adding single friend
long long a, b;
printf("Enter user ids of two users: \n");
scanf("%lld%lld", &a, &b);
if(a == b){ // User cannot add themselves as a friend
printf("\nyou can't add yourself as a friend!\n please change your input\n");
AddSingleFriend(list);
return;
}
if(a<=0 ||a>MASTER || list->array_of_users[a] == NULL) // condition to check whether the user ID is valid
{
printf("\n%lld is not a registered user!",a);
printf("\nTaking you back to the main menu...");
return;
}
else if(b<=0 ||b>MASTER || list->array_of_users[b] == NULL)// condition to check whether the user ID is valid
{
printf("\n%lld is not a registered user!",b);
printf("\nTaking you back to the main menu...");
return;
}
printf("\nAdding %lld as a friend of %lld...", b, a);
AddF(list, a, b); // calls addF funtion
}