Add heap tester.
This commit is contained in:
parent
17c38c219f
commit
52edc7807a
84
main.cpp
84
main.cpp
@ -1,11 +1,9 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
|
||||||
#include <algorithm>
|
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
|
||||||
using string = std::string;
|
using string = std::string;
|
||||||
|
|
||||||
//Placw in header!
|
//Place in header!
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void printm(T m){
|
void printm(T m){
|
||||||
std::cout << m;
|
std::cout << m;
|
||||||
@ -17,37 +15,71 @@ void printlm(T m){
|
|||||||
printm("\n");
|
printm("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
double promptd(string,bool*);
|
class Heap {
|
||||||
|
int pos {0};
|
||||||
|
int length {0};
|
||||||
|
int size;
|
||||||
|
int* storage;
|
||||||
|
public:
|
||||||
|
Heap(int sz) {
|
||||||
|
size = sz;
|
||||||
|
storage = new int[sz];
|
||||||
|
}
|
||||||
|
~Heap() {
|
||||||
|
delete[] storage;
|
||||||
|
}
|
||||||
|
bool isEmpty() {
|
||||||
|
return length == 0;
|
||||||
|
}
|
||||||
|
void add(int item) {
|
||||||
|
if (length + 1 > size) return;
|
||||||
|
pos = length;
|
||||||
|
while (pos > 0 && storage[(pos-1)/2] < item) {
|
||||||
|
storage[pos] = storage[(pos-1)/2];
|
||||||
|
pos = (pos-1)/2;
|
||||||
|
}
|
||||||
|
storage[pos] = item;
|
||||||
|
++length;
|
||||||
|
}
|
||||||
|
int extractMax() {
|
||||||
|
int max {storage[0]};
|
||||||
|
storage[0] = storage[--length];
|
||||||
|
int first {0};
|
||||||
|
while ((2*first)+1 <= length - 1) {
|
||||||
|
int largest {(2*first)+1};
|
||||||
|
if ((2*first)+2 <= length - 1 && storage[largest] < storage[(2*first)+2]) largest = (2*first)+2;
|
||||||
|
if (storage[first] >= storage[largest]) break;
|
||||||
|
int swap = storage[first];
|
||||||
|
storage[first] = storage[largest];
|
||||||
|
storage[largest] = swap;
|
||||||
|
first = largest;
|
||||||
|
}
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int prompti(string,bool*);
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::setprecision(3);
|
printlm("Create Heap:");
|
||||||
printlm("Calculate median and average from sequence:");
|
Heap h{16};
|
||||||
std::vector<double> v{};
|
|
||||||
bool s {};
|
bool s {};
|
||||||
|
int i {0};
|
||||||
do {
|
do {
|
||||||
auto d = promptd("Enter a number (Enter non-number to complete entry) : ",&s);
|
auto d = prompti("Enter a number: ",&s);
|
||||||
if (s) v.push_back(d);
|
if (s) {
|
||||||
|
h.add(d);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
while(s);
|
while(s && i < 16);
|
||||||
sort(v.begin(), v.end());
|
printlm("Output Heap:");
|
||||||
printm("The count is: ");
|
while (!h.isEmpty()) printlm(h.extractMax());
|
||||||
printlm(v.size());
|
|
||||||
printm("The median is: ");
|
|
||||||
int halfway {static_cast<int>(v.size()) / 2};
|
|
||||||
if (static_cast<int>(v.size())%2 == 1)
|
|
||||||
printlm(v.at(halfway));
|
|
||||||
else
|
|
||||||
printlm((v.at(halfway - 1) + v.at(halfway)) / 2.0);
|
|
||||||
double avg {};
|
|
||||||
for (int i = 0; i < static_cast<int>(v.size()); ++i) avg += v.at(i);
|
|
||||||
avg /= static_cast<int>(v.size());
|
|
||||||
printm("The average is: ");
|
|
||||||
printlm(avg);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
double promptd(string message, bool *status) {
|
int prompti(string message, bool *status) {
|
||||||
double tr {};
|
int tr {};
|
||||||
*status = false;
|
*status = false;
|
||||||
std::cout << message;
|
std::cout << message;
|
||||||
if (!(std::cin >> tr)){
|
if (!(std::cin >> tr)){
|
||||||
|
Loading…
Reference in New Issue
Block a user