-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
139 lines (109 loc) · 4.06 KB
/
main.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
#include <algorithm>
#include <iostream>
#include <string>
#include <stack>
#include <regex>
#include <avl_tree.hpp>
using namespace std;
using namespace regex_constants;
static const regex INSERT(R"(^\s*(?:i|insert)\s*(\d+)\s*$)", icase | optimize);
static const regex REMOVE(R"(^\s*(?:r|remove)\s*(\d+)\s*$)", icase | optimize);
static const regex PRINT(R"(^\s*(?:p|print)\s*(in|pre|post|level)?\s*$)", icase | optimize);
static const regex CLEAR(R"(^\s*(?:c|r|clear|reset)\s*$)", icase | optimize);
static const regex SAVE(R"(^\s*(?:s|save)\s+([^\\\?%\*]+)\s*$)", icase | optimize);
static const regex SAVEGV(R"(^\s*(?:g|graphviz)\s+([^\\\?%\*]+)\s*$)", icase | optimize);
static const regex QUIT(R"(^\s*(?:q|quit|exit)\s*$)", icase | optimize);
/**
* @brief Ponto de entrada
*
* @param argc Número de argumentos da linha de comando
* @param argv Valores dos argumentos da linha de comando
* @return int Código de retorno
*/
int main(int argc, char** argv) {
string line;
smatch m;
avl_tree<int> tree;
// Cabeçalho
cout << "Interactive AVL Tree" << endl;
cout << endl;
cout << "i|insert x : Insert X" << endl;
cout << "r|remove x : Remove X" << endl;
cout << "p|print [(sorted|level)] : Print out" << endl;
cout << "s|save <filename> : Save to file" << endl;
cout << "g|graphviz <filename> : Save Graphviz model to file" << endl;
cout << "c|r|clear|reset : Reset" << endl;
cout << "q|e|quit|exit : Quit" << endl;
cout << endl;
cout << "Have fun!" << endl;
for (;;) {
cout << "avl (" << tree.size() << ")> ";
getline(cin, line);
// Encerra o programa
if (regex_match(line, QUIT)) {
break;
// Insere um valor na árvore
} else if (regex_search(line, m, INSERT)) {
int n = stoi(m[1]);
try {
tree.insert(n);
} catch (const char* msg) {
cerr << "Err: " << msg << endl;
}
// Remove um valor da árvore
} else if (regex_search(line, m, REMOVE)) {
int n = stoi(m[1]);
try {
tree.remove(n);
} catch (const char* msg) {
cerr << "Err: " << msg << endl;
}
// Escreve a árvore na tela
} else if (regex_search(line, m, PRINT)) {
string mode = m[1];
if (mode.empty())
mode = "sorted";
transform(
mode.begin(), mode.end(),
mode.begin(),
::tolower
);
// Escreve os elementos da árvore em ordem
if (mode == "sorted") {
for (auto it = tree.begin_in_order(); it != tree.end_in_order(); it++)
cout << *it << " ";
cout << endl;
// Escreve os elementos da árvore por nível
} else if (mode == "level") {
int level = 0;
for (auto it = tree.begin_by_level(); it != tree.end_by_level(); it++) {
if (level != it.level()) {
cout << endl;
level = it.level();
}
cout << *it << " ";
}
cout << endl;
} else
cerr << "Err: Invalid printing mode `" << mode << "'" << endl;
// Salva a árvore num arquivo
} else if (regex_search(line, m, SAVE)) {
ofstream f(m[1], ios::binary);
f << tree;
f.close();
// Salva a árvore como modelo do graphviz num arquivo
} else if (regex_search(line, m, SAVEGV)) {
ofstream f(m[1]);
tree.gv_save(f);
f.close();
// Limpa a árvore
} else if (regex_match(line, CLEAR)) {
tree.clear();
// Comando inválido
} else {
cerr << "Err: Invalid command" << endl;
continue;
}
}
return 0;
}