diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..44da864 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,45 @@ +#include "histogram.h" +#include +#include +#include + +void +find_minmax(const std::vector& numbers, double& min, double& max, bool& res){ + if (numbers.size() == 0){ + res = false; + return; + } + min = numbers[0]; + max = numbers[0]; + for (auto x : numbers) { + if (x < min) { + min = x; + } + else if (x > max) { + max = x; + } + } +} +std::vector +make_histogram(const std::vector& numbers, size_t bin_count){ + double min, max; + bool res = true; + find_minmax (numbers, min, max, res); + if (res == false){ + std::cerr << "Number of elements cannot be equal to zero"; + exit(1); + } + double bin_size = (max - min) / bin_count; + std::vector bins(bin_count); + for (auto x : numbers) { + bool found = false; + for (size_t j = 0; (j < bin_count - 1) && !found ; j++) { + if ((min + j * bin_size <= x) && (x < min + (j + 1) * bin_size)) { + bins[j] += 1; + found = true; + } + } + if (!found) bins[bin_count - 1]++; + } + return bins; +} diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..71ce8fa --- /dev/null +++ b/histogram.h @@ -0,0 +1,6 @@ +#pragma once +#include + +std::vector +make_histogram(const std::vector& numbers, size_t bin_count); + diff --git a/main.cpp b/main.cpp index d30982d..a8e168f 100644 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,10 @@ #include #include +#include "histogram.h" +#include "text.h" using namespace std; -const size_t SCREEN_WIDTH = 80; -const size_t MAX_LENGTH = SCREEN_WIDTH - 3 - 1; - struct Input { vector numbers; size_t bin_count{}; @@ -28,83 +27,9 @@ input_data(){ return in; } -void -find_minmax(const std::vector& numbers, double& min, double& max, bool& res){ - if (numbers.size() == 0){ - res = false; - return; - } - min = numbers[0]; - max = numbers[0]; - for (auto x : numbers) { - if (x < min) { - min = x; - } - else if (x > max) { - max = x; - } - } -} -std::vector -make_histogram(const std::vector& numbers, size_t bin_count){ - double min, max; - bool res = true; - find_minmax (numbers, min, max, res); - if (res == false){ - std::cerr << "Number of elements cannot be equal to zero"; - exit(1); - } - double bin_size = (max - min) / bin_count; - std::vector bins(bin_count); - for (auto x : numbers) { - bool found = false; - for (size_t j = 0; (j < bin_count - 1) && !found ; j++) { - if ((min + j * bin_size <= x) && (x < min + (j + 1) * bin_size)) { - bins[j] += 1; - found = true; - } - } - if (!found) bins[bin_count - 1]++; - } - return bins; -} - -void -show_histogram_text(vector bins, size_t bin_count ){ - auto max_count = bins[0]; - for (auto x : bins) { - if (x > max_count) { - max_count = x; - } - } - for (size_t i = 0; i < bin_count; i++) { - size_t j = 100; - while (bins[i] < j) { - cout << " "; - j /= 10; - } - cout << bins[i] << "|"; - if (max_count > MAX_LENGTH) { - auto count = bins[i]; - size_t height = MAX_LENGTH * (static_cast(count) / max_count); - j = 0; - while (j < height) { - cout << "*"; - j++; - } - } - else { - j = 0; - while (j < bins[i]) { - cout << "*"; - j++; - } - } - cout << endl; - } -} -int main() { +int +main() { auto in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); show_histogram_text(bins, in.bin_count); diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..1e1fdca --- /dev/null +++ b/text.cpp @@ -0,0 +1,43 @@ +#include "text.h" +#include +#include + +using namespace std; + +const size_t SCREEN_WIDTH = 80; +const size_t MAX_LENGTH = SCREEN_WIDTH - 3 - 1; + +void +show_histogram_text(vector bins, size_t bin_count ){ + auto max_count = bins[0]; + for (auto x : bins) { + if (x > max_count) { + max_count = x; + } + } + for (size_t i = 0; i < bin_count; i++) { + size_t j = 100; + while (bins[i] < j) { + cout << " "; + j /= 10; + } + cout << bins[i] << "|"; + if (max_count > MAX_LENGTH) { + auto count = bins[i]; + size_t height = MAX_LENGTH * (static_cast(count) / max_count); + j = 0; + while (j < height) { + cout << "*"; + j++; + } + } + else { + j = 0; + while (j < bins[i]) { + cout << "*"; + j++; + } + } + cout << endl; + } +} diff --git a/text.h b/text.h new file mode 100644 index 0000000..52f62a7 --- /dev/null +++ b/text.h @@ -0,0 +1,5 @@ +#pragma once +#include + +void +show_histogram_text(std::vector bins, size_t bin_count );