-
Notifications
You must be signed in to change notification settings - Fork 0
/
program4.cpp
64 lines (57 loc) · 1.48 KB
/
program4.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
/*
* Name: Chad Smith
* Assignment: Program 4 - Min Heaps
* Due Date: November 14, 2015
*/
#include <iostream>
#include <fstream>
#include "Helpers.h"
#include "MinHeap.h"
template <class T>
void RunOperation(char operation, const std::vector<std::string>& arguments, MinHeap<T>& heap, std::ostream& file);
int main(int argc, char** argv)
{
std::vector<int> heapVector;
std::vector<std::string> commandVector;
BuildVector(commandVector, std::cin);
heapVector.reserve(500);
MinHeap<int> heap(heapVector);
std::ofstream file;
file.open("output.txt");
std::vector<std::string>::iterator iter;
for (iter = commandVector.begin(); iter != commandVector.end(); ++iter)
{
// split on the space to get the operation and command
auto command = SplitString(*iter, ' ');
auto operation = (*command.begin())[0];
RunOperation(operation, command, heap, std::cout);
}
file.close();
return 0;
}
// Runs the specified operation on the given heap with the arguments
template<class T>
void RunOperation(char operation, const std::vector<std::string>& arguments, MinHeap<T>& heap, std::ostream& file)
{
if(operation == 'I')
{
auto value = ToInt(arguments[1]);
heap.Insert(value);
}
else if(operation == 'D')
{
auto value = ToInt(arguments[1]);
heap.Delete(value);
}
else if(operation == 'C')
{
auto oldValue = ToInt(arguments[1]);
auto newValue = ToInt(arguments[2]);
heap.Change(oldValue, newValue);
}
else if(operation == 'P')
{
heap.PrintPostOrder(file);
file << std::endl;
}
}