forked from bgreenb11/ClassmateConnect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect.cpp
More file actions
206 lines (155 loc) · 4.12 KB
/
Copy pathconnect.cpp
File metadata and controls
206 lines (155 loc) · 4.12 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
/**************************************************************
* Classmate Connect - Prototype for final project in CS302
* Written By Daniel Troutman and Benjamin Greenberg
* Description: Finds students who share classes together
**************************************************************/
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <fstream>
#include <sstream>
using namespace std;
class Course;
class User{
string netid;
string name;
string email;
string phone_number;
map <int, Course *> courses;
public:
User(string name, string netid, string email, string phone_number);
void addCourse(int, Course *);
void print();
string getNetID();
};
class Course{
int crn;
string name;
string number;
string section;
vector<User*> students;
public:
Course(int crn, string name, string number, string section);
void addStudents(map<User*, int>&);
void addUser(User *);
};
bool cmp(pair<User*,int> &u1, pair<User*, int> &u2){
return u1.second < u2.second;
}
int main(int argc, char *argv[]){
fstream courseFile, userFile;
istringstream sin;
int crn;
string name, number, section, line, email, netid;
map<int, Course *> courses;
map<int, Course *>::iterator mit;
map<User *, int> classmates_map;
vector<User*> users;
User* tmpUser;
Course* tmpCourse;
if (argc != 3){
cout<<"Usage: ./connect course_file user_file\n";
return 1;
}
courseFile.open(argv[1]);
userFile.open(argv[2]);
//read in courses
while(courseFile >> crn){
courseFile >> name >> number >> section;
courses[crn] = new Course(crn, name, number, section);
}
//read in users and add to courses
while(getline(userFile, line)){
sin.str(line);
sin >> netid >> name>> email >> number;
tmpUser = new User(netid, name, email, number);
users.push_back(tmpUser);
while(sin >> crn){
courses[crn]->addUser(tmpUser);
tmpUser->addCourse(crn, courses[crn]);
}
sin.clear();
}
userFile.close();
userFile.open(argv[2], ios::out | ios::app);
/*
//get new user info
cout << "Name?\n";
cin >> name;
cout << "NetID?\n";
cin >> netid;
cout << "Email?\n";
cin >> email;
cout << "Phone Number?\n";
cin >> number;
*/
name = "Name_test_Name";
netid = "NetID_test_NetID";
email = "Email_test_Email";
number = "Number_test_Number";
//write new user to database
// userFile << netid << " " << name << " " << email << " " << number;
//searches through classes and adds classmates
cout << "Enter courses by CRN\n";
// while(cin >> crn){
crn = 42775;
if (courses.find(crn) != courses.end()){
tmpCourse = courses[crn];
tmpCourse->addStudents(classmates_map);
}
// userFile << " " << crn;
// }
// userFile << endl;
//turn classmates into heap
vector<pair<User *, int> > classmates_vec(classmates_map.begin(), classmates_map.end());
make_heap(classmates_vec.begin(), classmates_vec.end(), cmp);
//output classmate
//Option 1
/*
cout<<"Your match is ";
classmates_vec.at(0).first->print();
cout<<"You have "<<classmates_vec.at(0).second<<" classes in common"<<endl;
pop_heap(classmates_vec.begin(), classmates_vec.end());
*/
//Option 2
cout<<classmates_vec.at(0).first->getNetID()<<" "<<classmates_vec.at(0).second<<",\n";
for(mit=courses.begin();mit!=courses.end();++mit){
delete mit->second;
}
for(int i=0;i<users.size();i++){
delete users.at(i);
}
courseFile.close();
userFile.close();
return 0;
}
User::User(string netid, string name, string email, string phone_number){
this->netid = netid;
this->name = name;
this->email = email;
this->phone_number = phone_number;
}
void User::addCourse(int crn, Course* course){
this->courses[crn] = course;
}
void User::print(){
printf("%s\nPhone Number:%s\nEmail:%s\n", name.c_str(), phone_number.c_str(), email.c_str());
}
string User::getNetID(){
return netid;
}
Course::Course(int crn, string name, string number, string section){
this->crn = crn;
this->name = name;
this->number = number;
this->section = section;
}
void Course::addStudents(map<User*, int>& classmates_map){
for(int i=0;i<students.size();i++){
classmates_map[students.at(i)]++;
}
}
void Course::addUser(User* user){
students.push_back(user);
}