-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathconnectu.cpp
More file actions
475 lines (413 loc) · 13.5 KB
/
connectu.cpp
File metadata and controls
475 lines (413 loc) · 13.5 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
/*
* PROJECT: CONNECT-U (Starter Code)
* Course: ECE367L Data Structures & Algorithms
*
* SAFETY UPDATE:
* The saveData() function call in main() is currently COMMENTED OUT.
* This prevents you from accidentally wiping your 'posts.csv' file
* if your Lab 1 implementation is incomplete.
* * ONLY uncomment saveData() after you have verified Lab 1 works!
*/
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <ctime>
#include <vector>
#include <set>
#include <queue>
#include <algorithm>
using namespace std;
// ==========================================
// MODELS & DATA STRUCTURES
// ==========================================
struct Post {
int postId;
int userId;
string content;
int likes;
long timestamp;
Post* next;
Post(int pid, int uid, string txt, int lk, long time)
: postId(pid), userId(uid), content(txt), likes(lk), timestamp(time), next(nullptr) {}
// TODO: LAB 3 - Implement Scoring Logic
double getScore() {
return 0.0;
}
};
// TODO: LAB 1 - Linked List
class Timeline {
public:
Post* head;
Timeline() : head(nullptr) {}
// Task: Add a new post to the FRONT of the list (O(1))
void addPost(int pid, int uid, string content, int likes, long time) {
// TODO: LAB 1
}
void printTimeline() {
Post* current = head;
if (!current) { cout << " (No posts yet)" << endl; return; }
// Task: Traverse the linked list and print content
// TODO: LAB 1
}
};
// Forward Declaration
class User;
// TODO: LAB 4 - Binary Search Tree
struct BSTNode {
User* user;
BSTNode* left;
BSTNode* right;
BSTNode(User* u) : user(u), left(nullptr), right(nullptr) {}
};
class FriendBST {
public:
BSTNode* root;
FriendBST() : root(nullptr) {}
BSTNode* insert(BSTNode* node, User* u) ;
void printInOrder(BSTNode* node);
void addFriend(User* u) { root = insert(root, u); }
void printFriends() {
if (root == nullptr) cout << " (No friends yet)" << endl;
else printInOrder(root);
}
};
class User {
public:
int userId;
string username;
int techScore, artScore, sportScore;
Timeline timeline; // Lab 1
vector<User*> friends; // Graph
FriendBST friendTree; // Lab 4
User(int id, string name, int t, int a, int s)
: userId(id), username(name), techScore(t), artScore(a), sportScore(s) {}
void addPost(int pid, string content, int likes, long time) {
timeline.addPost(pid, userId, content, likes, time);
}
void addFriend(User* u) {
friends.push_back(u);
friendTree.addFriend(u);
}
vector<User*> getFriendsList() { return friends; }
};
// BST Implementation
BSTNode* FriendBST::insert(BSTNode* node, User* u) {
// TODO: LAB 4
return node;
}
void FriendBST::printInOrder(BSTNode* node) {
// TODO: LAB 4
}
// TODO: LAB 3 - Max Heap
class FeedHeap {
private:
Post* heap[1000];
int size;
void heapifyDown(int index) { /* TODO: LAB 3 */ }
void heapifyUp(int index) { /* TODO: LAB 3 */ }
public:
FeedHeap() : size(0) {}
void push(Post* p) { /* TODO: LAB 3 */ }
Post* popMax() { return nullptr; /* TODO: LAB 3 */ }
bool isEmpty() { return size == 0; }
};
vector<User*> allUsers;
// TODO: LAB 2 - Hash Map
struct HashNode {
string key;
User* value;
HashNode* next;
HashNode(string k, User* v) : key(k), value(v), next(nullptr) {}
};
class UserMap {
private:
static const int TABLE_SIZE = 10007;
HashNode** table;
unsigned long hashFunction(string key) {
// TODO: LAB 2
return 0;
}
public:
UserMap() {
table = new HashNode*[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++) table[i] = nullptr;
}
void put(string key, User* user) { /* TODO: LAB 2 */ }
User* get(string key) {
// --- TEMPORARY FALLBACK FOR LAB 1 ---
for(User* u : allUsers) {
if (u->username == key) return u;
}
// TODO: LAB 2 - REPLACE ABOVE WITH HASH LOOKUP
return nullptr;
}
};
UserMap userMap;
// ==========================================
// UTILITY FUNCTIONS
// ==========================================
vector<string> split(string s) {
vector<string> tokens;
string token;
bool inQuotes = false;
for (char c : s) {
if (c == '"') {
inQuotes = !inQuotes; // Toggle quote state
continue; // Skip the quote character itself
}
if (c == ',' && !inQuotes) {
// Found a delimiter outside of quotes -> New Token
tokens.push_back(token);
token.clear();
} else {
// Regular character or comma inside quotes
token += c;
}
}
tokens.push_back(token); // Add last token
return tokens;
}
int GLOBAL_POST_ID_COUNTER = 1;
Post* findPostById(int id) {
for (User* u : allUsers) {
Post* curr = u->timeline.head;
while (curr != nullptr) {
if (curr->postId == id) return curr;
curr = curr->next;
}
}
return nullptr;
}
void createNewPost(User* author, string content) {
int postId = GLOBAL_POST_ID_COUNTER++;
long timestamp = time(0);
author->addPost(postId, content, 0, timestamp);
cout << "\n[SUCCESS] Post saved to timeline." << endl;
}
void registerNewUser(string username, int tech, int art, int sport) {
int newId = allUsers.size() + 1;
User* newUser = new User(newId, username, tech, art, sport);
allUsers.push_back(newUser);
userMap.put(username, newUser);
cout << "\n[SUCCESS] Account created." << endl;
}
void addFriendship(User* requester, User* target) {
requester->addFriend(target);
target->addFriend(requester);
cout << "\n[SUCCESS] You are now friends with @" << target->username << endl;
}
// TODO: LAB 5 - Breadth First Search
void recommendFriends(User* startUser) {
cout << "\n[GRAPH ANALYSIS] Finding friends of friends..." << endl;
// TODO: LAB 5
}
// ==========================================
// FILE I/O
// ==========================================
void loadData() {
cout << "Loading data from CSV files..." << endl;
ifstream userFile("users.csv");
string line;
if (userFile.is_open()) {
getline(userFile, line);
while (getline(userFile, line)) {
vector<string> row = split(line);
if (row.size() < 5) continue;
User* newUser = new User(stoi(row[0]), row[1], stoi(row[2]), stoi(row[3]), stoi(row[4]));
allUsers.push_back(newUser);
userMap.put(row[1], newUser);
}
userFile.close();
}
ifstream relFile("relations.csv");
if (relFile.is_open()) {
getline(relFile, line);
while (getline(relFile, line)) {
vector<string> row = split(line);
if (row.size() < 2) continue;
int u1 = stoi(row[0]); int u2 = stoi(row[1]);
if (u1 <= allUsers.size() && u2 <= allUsers.size()) {
allUsers[u1-1]->addFriend(allUsers[u2-1]);
allUsers[u2-1]->addFriend(allUsers[u1-1]);
}
}
relFile.close();
}
ifstream postFile("posts.csv");
if (postFile.is_open()) {
getline(postFile, line);
while (getline(postFile, line)) {
vector<string> row = split(line);
if (row.size() < 5) continue;
int pid = stoi(row[0]); int uid = stoi(row[1]);
if (uid <= allUsers.size()) {
allUsers[uid-1]->addPost(pid, row[2], stoi(row[3]), stol(row[4]));
if (pid >= GLOBAL_POST_ID_COUNTER) GLOBAL_POST_ID_COUNTER = pid + 1;
}
}
postFile.close();
}
}
void saveData() {
// SAFETY CHECK: If no users exist, do not overwrite files!
if (allUsers.empty()) {
cout << "[SAFETY] No data in memory. Skipping save to prevent file wipe." << endl;
return;
}
cout << "Saving data..." << endl;
ofstream userFile("users.csv");
userFile << "user_id,username,tech_score,art_score,sport_score\n";
for (User* u : allUsers) {
userFile << u->userId << "," << u->username << "," << u->techScore << "," << u->artScore << "," << u->sportScore << "\n";
}
userFile.close();
ofstream relFile("relations.csv");
relFile << "user_id_1,user_id_2\n";
for (User* u : allUsers) {
for (User* f : u->friends) {
if (u->userId < f->userId) relFile << u->userId << "," << f->userId << "\n";
}
}
relFile.close();
ofstream postFile("posts.csv");
postFile << "post_id,user_id,content,likes,timestamp\n";
for (User* u : allUsers) {
vector<Post*> temp;
Post* curr = u->timeline.head;
while (curr) { temp.push_back(curr); curr = curr->next; }
for (int i = temp.size() - 1; i >= 0; i--) {
Post* p = temp[i];
string safeContent = p->content;
if (safeContent.find(',') != string::npos) {
safeContent = "\"" + safeContent + "\"";
}
postFile << p->postId << "," << p->userId << "," << safeContent << "," << p->likes << "," << p->timestamp << "\n";
}
}
postFile.close();
cout << "Done." << endl;
}
// ==========================================
// MAIN MENU (UI)
// ==========================================
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
void showUserDashboard(User* currentUser) {
int choice = 0;
while (choice != 7) {
cout << "\n--- Welcome, @" << currentUser->username << " ---" << endl;
cout << "1. View My Post (Lab 1)" << endl;
cout << "2. Create New Post (Lab 1)" << endl;
cout << "3. Add Friend (Lab 2)" << endl;
cout << "4. Algorithmic Feed (Lab 3)" << endl;
cout << "5. View Friends Sorted (Lab 4)" << endl;
cout << "6. Get Friend Recommendations (Lab 5)" << endl;
cout << "7. Logout" << endl;
cout << "Select >> ";
cin >> choice;
if (choice == 1) {
cout << "\n[MY POST]" << endl;
currentUser->timeline.printTimeline();
}
else if (choice == 2) {
cout << "\nEnter post content: ";
cin.ignore();
string content;
getline(cin, content);
createNewPost(currentUser, content);
}
else if (choice == 3) {
string friendName;
cout << "Enter username to add: "; cin >> friendName;
User* target = userMap.get(friendName);
if(target && target != currentUser) addFriendship(currentUser, target);
else cout << "Invalid user (or Hash Map not implemented)." << endl;
}
else if (choice == 4) {
cout << "\n[ALGORITHMIC FEED]" << endl;
FeedHeap feed;
vector<User*> friends = currentUser->getFriendsList();
for(User* f : friends) {
Post* p = f->timeline.head;
int limit = 0;
while(p != nullptr && limit < 5) {
feed.push(p);
p = p->next;
limit++;
}
}
int count = 0;
while(!feed.isEmpty() && count < 10) {
Post* top = feed.popMax();
if(top)
cout << " > [ID: " << top->postId << "] [Score: " << (int)top->getScore() << "] @"
<< allUsers[top->userId - 1]->username << ": " << top->content
<< " (" << top->likes << " likes)" << endl;
count++;
}
if(count == 0) cout << " No posts found." << endl;
else {
cout << "\nDo you want to like a post? (y/n): ";
char resp; cin >> resp;
if (resp == 'y' || resp == 'Y') {
int pid; cout << "Enter Post ID: "; cin >> pid;
Post* p = findPostById(pid);
if (p) { p->likes++; cout << "Liked!" << endl; }
}
}
}
else if (choice == 5) {
cout << "\n[MY FRIENDS]" << endl;
currentUser->friendTree.printFriends();
}
else if (choice == 6) {
recommendFriends(currentUser);
}
else if (choice == 7) {
cout << "Logging out..." << endl;
}
}
}
void showMainMenu() {
int choice = 0;
while (choice != 3) {
cout << "\n=== CONNECT-U ===" << endl;
cout << "1. Login" << endl;
cout << "2. Register" << endl;
cout << "3. Exit & Save" << endl;
cout << "Select >> ";
cin >> choice;
if (choice == 1) {
string username;
cout << "Username: "; cin >> username;
User* user = userMap.get(username);
if (user) showUserDashboard(user);
else cout << "User not found." << endl;
}
else if (choice == 2) {
string username;
int t, a, s;
cout << "Username: "; cin >> username;
cout << "Tech/Art/Sport (1-10): "; cin >> t >> a >> s;
registerNewUser(username, t, a, s);
}
else if (choice == 3) {
// SAFETY: Commented out to prevent data loss on initial run.
// Students must uncomment this ONLY when Lab 1 is complete.
// saveData();
cout << "Goodbye! " << endl;
}
}
}
int main() {
loadData();
clearScreen();
showMainMenu();
return 0;
}