-
Notifications
You must be signed in to change notification settings - Fork 0
/
friends.c
257 lines (220 loc) · 6.22 KB
/
friends.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
WeChat: cstutorcs
QQ: 749389476
Email: tutorcs@163.com
#include "friends.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Create a new user with the given name. Insert it at the tail of the list
* of users whose head is pointed to by *user_ptr_add.
*
* Return:
* - 0 on success.
* - 1 if a user by this name already exists in this list.
* - 2 if the given name cannot fit in the 'name' array
* (don't forget about the null terminator).
*/
int create_user(const char *name, User **user_ptr_add) {
if (strlen(name) >= MAX_NAME) {
return 2;
}
User *new_user = malloc(sizeof(User));
if (new_user == NULL) {
perror("malloc");
exit(1);
}
strncpy(new_user->name, name, MAX_NAME); // name has max length MAX_NAME - 1
for (int i = 0; i < MAX_NAME; i++) {
new_user->profile_pic[i] = '\0';
}
new_user->first_post = NULL;
new_user->next = NULL;
for (int i = 0; i < MAX_FRIENDS; i++) {
new_user->friends[i] = NULL;
}
// Add user to list
User *prev = NULL;
User *curr = *user_ptr_add;
while (curr != NULL && strcmp(curr->name, name) != 0) {
prev = curr;
curr = curr->next;
}
if (*user_ptr_add == NULL) {
*user_ptr_add = new_user;
return 0;
} else if (curr != NULL) {
free(new_user);
return 1;
} else {
prev->next = new_user;
return 0;
}
}
/*
* Return a pointer to the user with this name in
* the list starting with head. Return NULL if no such user exists.
*
* NOTE: You'll likely need to cast a (const User *) to a (User *)
* to satisfy the prototype without warnings.
*/
User *find_user(const char *name, const User *head) {
while (head != NULL && strcmp(name, head->name) != 0) {
head = head->next;
}
return (User *)head;
}
/*
* Print the usernames of all users in the list starting at curr.
* Names should be printed to standard output, one per line.
*/
void list_users(const User *curr) {
printf("User List\n");
while (curr != NULL) {
printf("\t%s\n",curr->name);
curr = curr->next;
}
}
/*
* Make two users friends with each other. This is symmetric - a pointer to
* each user must be stored in the 'friends' array of the other.
*
* New friends must be added in the first empty spot in the 'friends' array.
*
* Return:
* - 0 on success.
* - 1 if the two users are already friends.
* - 2 if the users are not already friends, but at least one already has
* MAX_FRIENDS friends.
* - 3 if the same user is passed in twice.
* - 4 if at least one user does not exist.
*
* Do not modify either user if the result is a failure.
* NOTE: If multiple errors apply, return the *largest* error code that applies.
*/
int make_friends(const char *name1, const char *name2, User *head) {
User *user1 = find_user(name1, head);
User *user2 = find_user(name2, head);
if (user1 == NULL || user2 == NULL) {
return 4;
} else if (user1 == user2) { // Same user
return 3;
}
int i, j;
for (i = 0; i < MAX_FRIENDS; i++) {
if (user1->friends[i] == NULL) { // Empty spot
break;
} else if (user1->friends[i] == user2) { // Already friends.
return 1;
}
}
for (j = 0; j < MAX_FRIENDS; j++) {
if (user2->friends[j] == NULL) { // Empty spot
break;
}
}
if (i == MAX_FRIENDS || j == MAX_FRIENDS) { // Too many friends.
return 2;
}
user1->friends[i] = user2;
user2->friends[j] = user1;
return 0;
}
/*
* Print a post.
* Use localtime to print the time and date.
*/
int print_post(const Post *post) {
if (post == NULL) {
return 1;
}
// Print author
printf("From: %s\n", post->author);
// Print date
printf("Date: %s\n", asctime(localtime(post->date)));
// Print message
printf("%s\n", post->contents);
return 0;
}
/*
* Print a user profile.
* For an example of the required output format, see the example output
* linked from the handout.
* Return:
* - 0 on success.
* - 1 if the user is NULL.
*/
int print_user(const User *user) {
if (user == NULL) {
return 1;
}
// Print name
printf("Name: %s\n\n", user->name);
printf("------------------------------------------\n");
// Print friend list.
printf("Friends:\n");
for (int i = 0; i < MAX_FRIENDS && user->friends[i] != NULL; i++) {
printf("%s\n", user->friends[i]->name);
}
printf("------------------------------------------\n");
// Print post list.
printf("Posts:\n");
const Post *curr = user->first_post;
while (curr != NULL) {
print_post(curr);
curr = curr->next;
if (curr != NULL) {
printf("\n===\n\n");
}
}
printf("------------------------------------------\n");
return 0;
}
/*
* Make a new post from 'author' to the 'target' user,
* containing the given contents, IF the users are friends.
*
* Insert the new post at the *front* of the user's list of posts.
*
* Use the 'time' function to store the current time.
*
* 'contents' is a pointer to heap-allocated memory - you do not need
* to allocate more memory to store the contents of the post.
*
* Return:
* - 0 on success
* - 1 if users exist but are not friends
* - 2 if either User pointer is NULL
*/
int make_post(const User *author, User *target, char *contents) {
if (target == NULL || author == NULL) {
return 2;
}
int friends = 0;
for (int i = 0; i < MAX_FRIENDS && target->friends[i] != NULL; i++) {
if (strcmp(target->friends[i]->name, author->name) == 0) {
friends = 1;
break;
}
}
if (friends == 0) {
return 1;
}
// Create post
Post *new_post = malloc(sizeof(Post));
if (new_post == NULL) {
perror("malloc");
exit(1);
}
strncpy(new_post->author, author->name, MAX_NAME);
new_post->contents = contents;
new_post->date = malloc(sizeof(time_t));
if (new_post->date == NULL) {
perror("malloc");
exit(1);
}
time(new_post->date);
new_post->next = target->first_post;
target->first_post = new_post;
return 0;
}