-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
315 lines (299 loc) · 7.11 KB
/
main.cpp
File metadata and controls
315 lines (299 loc) · 7.11 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
310
311
312
313
314
315
#include <fstream>
#include <chrono>
#include "MaxHeap.h"
#include "MinHeap.h"
#include "Hashtable.h"
#include "AvlTree.h"
#include "Graph.h"
bool IsCommand(std::string command, std::string line)
{
return (line.find(command) != std::string::npos);
}
std::string ExtractFilename(std::string command, std::string line)
{
size_t pos = line.find_last_of(command);
std::string filename = line.substr(pos + 1);
return filename;
}
int ExtractNumber(std::string line)
{
std::string n = "";
unsigned int i = 0;
while (i <= line.length() - 1)
{
if (line[i] >= '0' && line[i] <= '9')
{
n += line[i];
}
i++;
}
int number = -1;
if (n != "")
number = stoi(n);
return number;
}
int main()
{
using std::chrono::steady_clock;
std::ifstream commands("commands.txt");
std::ofstream output("output.txt");
if (!commands)
{
output << "ERROR:\tCOMMAND FILE NOT FOUND" << std::endl;
output << "HINT:\tFILE SHOULD BE IN THE SAME FOLDER AS THE EXECUTABLE NAMED \"commands.txt\"" << std::endl;
return 0;
}
MaxHeap maxHeap;
MinHeap minHeap;
Hashtable hashTable;
AVLTree avlTree;
Graph graph;
float totalTimeElapsed = 0.0;
output << "COMMAND RESULT";
std::string line = "";
while (std::getline(commands, line))
{
// Just some formating for the output
output << std::endl << line;
std::string space = "";
for (unsigned int i = 0; i < (50 - line.length()); i++)
space += '.';
output << space;
auto startPoint = std::chrono::steady_clock::now();
int number = ExtractNumber(line);
//~~~~~~~~~~~~~~~ MAX HEAP ~~~~~~~~~~~~~~~
if (IsCommand("BUILD MAXHEAP", line))
{
std::string filename = ExtractFilename("BUILD MAXHEAP ", line);
if (maxHeap.Build(filename))
output << "SUCCESS - ";
else
{
output << "FILE NOT FOUND. MAKE SURE IT IS A .txt LOCATED IN THE FOLDER OF THE EXECUTABLE";
continue;
}
}
else if (IsCommand("INSERT MAXHEAP", line))
{
if (maxHeap.Insert(number))
output << "SUCCESS - ";
else
{
output << "HEAP IS FULL";
continue;
}
}
else if (IsCommand("FINDMAX MAXHEAP", line))
{
int max = maxHeap.GetMax();
if (max != -1)
output << max;
else
output << "HEAP IS EMPTY";
continue;
}
else if (IsCommand("DELETEMAX MAXHEAP", line))
{
if (maxHeap.DeleteMax())
output << "SUCCESS - ";
else
{
output << "HEAP IS EMPTY";
continue;
}
}
else if (IsCommand("GETSIZE MAXHEAP", line))
{
int size = maxHeap.GetSize();
output << size << " ELEMENTS";
continue;
}
//~~~~~~~~~~~~~~~ MIN HEAP ~~~~~~~~~~~~~~~
else if (IsCommand("BUILD MINHEAP", line))
{
std::string filename = ExtractFilename("BUILD MINHEAP ", line);
if (minHeap.Build(filename))
output << "SUCCESS - ";
else
{
output << "FILE NOT FOUND. MAKE SURE IT IS A .txt LOCATED IN THE FOLDER OF THE EXECUTABLE";
continue;
}
}
else if (IsCommand("INSERT MINHEAP", line))
{
if (minHeap.Insert(number))
output << "SUCCESS - ";
else
{
output << "HEAP IS FULL";
continue;
}
}
else if (IsCommand("FINDMIN MINHEAP", line))
{
int min = minHeap.GetMin();
if (min != -1)
output << min;
else
output << "HEAP IS EMPTY";
continue;
}
else if (IsCommand("DELETEMIN MINHEAP", line))
{
if (minHeap.DeleteMin())
output << "SUCCESS - ";
else
{
output << "HEAP IS EMPTY";
continue;
}
}
else if (IsCommand("GETSIZE MINHEAP", line))
{
int size = minHeap.GetSize();
output << size << " ELEMENTS";
continue;
}
//~~~~~~~~~~~~~~~ HASHTABLE ~~~~~~~~~~~~~~~
else if (IsCommand("BUILD HASHTABLE", line))
{
std::string filename = ExtractFilename("BUILD HASHTABLE ", line);
if (hashTable.Build(filename))
output << "SUCCESS - ";
else
{
output << "FILE NOT FOUND. MAKE SURE IT IS A .txt LOCATED IN THE FOLDER OF THE EXECUTABLE";
continue;
}
}
else if (IsCommand("INSERT HASHTABLE", line))
{
if (hashTable.Insert(number))
output << "SUCCESS - ";
else
output << "NUMBER ALREADY EXISTS - ";
}
else if (IsCommand("DELETE HASHTABLE", line))
{
if (hashTable.Delete(number))
output << "SUCCESS - ";
else
output << "NUMBER NOT FOUND - ";
}
else if (IsCommand("SEARCH HASHTABLE", line))
{
if (hashTable.Search(number))
output << "SUCCESS - ";
else
output << "NUMBER NOT FOUND - ";
}
else if (IsCommand("GETSIZE HASHTABLE", line))
{
int size = hashTable.GetSize();
output << size << " ELEMENTS";
continue;
}
//~~~~~~~~~~~~~~~ AVL TREE ~~~~~~~~~~~~~~~
else if (IsCommand("BUILD AVLTREE", line))
{
std::string filename = ExtractFilename("BUILD AVLTREE ", line);
if (avlTree.Build(filename))
output << "SUCCESS - ";
else
{
output << "FILE NOT FOUND. MAKE SURE IT IS A .txt LOCATED IN THE FOLDER OF THE EXECUTABLE";
continue;
}
}
else if (IsCommand("FINDMIN AVLTREE", line))
{
int min = avlTree.GetMin();
if (min != -1)
output << min << " - ";
else
{
output << "AVL TREE IS EMPTY";
continue;
}
}
else if (IsCommand("INSERT AVLTREE", line))
{
avlTree.Insert(number);
}
else if (IsCommand("DELETE AVLTREE", line))
{
if (avlTree.Delete(number))
output << "SUCCESS - ";
else
output << "NUMBER NOT FOUND - ";
}
else if (IsCommand("SEARCH AVLTREE", line))
{
if (avlTree.Search(number))
output << "SUCCESS - ";
else
output << "NUMBER NOT FOUND - ";
}
else if (IsCommand("GETSIZE AVLTREE", line))
{
int size = avlTree.GetSize();
output << size << " ELEMENTS";
continue;
}
//~~~~~~~~~~~~~~~ GRAPH ~~~~~~~~~~~~~~~
else if (IsCommand("BUILD GRAPH", line))
{
std::string filename = ExtractFilename("BUILD GRAPH ", line);
if (graph.Build(filename))
output << "SUCCESS - ";
else
{
output << "FILE NOT FOUND. MAKE SURE IT IS A .txt LOCATED IN THE FOLDER OF THE EXECUTABLE";
continue;
}
}
else if (IsCommand("GETSIZE GRAPH", line))
{
int nodes, links;
graph.GetSize(nodes, links);
output << nodes << " NODE, " << links << " LINKS";
continue;
}
else if (IsCommand("INSERT GRAPH", line))
{
if (graph.Insert(line))
output << "SUCCESS - ";
else
output << "COULD NOT INSERT NEW NODE";
}
else if (IsCommand("DELETE GRAPH", line))
{
if (graph.Delete(line))
output << "SUCCESS - ";
else
output << "NUMBERS OR LINK NOT FOUND - ";
}
else if (IsCommand("FINDCONNECTEDCOMPONENTS GRAPH", line))
{
int components = graph.ConnectedComponents();
output << components << " - ";
}
else if (IsCommand("COMPUTESPANNINGTREE GRAPH", line))
{
output << graph.ComputeSpanningTree();
}
else if (IsCommand("COMPUTESHORTESTPATH GRAPH", line))
{
output << "NOT SUPPORTED";
continue;
}
auto endPoint = std::chrono::steady_clock::now();
auto commandRuntime = std::chrono::duration_cast<std::chrono::duration<float>>(endPoint - startPoint);
float durationMs = commandRuntime.count() * 1000.0f;
totalTimeElapsed += durationMs;
output << durationMs << " ms";
}
output << "\n\nTOTAL TIME ELAPSED " << totalTimeElapsed << " ms" << std::endl;
return 0;
}