-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestao8.c
102 lines (80 loc) · 2.35 KB
/
questao8.c
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
#include "common/common.h"
#include "structs/header/avlTree.h"
#include "structs/header/RBTree.h"
#include <time.h>
int main(int argc, char *argv[]){
if(argc == 1){
printLn("Árvore inálida!");
printLn("Árvores: rbtree ou avltree.");
return 1;
}
if(argc == 2){
printLn("Arquivo inálido!");
printLn("Informe o caminho de um arquivo válido.");
return 1;
}
if(argc == 3){
printLn("Tamanho de buffer inválido");
printLn("Informe um tamanho de buffer válido.");
return 1;
}
String tree = newString(argv[1]);
String fileName = newString(argv[2]);
int size = atoi(argv[3]);
if(size <= 0){
printLn("Tamanho de buffer inválido!");
printLn("Informe um tamanho de buffer válido.");
return 1;
}
int *data = (int *) malloc(sizeof(int) * size);
size = readOutputFile(fileName, data, size);
char outputFile[255];
char datetime[100];
time_t now = time(0);
strftime (datetime, 100, "%Y-%m-%dT%H:%M:%S", localtime (&now));
sprintf(outputFile, "output/out-%s-%s_%s.txt", getCStr(tree), argv[3], datetime);
long int t = 0;
if(cStrIsEqual(tree, "rbtree")){
RBTreeInt rb = newRBTreeInt();
t = getMicroTime();
for(int i = 0; i < size; i++){
insertRBTreeInt(rb, data[i]);
}
t = getMicroTime() - t;
FILE * rbFile;
rbFile = fopen (outputFile , "w+");
if (rbFile != NULL){
fPrintRBTreeInt(rbFile, rb);
fclose(rbFile);
}
deleteRBTreeInt(rb);
}
else if(cStrIsEqual(tree, "avltree")){
AvlTreeInt avl = newAvlTreeInt();
t = getMicroTime();
for(int i = 0; i < size; i++){
insertAvlTreeInt(avl, data[i]);
}
t = getMicroTime() - t;
FILE * avlFile;
avlFile = fopen (outputFile , "w+");
if (avlFile != NULL){
fPrintAvlTreeInt(avlFile, avl);
fclose(avlFile);
}
deleteAvlTreeInt(avl);
}
else{
printLn("Árvore inálida!");
printLn("Árvores: rbtree ou avltree.");
deleteString(tree);
deleteString(fileName);
free(data);
return 1;
}
printf("%ld", t);
deleteString(tree);
deleteString(fileName);
free(data);
return 0;
}