-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessData.h
528 lines (503 loc) · 9.85 KB
/
processData.h
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#include "main.h"
bool shorter = false;
bool success = false;
// Balance
enum BalanceValue
{
LH = -1,
EH = 0,
RH = 1
};
// Data exchange Rate
class dataEx{
public:
int time;
float BP;
float AP;
dataEx(){};
~dataEx(){};
dataEx(int _time, float _BP, float _AP){
this->time = _time;
this->BP = _BP;
this->AP = _AP;
}
};
// Exchange Rate
class exRate{
public:
// Data
dataEx data;
// Pointer
exRate *left;
exRate *right;
// Balance
BalanceValue balance;
// Constructor default
exRate(){};
// Constructor
exRate(dataEx _dataEx){
this->data = _dataEx;
left = nullptr;
right = nullptr;
this->balance = EH;
}
// Destructor
~exRate(){};
};
// Data of Currency Fair
class dataFair{
public:
string BC;
string QC;
dataFair(){};
~dataFair(){};
dataFair(string _BC,string _QC){
this->BC = _BC;
this->QC = _QC;
}
};
//Currency Fair
class currFair{
public:
// Attribute
dataFair data;
currFair *next;
exRate *root;
// Constructor default
currFair(){};
// Constructor
currFair(dataFair _data){
this->data = _data;
root = nullptr;
next = nullptr;
}
~currFair(){};
// Method assitance
exRate *rotateRight(exRate *&node);
exRate *rotateLeft(exRate *&node);
exRate *leftBalance(exRate *&node, bool &taller);
exRate *rightBalance(exRate *&node, bool &taller);
exRate *removeLeftBalance(exRate *&node, bool &shorter);
exRate *removeRightBalance(exRate *&node, bool &shorter);
// Method primary
exRate *removeRec(exRate*&node, int &time, bool &shorter, bool &success);
exRate *insertRec(exRate*&node, dataEx &_dataER, bool &taller);
void remove(int*&,int&,exRate *&node,int _timeA,int _timeB);
void clearRec(exRate *&node);
void upLoad(exRate *&node,int _time,float _BP,float _AP,int &result);
// Con trỏ save lưu giữ node bằng time hay node gần nhất của nó luôn
void findNode(exRate *node,int _time,exRate *&save);
};
// Linked list
class List{
public:
currFair *head;
int deposit;
float leverage;
int size=0;
int *arrID = new int[1000];
// Constructor
List(){
this->head = nullptr;
deposit =0;
leverage = 1;
}
// Destructor
~List(){};
};
class nodeID{
public:
nodeID *next;
int ID;
float cost;
float scale;
int BorS;
string BC;
string QC;
nodeID(){};
nodeID(int _ID,float _cost,float _scale,int _BorS,string _BC,string _QC){
this->ID = _ID;
this->cost = _cost;
this->scale = _scale;
this->BorS = _BorS;
this->BC = _BC;
this->QC = _QC;
next = nullptr;
}
~nodeID(){};
};
class listID {
public:
nodeID *phead;
int count;
listID(){
phead = nullptr;
count =0;
}
~listID(){};
};
class ProcessData : public currFair
{
private:
List *pList;
listID *pID;
static const int MAXSIZECODE = 8;
static int split(string line, string *&sp);
public:
ProcessData();
~ProcessData();
int process(string line);
int SD(const string *sp, const int n);
int CD(const string *sp, const int n);
int SL(const string *sp, const int n);
int INS(const string *sp, const int n);
int DEL(const string *sp, const int n);
int UPD(const string *sp, const int n);
int OB(const string *sp, const int n);
int OS(const string *sp, const int n);
int CB(const string *sp, const int n);
int CS(const string *sp, const int n);
};
exRate *currFair::rotateRight(exRate *&node){
exRate *temp = node->left;
node->left = temp->right;
temp->right = node;
node = temp;
return node;
}
exRate *currFair::rotateLeft(exRate *&node)
{
exRate *temp = node->right;
node->right = temp->left;
temp->left = node;
node = temp;
return node;
}
exRate *currFair::leftBalance(exRate *&node, bool &taller){
exRate *leftTree = node->left;
if (leftTree->balance == LH)
{
node->balance = EH;
leftTree->balance = EH;
node = rotateRight(node);
taller = false;
}
else
{
exRate *rightTree = leftTree->right;
if (rightTree->balance == LH)
{
node->balance = RH;
leftTree->balance = EH;
}
else if (rightTree->balance == EH)
{
node->balance = EH;
leftTree->balance = EH;
}
else
{
node->balance = EH;
leftTree->balance = LH;
}
rightTree->balance = EH;
node->left = rotateLeft(leftTree);
node = rotateRight(node);
taller = false;
}
return node;
}
exRate *currFair::rightBalance(exRate *&node, bool &taller){
exRate *rightTree = node->right;
if (rightTree->balance == RH)
{
node->balance = EH;
rightTree->balance = EH;
node = rotateLeft(node);
taller = false;
}
else
{
exRate *leftTree = rightTree->left;
if (leftTree->balance == RH)
{
node->balance = LH;
rightTree->balance = EH;
}
else if (leftTree->balance == EH)
{
node->balance = EH;
rightTree->balance = EH;
}
else
{
node->balance = EH;
rightTree->balance = RH;
}
leftTree->balance = EH;
node->right = rotateRight(rightTree);
node = rotateLeft(node);
taller = false;
}
return node;
}
exRate *currFair::removeLeftBalance(exRate *&node, bool &shorter)
{
if (node->balance == RH)
{
node->balance = EH;
}
else if (node->balance == EH)
{
node->balance = LH;
shorter = false;
}
else
{
exRate *leftTree = node->left;
if (leftTree->balance == RH)
{
exRate *rightTree = leftTree->right;
if (rightTree->balance == RH)
{
leftTree->balance = LH;
node->balance = EH;
}
else if (rightTree->balance == EH)
{
node->balance = EH;
leftTree->balance = EH;
}
else
{
node->balance = RH;
leftTree->balance = EH;
}
rightTree->balance = EH;
node->left = rotateLeft(leftTree);
node = this->rotateRight(node);
shorter =false;
}
else
{
if (leftTree->balance != EH)
{
node->balance = EH;
leftTree->balance = EH;
}
else
{
node->balance = LH;
leftTree->balance = RH;
shorter = false;
}
node = this->rotateRight(node);
}
}
return node;
}
exRate *currFair::removeRightBalance(exRate *&node, bool &shorter)
{
if (node->balance == LH)
{
node->balance = EH;
}
else if (node->balance == EH)
{
node->balance = RH;
shorter = false;
}
else
{
exRate *rightTree = node->right;
if (rightTree->balance == LH)
{
exRate *leftTree = rightTree->left;
if (leftTree->balance == LH)
{
rightTree->balance = RH;
node->balance = EH;
}
else if (leftTree->balance == EH)
{
node->balance = EH;
rightTree->balance = EH;
}
else
{
node->balance = LH;
rightTree->balance = EH;
}
leftTree->balance = EH;
node->right = this->rotateRight(rightTree);
node = this->rotateLeft(node);
shorter =false;
}
else
{
if (rightTree->balance != EH)
{
node->balance = EH;
rightTree->balance = EH;
}
else
{
node->balance = RH;
rightTree->balance = LH;
shorter = false;
}
node = this->rotateLeft(node);
}
}
return node;
}
exRate *currFair::removeRec(exRate *&node, int &time, bool &shorter, bool &success)
{
if (node == NULL)
{
shorter = false;
success = false;
return NULL;
}
if (time < node->data.time) {
node->left = this->removeRec(node->left, time, shorter, success);
if (shorter)
node = removeRightBalance(node, shorter);
}
else if (time > node->data.time) {
node->right = this->removeRec(node->right, time, shorter, success);
if (shorter)
node = removeLeftBalance(node, shorter);
}
else {
exRate* removeNode = node;
if (node->right == NULL) {
exRate* newNode = node->left;
success = true;
shorter = true;
delete removeNode;
return newNode;
}
else if (node->left == NULL) {
exRate* newNode = node->right;
success = true;
shorter = true;
delete removeNode;
return newNode;
}
else {
exRate* exchPtr = node->left;
while (exchPtr->right != NULL) {
exchPtr = exchPtr->right;
}
node->data = exchPtr->data;
node->left = this->removeRec(node->left, exchPtr->data.time, shorter, success);
if (shorter)
node = this->removeRightBalance(node, shorter);
}
}
return node;
}
// Delete
void currFair::remove(int*& sp,int& count,exRate *&node,int timeA,int timeB){
if (node == NULL) return;
else {
remove(sp,count,node->left,timeA,timeB);
if (node->data.time >= timeA && node->data.time <= timeB){
sp[count]=node->data.time;
++count;
}
remove(sp,count,node->right,timeA,timeB);
}
}
// Insert
exRate *currFair::insertRec(exRate*&node, dataEx &_dataER, bool &taller)
{
if (node == NULL)
{
node = new exRate(_dataER);
taller = true;
return node;
}
if (_dataER.time < node->data.time)
{
node->left = insertRec(node->left, _dataER, taller);
if (taller)
{
if (node->balance == LH)
{
node = leftBalance(node, taller);
taller = false;
}
else if (node->balance == EH)
{
node->balance = LH;
}
else
{
node->balance = EH;
taller = false;
}
}
}
else if (_dataER.time > node->data.time)
{
node->right = insertRec(node->right, _dataER, taller);
if (taller)
{
if (node->balance == LH)
{
node->balance = EH;
taller = false;
}
else if (node->balance == EH)
{
node->balance = RH;
}
else
{
node = rightBalance(node, taller);
taller = false;
}
}
}
else if (_dataER.time == node->data.time)
{
node->data.AP = _dataER.AP;
node->data.BP = _dataER.BP;
}
return node;
}
// Clear
void currFair::clearRec(exRate *&node)
{
if (node != NULL)
{
this->clearRec(node->left);
this->clearRec(node->right);
delete node;
node = NULL;
}
}
void currFair::upLoad(exRate *&node,int _time,float _BP,float _AP,int &result){
if (node == nullptr) return ;
if (_time < node->data.time) upLoad(node->left,_time,_BP,_AP,result);
else if (_time > node->data.time) upLoad(node->right,_time,_BP,_AP,result);
else {
node->data.AP = _AP;
node->data.BP = _BP;
result =1;
}
}
// Trả về 1 cái node có time như vậy hay gần nhất
void currFair::findNode(exRate *node,int _time,exRate *&save){
if (node== nullptr) return;
if (_time< node->data.time) findNode(node->left,_time,save);
else if (_time> node->data.time){
save = node;
findNode(node->right,_time,save);
}
else {
save = node;
}
}