-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainAVL.cpp
More file actions
309 lines (262 loc) · 6.42 KB
/
Copy pathmainAVL.cpp
File metadata and controls
309 lines (262 loc) · 6.42 KB
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
// ---------- Projeto II AVL - EDII ------------
// --------- Grupo cavalo_sentado.png ----------
// Claudia Fiorentino Andrade - 42005302
// João Victor Ferreira Pimenta - 42005876
// Joyce Cui - 42017157
// Ryan Marco Andrade dos Santos - 42080223
// Victor Prado Chaves - 32070772
// mainAVL.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <list>
#include <iterator>
#include "AVL.h"
#include "Alimento.h"
// Prototipos das funcoes presentes ----------------------
void Insert(AVL *avl, Alimento novo_alimento);
void Remove(AVL *avl);
void Search(AVL *avl);
void Predecessor(AVL *avl);
void Successor(AVL *avl);
void FindMin(AVL *avl);
void FindMax(AVL *avl);
void TraverseInOrder(AVL *avl);
void TraversePreOrder(AVL *avl);
void TraversePostOrder(AVL *avl);
void Clear(AVL *avl);
// Lendo Arquivo CSV -----------------------------------
void LerArq()
{
std::string linha, temp;
std::ifstream arq("NutritionalFacts_Fruit_Vegetables_Seafood - NutritionalFacts_Fruit_Vegetables_Seafood.csv");
Alimento alimento;
std::string dados[11];
int j;
if (arq.is_open())
{
// define arquivo
while (getline(arq, linha))
{
// define linha
for (int x = 0; x < 12; x++)
{
temp = "";
// define palavra
for (int i = 0; i < linha.size(); i++)
{
if (linha[i] != ',')
{
temp = temp + linha[i];
}
}
dados[x] = temp;
}
}
// Alimento alimento = Alimento(dados);
}
else
{
std::cout << "Erro! Nao foi possivel ler o arquivo";
}
}
// Implementações das funções ----------------------------
void Insert(AVL *avl, Alimento novo_alimento)
{
NodeAVL *node = avl->Insert(novo_alimento);
if (node)
std::cout << "Node inserted!\n";
else
std::cout << "*** ERROR! Couldn't insert node!\n";
}
void Remove(AVL *avl)
{
std::string nomeAlimento;
std::cout << "Remove: ";
std::cin >> nomeAlimento;
avl->Remove(nomeAlimento);
}
void Search(AVL *avl)
{
std::string nomeAlimento;
std::cout << "Search food name: ";
std::cin >> nomeAlimento;
NodeAVL *node = avl->Search(nomeAlimento);
if (node)
std::cout << "Node found:\n"
<< node->ToString();
else
std::cout << "*** ERROR! Couldn't find node!\n";
}
void Predecessor(AVL *avl)
{
std::string nomeAlimento;
std::cout << "Find predecessor of: ";
std::cin >> nomeAlimento;
NodeAVL *node = avl->Predecessor(nomeAlimento);
if (node)
std::cout << "Predecessor of " << nomeAlimento << ":\n"
<< node->ToString();
else
std::cout << "*** ERROR! There is no predecessor of " << nomeAlimento << "!\n";
}
void Successor(AVL *avl)
{
std::string nomeAlimento;
std::cout << "Find successor of: ";
std::cin >> nomeAlimento;
NodeAVL *node = avl->Successor(nomeAlimento);
if (node)
std::cout << "Successor of " << nomeAlimento << ":\n"
<< node->ToString();
else
std::cout << "*** ERROR! There is no successor of " << nomeAlimento << "!\n";
}
void FindMin(AVL *avl)
{
NodeAVL *node = avl->FindMin();
if (node)
std::cout << "Minimum:\n"
<< node->ToString();
else
std::cout << "*** ERROR! Couldn't find minimum (tree is probably empty...)\n";
}
void FindMax(AVL *avl)
{
NodeAVL *node = avl->FindMax();
if (node)
std::cout << "Maximum:\n"
<< node->ToString();
else
std::cout << "*** ERROR! Couldn't find maximum (tree is probably empty...)\n";
}
void TraverseInOrder(AVL *avl)
{
std::cout << avl->TraverseInOrder() << '\n';
}
void TraversePreOrder(AVL *avl)
{
std::cout << avl->TraversePreOrder() << '\n';
}
void TraversePostOrder(AVL *avl)
{
std::cout << avl->TraversePostOrder() << '\n';
}
void Clear(AVL *avl)
{
avl->Clear();
}
// Funções da atividade ----------------------------------
std::list<std::string> gera_consumidos()
{
std::list<std::string> lista_consumidos;
std::string consumidos;
std::cout << "Quais foram os alimentos consumidos (digite '-1' para finalizar): ";
while (true)
{
std::cin >> consumidos;
if (consumidos == "-1") return lista_consumidos;
else lista_consumidos.push_back(consumidos);
}
}
// Estas funções que são chamadas
// pela main devem receber a AVL e
// dentro delas serão requisitadas as
// variáveis p/ realizar operações
// Leitura dos Dados
// Valores Nutricionais
// Qntd de Calorias
void op3(AVL* avl){
std::list<std::string> consumidos = gera_consumidos();
std::cout << avl-> Qnt_Calories(consumidos);
}
// Qntd de Vitaminas
void op4 (AVL *avl)
{
std::list<std::string> consumidos = gera_consumidos();
std::cout << avl->QntdVitaminas(consumidos);
}
// Qntd de Proteínas
void op5 (AVL *avl)
{
std::list<std::string> consumidos = gera_consumidos();
std::cout << avl -> QntdProteinas(consumidos);
}
// Outras Informações
void op6 (AVL * avl)
{
std::list<std::string> consumidos = gera_consumidos();
std::cout << avl->OutrasInfos(consumidos) << std::endl;
}
// Síntese Nutricional
void SinteseNutri(AVL* avl){
// int qtde;
// Alimento comida;
// std::cout<<"Defina a quantidade de alimentos consumidos";
// std::cin>> qtde;
// Alimento alimentos[qtde]; // Importando a relação de alimentos consumidos. Talvez o vetor possa ser do tipo string, pois só será usado o nome do alimento
// for(int i=0; i<qtde; i++) //popula vetor
// std::cin>>comida;
// Sintese(avl, alimentos);
std::list<std::string> consumidos = gera_consumidos();
std::cout << avl->Sintese(consumidos) << std::endl;
}
void ValoresNutri(AVL *avl)
{
std::string nomeAlimento;
std::cout << "Search food name: ";
std::cin >> nomeAlimento;
std::cout << avl->ValoresNutri(nomeAlimento);
}
// Implementação das funções -----------------------------
int main()
{
AVL *avl = new AVL();
LerArq();
int option = -1;
do
{
// Menu
std::cout << "*** AVL Tree *** \n"
<< "[1] Leitura dos Dados \n"
<< "[2] Valores Nutricionais \n"
<< "[3] Quantidade de Calorias \n"
<< "[4] Quantidade de Vitaminas \n"
<< "[5] Quantidade de Proteinas \n"
<< "[6] Outras Informacoes \n"
<< "[7] Sintese Nutricional \n"
<< "[8] EXIT \n"
<< "Option: ";
std::cin >> option;
std::cout << '\n';
switch (option)
{
case 1:
TraverseInOrder(avl);
break;
case 2:
ValoresNutri(avl);
break;
case 3:
op3(avl);
break;
case 4:
op4(avl);
break;
case 5:
op5(avl);
break;
case 6:
op6(avl);
break;
case 7:
SinteseNutri(avl);
break;
case 8:
break;
}
std::cout << '\n';
} while (option != 8);
std::cout << "-------. Adios .-------";
delete avl;
}