-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrd.cpp
More file actions
185 lines (140 loc) · 4.78 KB
/
Copy pathrd.cpp
File metadata and controls
185 lines (140 loc) · 4.78 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
#include "rd.hpp"
#include <fstream>
#include <iostream>
#include "graphw.hpp"
#include <boost/tokenizer.hpp>
using namespace std;
pair<int,int> lineConvert(string line);
Reader::Reader (const char* name):fname(name), tmax(-1), exams(0),students(-1){}
int Reader::getExamN(){
if(this->exams!=0)
return this->exams;
int exams = 0;
string fName = std::string(this->fname);
string exmFile = fName + ".exm";
string line;
ifstream exmread (exmFile);
if(exmread.is_open()){
while(getline(exmread,line))
if(line.length()>1) //dont count the last line, or empty ones
++exams;
this->exams = exams;
}
else cout << "Unable to open: " << exmFile << endl;
return exams;
}
//IF THIS FUNCTIONS RETURNS -1 there is a problem
int Reader::getTmax(){
//If the tmax has been read already, avoid rereading from file
if(this->tmax!=-1)
return this->tmax;
//If the tmax value has not been read, then read for the first time
int tmax;
string fName = std::string(this->fname);
string sloFile = fName + ".slo";
string line;
ifstream sloread (sloFile);
if(sloread.is_open()){
getline(sloread,line); //get the number tmax
tmax = stoi(line);
sloread.close();
this->tmax = tmax;
return tmax;
}
else cout << "Unable to open: " << sloFile << endl;
return -1;
}
G::Graph Reader::read(){
//
string fName = std::string(this->fname);
string stuFile = fName + ".stu";
string exmFile = fName + ".exm";
string line;
string nextline;
int studentsNumber = 0;
int tmax = getTmax();
int exams = getExamN();
int *stuExams ;
stuExams = new int[tmax]; //list of exams for the current student
G::Graph conflicts(exams); //Grafo dei conflitti
ifstream sturead (stuFile);
if (sturead.is_open())
{
int student;
int exam;
int nextStudent;
int nextExam;
int i=0;
getline(sturead,line);
pair<int,int> stuExam = lineConvert(line);
student = stuExam.first;
exam = stuExam.second;
//Reading each line, separating it into the student part
//and into the exam the student is taking
bool flag = false; //ho finito a processare il corrente studente
while ( getline (sturead,nextline) || !flag)
{
flag = false;
stuExam = lineConvert(nextline);
nextStudent = stuExam.first;
nextExam = stuExam.second;
stuExams[i++]=exam-1;
//Per l'ultima iterazione
if(sturead.peek()==EOF)
stuExams[i++]=nextExam-1;
//Whenever we pass onto a new student, start adding the edges
//pertaining to the current student
if( nextStudent!=student || sturead.peek()==EOF){
studentsNumber++;
//ADD GRAPH STUFF
for(int y=0; y<i-1; y++){
int exam1 = stuExams[y];
for(int x=y+1; x<i; x++){
int exam2 = stuExams[x];
pair<G::Edge, bool> ed = edge(exam1,exam2,conflicts);
//Add edge if its not present
if(ed.second==0)
add_edge(exam1,exam2,1,conflicts);
else{ //Increment its value otherwise
int weight = get(edge_weight_t(), conflicts, ed.first);
put(edge_weight_t(), conflicts, ed.first, weight+1);
}
}
}
flag = true; // Segna che lo studente corrente è stato processato
i=0;
}
exam = nextExam;
student = nextStudent;
}
sturead.close();
}
else cout << "Unable to open " << stuFile << endl;
delete [] stuExams;
this->students = studentsNumber;
return conflicts;
}
/*Li viene data una riga del file studente
ritorna una coppia di cui
il primo elemento è la matricola dello studente
il secondo elemento è il codice dell'esame
**/
pair<int,int> lineConvert(string line){
int student,exam;
tokenizer<> tok(line);
tokenizer<>::iterator beg=tok.begin();
string str = *beg;
student = stoi(str.erase(0,1));
str = *(++beg);
exam = stoi(str);
pair<int,int> stuExm;
stuExm.first = student;
stuExm.second = exam;
return stuExm;
}
int Reader::getStudents(){
if(this->students == -1){
cout << "Please call reader function first" << endl;
}
return this->students;
}