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 <vector>
|
||||
#include <algorithm>
|
||||
#include <iomanip>
|
||||
|
||||
using string = std::string;
|
||||
|
||||
//Placw in header!
|
||||
//Place in header!
|
||||
template <typename T>
|
||||
void printm(T m){
|
||||
std::cout << m;
|
||||
@ -17,37 +15,71 @@ void printlm(T m){
|
||||
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() {
|
||||
std::setprecision(3);
|
||||
printlm("Calculate median and average from sequence:");
|
||||
std::vector<double> v{};
|
||||
printlm("Create Heap:");
|
||||
Heap h{16};
|
||||
bool s {};
|
||||
int i {0};
|
||||
do {
|
||||
auto d = promptd("Enter a number (Enter non-number to complete entry) : ",&s);
|
||||
if (s) v.push_back(d);
|
||||
auto d = prompti("Enter a number: ",&s);
|
||||
if (s) {
|
||||
h.add(d);
|
||||
++i;
|
||||
}
|
||||
while(s);
|
||||
sort(v.begin(), v.end());
|
||||
printm("The count is: ");
|
||||
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);
|
||||
}
|
||||
while(s && i < 16);
|
||||
printlm("Output Heap:");
|
||||
while (!h.isEmpty()) printlm(h.extractMax());
|
||||
return 0;
|
||||
}
|
||||
|
||||
double promptd(string message, bool *status) {
|
||||
double tr {};
|
||||
int prompti(string message, bool *status) {
|
||||
int tr {};
|
||||
*status = false;
|
||||
std::cout << message;
|
||||
if (!(std::cin >> tr)){
|
||||
|
Loading…
Reference in New Issue
Block a user