forked from harshithatlb/programming_practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intuit.cpp
162 lines (143 loc) · 3.9 KB
/
intuit.cpp
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cctype>
#include <fstream>
#include <map>
/* Notes and todos
* The Employee class contains a list of friends
*/
using namespace std;
class emp{
public:
string id; // id
string name; // name
string dept; // dept
vector<emp> adjList; // contains list of friends
emp(string i, string n, string d){
id = i;
name = n;
dept = d;
}
emp(){
}
void addFriend(emp b){
bool present = false;
vector<emp>::iterator it = adjList.begin();
while(it!= adjList.end()){
if ((*it).id == b.id)
present = true;
it++;
}
if (!present)
adjList.push_back(b);
}
string print_friends(){
string ret;
ret.append(id);
if ( adjList.size() == 0)
ret.append(": None");
else ret.append(": ");
for (vector<emp>::iterator it = adjList.begin(); it!=adjList.end(); it++ )
{
emp x = *it;
if (it == adjList.begin())
ret.append( x.id);
else{
ret.append(", ");
ret.append(x.id);
}
}
return ret;
}
void print_emp(){
cout << id << name << dept <<endl;
}
};
// function taking employees and relation and returns a vector of strings
vector<string> find_adj( map<string, emp> maprep, vector<pair<string,string> > relation){
vector<pair<string, string> >::iterator vec_it = relation.begin();
vector<string> ret;
string id1, id2;
map<string, emp>::iterator itu, itv;
emp v, u;
pair<string,string> p;
for (; vec_it != relation.end(); vec_it++){
p = *vec_it;
id1 = p.first;
id2 = p.second;
itu = maprep.find(id1);
if(itu!= maprep.end())
u = itu->second;
itv = maprep.find(id2);
if (itv!= maprep.end())
v = itv->second;
if (itv!= maprep.end() && itu!=maprep.end() ){
u.addFriend(v);
v.addFriend(u);
maprep.erase(itu);
maprep.erase(itv);
maprep.insert(make_pair(u.id,u));
maprep.insert(make_pair(v.id, v));
}
}
map<string, emp>::iterator it;
for ( it = maprep.begin(); it!=maprep.end(); it++){
emp u = it->second;
ret.push_back(u.print_friends());
}
return ret;
}
int main(){
string num, name, dept;
string str, stri;
string id1, id2;
ifstream empfile("employee.csv");
ifstream fr("friendships.csv");
vector<pair<string, string> >relation;
int count = 0;
int kk;
//reading the header
getline(empfile, str);
getline(fr,stri);
//reading the file
// mapping of id's with employees
map<string, emp > maprep;
while(empfile.good()){
getline(empfile, num, ',');
getline(empfile, name, ',');
getline(empfile, dept, '\n');
if (empfile.eof())
break;
emp e (num, name, dept);
maprep.insert(make_pair(num, e));
count++;
}
map<string, emp>::iterator itu, itv;
emp v, u;
while(fr.good() && !fr.eof()){
fr >> str;
int strlength = str.length();
kk = 0;
while(kk < str.length()){
if (str.at(kk) == ',')
break;
kk++;
}
id1 = str.substr(0, kk);
id2 = str.substr(kk+1, str.length()-kk);
relation.push_back(make_pair(id1,id2));
if (fr.eof())
break;
}
// printitng all the relationships
// callling the function
vector<string> string_vec = find_adj(maprep, relation);
vector<string>::iterator it;
for ( it = string_vec.begin(); it!=string_vec.end(); it++){
cout<<*it;
cout <<endl;
}
return 0;
}