-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransactionManagerAVL.cpp
475 lines (443 loc) · 14.8 KB
/
TransactionManagerAVL.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
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: TransactionManagerAVL.cpp
* Author: li471
*
* Created on 21 de abril de 2021, 16:57
*/
#include "TransactionManagerAVL.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <queue>
#include <stack>
#include<math.h>
using namespace std;
/**
* Contructor con parámetros 1
*
* @param buyingFee tasas de compra
* @param sellingFee tasas de venta
*/
TransactionManagerAVL::TransactionManagerAVL(float buyingFee, float sellingFee) : AVLTree<string, Transaction>() {
this->buyingFee = buyingFee;
this->sellingFee = sellingFee;
}
/**
* Constructor con parámetros 2
* @param file_path ruta fichero
* @param buyingFee tasas de compra
* @param sellingFee tasas de venta
*/
TransactionManagerAVL::TransactionManagerAVL(string file_path, float buyingFee, float sellingFee) : AVLTree<string, Transaction>() {
this->buyingFee = buyingFee;
this->sellingFee = sellingFee;
loadFromFile(file_path);
}
/**
* Destructor
*/
TransactionManagerAVL::~TransactionManagerAVL() {
}
/**
* Va mostrando todos los elementos del árbol cronológicamente de lo mas viejo a lo mas nuevo según la petición del usuario
*
* Coste O(n) donde n en el peor de los casos es el tamaño del árbol.
*/
void TransactionManagerAVL::showAll() const {
if (isEmpty()) {
cout << "Encara no hi ha transaccions" << endl;
} else {
stack<BinaryTreeNode<string, Transaction>*> temp;
BinaryTreeNode<string, Transaction>* temp_node = p_root;
int comptador = 0, fi = number_asker();
while ((!temp.empty() || temp_node != nullptr) && comptador < fi) {
if (temp_node != nullptr) {
temp.push(temp_node);
temp_node = temp_node->getLeft();
} else {
temp_node = temp.top();
temp.pop();
vector<Transaction> vect = temp_node->getValues();
cout << "| ";
for (int i = 0; i < vect.size(); i++) {
vect[i].print();
cout << " | ";
}
cout << endl;
temp_node = temp_node->getRight();
comptador++;
if (comptador == fi) {
int temp = number_asker();
if (temp > 0)
fi += temp;
}
}
}
}
}
/*
void TransactionManagerAVL::showAll_recursive() const {
int comptador = 0, fi = 10000000;
showAll_recursive(comptador, fi, p_root);
}
void TransactionManagerAVL::showAll_recursive(int& comptador, int& fi, BinaryTreeNode<string, Transaction>* n) const {
if (comptador < fi) {
if (n->hasLeft())
showAll_recursive(comptador, fi, n->getLeft());
vector<Transaction> vect = n->getValues();
for (int i = 0; i < vect.size(); i++) {
//vect[i].print();
//cout << endl;
}
comptador++;
if (comptador == fi) {
int temp = number_asker();
if (temp > 0)
fi += temp;
}
if (n->hasRight())
showAll_recursive(comptador, fi, n->getRight());
}
}*/
/**
* Comprueba si un archivo existe
* @param name nombre de archivo
* @return si existe
*/
int TransactionManagerAVL::fileExists(string const& name) {
ifstream f(name.c_str());
if (!f.good())
throw invalid_argument("Fitxer no trobat");
return 0;
}
/**
* Splitter
* @param _string string para splittear
* @param delimiter delimitador
* @param output vector splitteado
*/
void TransactionManagerAVL::splitter(string const & _string, const char delimiter, vector<string> &output) {
stringstream string_s(_string);
string save;
while (getline(string_s, save, delimiter)) {
output.push_back(save);
}
}
/**
* Carga la información de un fichero en el árbol
*
* Coste O(nh) donde n es el número de elementos a leer y h es la altura del árbol.
*
* @param file_path fichero de donde se quiere cargar la información
*/
void TransactionManagerAVL::loadFromFile(string file_path) {
fstream fichero;
string frase;
//Programa principal
fileExists(file_path);
fichero.open(file_path);
cout << "Reading file " << file_path << "...\n";
while (!fichero.eof()) {
vector<string> informacio;
//Llegida de la linea
getline(fichero, frase);
//Es fa split de l'input
if (frase != "") {
splitter(frase, ',', informacio);
try {
Transaction newTransaction(informacio[0], stoi(informacio[1]), stof(informacio[2]));
AVLTree<string, Transaction>::add(informacio[0], newTransaction);
} catch (exception const& e) {
cout << "Error al afegir una transacció.\n";
}
}
}
cout << endl << endl;
fichero.close();
}
/**
* Muestr apor pantalla la transacción más vieja.
*
* Coste O(h) donde h es la altura del árbol
*/
void TransactionManagerAVL::showOldest() const {
if (isEmpty()) {
cout << "Encara no hi ha transaccions" << endl;
} else {
BinaryTreeNode<string, Transaction> *temp = this->p_root;
while (temp->hasLeft())
temp = temp->getLeft();
vector<Transaction> vect = temp->getValues();
cout << "| ";
for (int i = 0; i < vect.size(); i++) {
vect[i].print();
cout << " | ";
}
cout << endl;
}
}
/**
* Muestra por pantalla la transacción más nueva.
*
* Coste O(h) donde h es la altura del árbol
*/
void TransactionManagerAVL::showNewest() const {
if (isEmpty()) {
cout << "Encara no hi ha transaccions" << endl;
} else {
BinaryTreeNode<string, Transaction> *temp = this->p_root;
while (temp->hasRight())
temp = temp->getRight();
vector<Transaction> vect = temp->getValues();
cout << "| ";
for (int i = 0; i < vect.size(); i++) {
vect[i].print();
cout << " | ";
}
cout << endl;
}
}
/**
* Pregunta un número al usuario
*
* @return número que ha introducido el usuario
*/
int TransactionManagerAVL::number_asker() const {
cout << "Quants transaccions vol imprimir?" << endl;
int temp;
cin >> temp;
return temp;
}
/**
* Va mostrando todos los elementos del árbol cronológicamente de lo mas nuevo a lo mas viejo según la petición del usuario
*
* Coste O(n) donde n en el peor de los casos es el tamaño del árbol
*/
void TransactionManagerAVL::showAllReverse() const {
if (isEmpty()) {
cout << "Encara no hi ha transaccions" << endl;
} else {
stack<BinaryTreeNode<string, Transaction>*> temp;
BinaryTreeNode<string, Transaction>* temp_node = p_root;
int comptador = 0, fi = number_asker();
while ((!temp.empty() || temp_node != nullptr) && comptador < fi) {
if (temp_node != nullptr) {
temp.push(temp_node);
temp_node = temp_node->getRight();
} else {
temp_node = temp.top();
temp.pop();
vector<Transaction> vect = temp_node->getValues();
cout << "| ";
for (int i = 0; i < vect.size(); i++) {
vect[i].print();
cout << " | ";
}
cout << endl;
temp_node = temp_node->getLeft();
comptador++;
if (comptador == fi) {
int temp = number_asker();
if (temp > 0)
fi += temp;
}
}
}
}
}
/**
* Devuelve las tasas totales de todas las trasacciones
*
* Coste O(n) donde n es el tamaño del árbol
*
* @return la suma de todas las tasas de las transacciones
*/
float TransactionManagerAVL::feesInTotal() const {
if (isEmpty())
return 0;
else {
stack<BinaryTreeNode<string, Transaction>*> temp;
BinaryTreeNode<string, Transaction>* temp_node = p_root;
float tases;
while ((!temp.empty() || temp_node != nullptr)) {
if (temp_node != nullptr) {
temp.push(temp_node);
temp_node = temp_node->getLeft();
} else {
temp_node = temp.top();
temp.pop();
vector<Transaction> vect = temp_node->getValues();
for (int i = 0; i < vect.size(); i++) {
if (vect[i].getQuantitat() < 0)
tases += buyingFee * vect[i].getQuantitat()*-1;
else
tases += sellingFee * vect[i].getQuantitat();
}
temp_node = temp_node->getRight();
}
}
return tases;
}
}
/*
* LEER INTERVALO FORMA ITERATIVA POCO EFICIENTE
*
* float TransactionManagerAVL::feesInTimeInterval(pair<string, string> interval) const {
if (isEmpty())
return 0;
else {
string p_1, p_2;
if (interval.first > interval.second) {
p_1 = interval.first;
p_2 = interval.second;
} else {
p_1 = interval.second;
p_2 = interval.first;
}
stack<BinaryTreeNode<string, Transaction>*> temp;
BinaryTreeNode<string, Transaction>* temp_node = p_root;
float tases = 0;
bool fi = false;
while ((!temp.empty() || temp_node != nullptr) && !fi) {
if (temp_node != nullptr) {
temp.push(temp_node);
temp_node = temp_node->getLeft();
} else {
temp_node = temp.top();
temp.pop();
if (temp_node->getKey() >= p_2 and temp_node->getKey() <= p_1) {
vector<Transaction> vect = temp_node->getValues();
for (int i = 0; i < vect.size(); i++) {
if (vect[i].getQuantitat() < 0)
tases += buyingFee * vect[i].getQuantitat()*-1;
else
tases += sellingFee * vect[i].getQuantitat();
}
} else if (temp_node->getKey() > p_1) {
fi = true;
}
temp_node = temp_node->getRight();
}
}
return tases;
}
}*/
/**
* Devuelve las tasas en un intervalo de tiempo
*
* Coste O(h+n) donde n es el número de transacciones dentro del intervalo y h la altura del árbol
*
* @param interval el intervalo de tiempo donde se quiere calcular las tasas
* @return las tasas en ese determinado intervalo
*/
float TransactionManagerAVL::feesInTimeInterval(pair<string, string> interval) const {
float fee = 0;
string p_1;
if (interval.first < interval.second) {
p_1 = interval.first;
interval.first = interval.second;
interval.second = p_1;
}
fee_calculator(interval, p_root, fee);
return fee;
}
/**
* Función auxiliar recursiva para calcular las tasas en un intervalo
*
* Coste O(h+n) donde n es el número de transacciones dentro del intervalo y h la altura del árbol
*
* @param interval intervalo en el que se quiere calcular las tasas
* @param n parámetro recursivo
* @param fee tasas totales en ese intervalo
*/
void TransactionManagerAVL::fee_calculator(pair<string, string> interval, BinaryTreeNode<string, Transaction>* n, float& fee) const {
if (n != nullptr) {
if (interval.second < n->getKey())
fee_calculator(interval, n->getLeft(), fee);
if (n->getKey() >= interval.second and n->getKey() <= interval.first) {
vector<Transaction> vect = n->getValues();
for (int i = 0; i < vect.size(); i++) {
if (vect[i].getQuantitat() < 0)
fee += buyingFee * vect[i].getQuantitat()*-1;
else
fee += sellingFee * vect[i].getQuantitat();
}
}
if (interval.first > n->getKey())
fee_calculator(interval, n->getRight(), fee);
}
}
float TransactionManagerAVL::feesSinceTime(string date) const {
if (isEmpty())
return 0;
else {
stack<BinaryTreeNode<string, Transaction>*> temp;
BinaryTreeNode<string, Transaction>* temp_node = p_root;
float tases;
bool fi = false;
while ((!temp.empty() || temp_node != nullptr) && !fi) {
if (temp_node != nullptr) {
temp.push(temp_node);
temp_node = temp_node->getRight();
} else {
temp_node = temp.top();
temp.pop();
if (temp_node->getKey() >= date) {
vector<Transaction> vect = temp_node->getValues();
for (int i = 0; i < vect.size(); i++) {
if (vect[i].getQuantitat() < 0)
tases += buyingFee * vect[i].getQuantitat()*-1;
else
tases += sellingFee * vect[i].getQuantitat();
}
} else {
fi = true;
}
temp_node = temp_node->getLeft();
}
}
return tases;
}
}
/*
float TransactionManagerAVL::feesSinceTime(string date) const {
return feesSinceTime("0");
}*/
/**
* Calcula los queries
*
* O(nh) en el peores de los casos n líneas del fichero y h altura del árbol
*
* @param file_path fichero
* @return tasas
*/
float TransactionManagerAVL::queries_calculator(string file_path) {
float tases = 0;
fstream fichero;
string input;
//Programa principal
fileExists(file_path);
fichero.open(file_path);
cout << "Reading file " << file_path << "...\n\n";
while (!fichero.eof()) {
//Llegida de la linea
getline(fichero, input);
try {
vector<Transaction> vect = valuesOf(input);
for (int i = 0; i < vect.size(); i++) {
if (vect[i].getQuantitat() < 0)
tases += buyingFee * vect[i].getQuantitat()*-1;
else
tases += sellingFee * vect[i].getQuantitat();
}
} catch (exception& e) {
}
}
fichero.close();
return tases;
}