This repository has been archived by the owner on Feb 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
producao.cpp
295 lines (224 loc) · 7.98 KB
/
producao.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
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
#include "producao.h"
#include "buffer.h"
#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;
producao* removeProducao(producao** allProducao){
producao* lastProducao = NULL;
if((*allProducao) != NULL){
if((*allProducao)->last != NULL && (*allProducao)->last != (*allProducao)){
//Make the last producao one producao before the current
(*allProducao)->last = (*allProducao)->last->prev;
//Save the old last address
lastProducao = (*allProducao)->last->next;
//Make the old last producao a unique node
(*allProducao)->last->next->prev = NULL;
}
//If the current node is the last one remaing
if((*allProducao)->last == (*allProducao)){
(*allProducao)->last = NULL;
lastProducao = (*allProducao);
(*allProducao) = NULL;
}
}
return lastProducao;
}
producao* getAllProducoesFromThatDocente(producao* producoes, long docenteId){
if(producoes != NULL){
if(docenteId > -1){
if(docenteId < producoes->docenteId){
return getAllProducoesFromThatDocente(producoes->right, docenteId);
}
if(docenteId > producoes->docenteId){
return getAllProducoesFromThatDocente(producoes->left, docenteId);
}
if(docenteId == producoes->docenteId || producoes == NULL){
return producoes;
}
}
}
}
void printProducoes(producao* producaoNode){
int num = 0;
if(producaoNode != NULL){
producao* iterator = producaoNode;
while(iterator->next != NULL){
cout << "\t" << iterator->type << " - " << iterator->title << endl;
num++;
iterator = iterator->next;
}
cout << "num = " << num << endl;
}
}
void postorderProducao(producao* producaoNode){
if (producaoNode != NULL){
// first recur on left subtree
postorderProducao(producaoNode->left);
// then recur on right subtree
postorderProducao(producaoNode->right);
// now print the current node title
cout << producaoNode->docenteId << endl;
printProducoes(producaoNode);
}
}
void destroyAllProducoes(producao** producoes){
//If the user actually pass a valid double linked list of docente
if(*producoes != NULL){
//Get the address of the first docente
producao* iterator = *producoes;
//Go to the end of the double linked list of docente
while(iterator->next != NULL){
iterator = iterator->next;
}
//Go to the start of the double linked list of docente
while(iterator->prev != NULL){
//Go backwards one docente
iterator = iterator->prev;
//Desalocate the docente that is after the current
destroyProducao(&iterator->next);
}
//Destroy the first docente (the only one remaing)
destroyProducao(&iterator);
*producoes = NULL;
}
}
void destroyProducao(producao** producao){
if(*producao != NULL){
if((*producao)->left == NULL){
if((*producao)->right == NULL){
if((*producao)->next == NULL){
delete[] *producao;
}
}
}
}
*producao = NULL;
}
producao* loadAllProducoes (const char* filePath){
//Load qualis_capes_periodicos.csv into memory
character* producaoCSV = createBufferFile(filePath);
character* currentLine = NULL;
//Remove the file header, it's useless
currentLine = removeFirstBufferLine(&producaoCSV);
destroyBufferFile(¤tLine);
int maxNodeNum = 20;
//A binary search tree for all producao data type (it use docenteId as key)
producao* producaoBST = NULL;
//Remove line by line from qualis_capes_periodicos.csv
while(currentLine = removeFirstBufferLine(&producaoCSV)){
//Get docente's ID
long docenteId = stringToLong(getNthColumnData(currentLine, 1));
//Get producao ID
long id = stringToLong(getNthColumnData(currentLine, 2));
//Get producao type
char* type = getNthColumnData(currentLine, 3);
//Get producao issn
char* issn = getNthColumnData(currentLine, 4);
//Get producao title
char* title = getNthColumnData(currentLine, 5);
//Get producao local
char* local = getNthColumnLocal(currentLine);
//Get producao year
int year = stringToInt(getNthColumnData(currentLine, 7));
//Destroy the removed line (Only the text inside of it)
destroyBufferFile(¤tLine);
//Insert it in the BST
insertProducao(&producaoBST,docenteId, id, issn, type, title, local, year);
}
return producaoBST;
}
producao* createProducao(long docenteId,long id, char* issn, char* type, char* title, char* local, int year){
producao* newProducao = NULL;
newProducao = new producao[1];
if(newProducao != NULL){
//Actual data
newProducao->docenteId = docenteId;
newProducao->id = id;
newProducao->issn = issn;
newProducao->type = type;
newProducao->title = title;
newProducao->local = local;
newProducao->year = year;
//Double linked list atributes
newProducao->next = NULL;
newProducao->prev = NULL;
newProducao->last = NULL;
//Binary Search Tree atributes
newProducao->right = NULL;
newProducao->left = NULL;
//Baker atributes
newProducao->isUsedAlready = 0;
}
return newProducao;
}
void destroyProducao(producao* producaoToDestroy){
//If the user actually pass a valid producao
if(producaoToDestroy != NULL){
delete[] producaoToDestroy;
}
}
void addProducao(producao** list, long docenteId,long id, char* issn, char* type, char* title, char* local, int year){
//If the user actually pass a empty list
if((*list) != NULL){
//Create a new producao
producao* newProducao = createProducao(docenteId, id, issn, type, title, local, year);
//Check if the new producao was allocated
if(newProducao != NULL){
//Check if the last producao was not set yet
if((*list)->last == NULL){
//Iterate through list until the its end
producao* iterator = (*list);
while(iterator->next != NULL){
iterator = iterator->next;
}
//Check if the list is not a sub-list
if((*list)->prev == NULL){
//Save the address of the last producao
(*list)->last = iterator;
}else{
//Save the address of the last producao
producao* lastproducaoAddress = iterator;
//Go back until the given producao as parameter
iterator = (*list);
//Iterate through list until the its head
while(iterator->prev != NULL){
iterator = iterator->prev;
}
//Save the address of the last producao
iterator->last = lastproducaoAddress;
}
}
//Insert the new producao at the list
newProducao->next = NULL;
//Save the adress of the old last producao
newProducao->prev = (*list)->last;
//Make the old last producao point to the new one
(*list)->last->next = newProducao;
//Update the new last producao
(*list)->last = newProducao;
}
}else{
producao* newList = createProducao(docenteId, id, issn, type, title, local, year);
newList->last = newList;
*list = newList;
}
}
void insertProducao(producao** producaoTree, long docenteId,long id, char* issn, char* type, char* title, char* local, int year){
//If the tree is empty, return a new tree
if((*producaoTree) == NULL){
(*producaoTree) = createProducao(docenteId, id, issn, type, title, local, year);
}
//If Im at the correct docente ID node at the BST
if((*producaoTree)->docenteId == docenteId){
addProducao(producaoTree ,docenteId, id, issn, type, title, local, year);
}else{
//If the given producao belongs to a docente with a ID lesser the the current
if((*producaoTree)->docenteId < docenteId){
insertProducao(&((*producaoTree)->left), docenteId, id, issn, type, title, local, year);
//If the given producao belongs to a docente with a ID greater the the current
}else if((*producaoTree)->docenteId > docenteId){
insertProducao(&((*producaoTree)->right), docenteId, id, issn, type, title, local, year);
}
}
}