diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..5bfcbfd --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,57 @@ +#include "histogram.h" +#include + +static void find_minmax(const std::vector& numbers, double& min, double& max) +{ + min = numbers[0]; + for (double x : numbers) + { + if (x < min) + { + min = x; + } + else if (x > max) + { + max = x; + } + } +} + +std::vector make_histogram(const std::vector &numbers, std::size_t bin_count) +{ + double min = numbers[0]; + double max = numbers[0]; + find_minmax(numbers, min, max); + double bin_size = (max - min) / bin_count; + std::vectorbins(bin_count); + + for (std::size_t i = 0; i < numbers.size(); i++) + { + bool found = false; + for (std::size_t j = 0; (j < bin_count - 1) && !found; j++) + { + auto lo = min + j * bin_size; + auto hi = min + (j + 1) * bin_size; + if ((lo <= numbers[i]) && (numbers[i] < hi)) + { + bins[j]++; + found = true; + } + } + if (!found) + { + bins[bin_count - 1]++; + } + } + double b; + for (std::size_t i = 0; i < bin_count-1; i++) + { + if (bins[i] > bins[i+1]) + { + b = bins[i]; + bins[i] = bins[i + 1]; + bins[i + 1] = b; + } + } + return bins; +} diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..fb756b1 --- /dev/null +++ b/histogram.h @@ -0,0 +1,8 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED +#include + +std::vector make_histogram(const std::vector& numbers, std::size_t bin_count); + + +#endif // HISTOGRAM_H_INCLUDED diff --git a/main.cpp b/main.cpp index b8cef5c..4866c09 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,8 @@ #include #include +#include "histogram.h" +#include "text.h" + using namespace std; struct Input @@ -25,110 +28,6 @@ Input input_data() return in; } -void find_minmax(const vector& numbers, double& min, double& max) -{ - min = numbers[0]; - for (double x : numbers) - { - if (x < min) - { - min = x; - } - else if (x > max) - { - max = x; - } - } -} - -vector make_histogram(const vector &numbers, size_t bin_count) -{ - double min = numbers[0]; - double max = numbers[0]; - find_minmax(numbers, min, max); - double bin_size = (max - min) / bin_count; - vectorbins(bin_count); - - for (size_t i = 0; i < numbers.size(); i++) - { - bool found = false; - for (size_t j = 0; (j < bin_count - 1) && !found; j++) - { - auto lo = min + j * bin_size; - auto hi = min + (j + 1) * bin_size; - if ((lo <= numbers[i]) && (numbers[i] < hi)) - { - bins[j]++; - found = true; - } - } - if (!found) - { - bins[bin_count - 1]++; - } - } - double b; - for (size_t i = 0; i < bin_count-1; i++) - { - if (bins[i] > bins[i+1]) - { - b = bins[i]; - bins[i] = bins[i + 1]; - bins[i + 1] = b; - } - } - return bins; -} - -void show_histogram_text(const vector &bins) -{ - const size_t SCREEN_WIDTH = 80; - const size_t MAX_STAR = SCREEN_WIDTH - 3 - 1; - size_t max_star_search = bins[-1]; - - if (max_star_search > MAX_STAR) - { - for (size_t x : bins) - { - size_t height = MAX_STAR * (static_cast(x) / max_star_search); - if (x < 100) - { - cout << " "; - if (x < 10) - { - cout << " "; - } - } - cout << x << "|"; - for (size_t i = 0; i < height; i++) - { - cout << "*"; - } - cout << endl; - } - } - else - { - for (size_t x : bins) - { - if (x < 100) - { - cout << " "; - if (x < 10) - { - cout << " "; - } - } - cout << x << "|"; - for (size_t i = 0; i < x; i++) - { - cout << "*"; - } - cout << endl; - } - } -} - int main() { auto in = input_data(); diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..a787216 --- /dev/null +++ b/text.cpp @@ -0,0 +1,52 @@ +#include +#include +#include "text.h" + +void show_histogram_text(const std::vector &bins) +{ + const size_t SCREEN_WIDTH = 80; + const size_t MAX_STAR = SCREEN_WIDTH - 3 - 1; + size_t max_star_search = bins[-1]; + + if (max_star_search > MAX_STAR) + { + for (size_t x : bins) + { + size_t height = MAX_STAR * (static_cast(x) / max_star_search); + if (x < 100) + { + std::cout << " "; + if (x < 10) + { + std::cout << " "; + } + } + std::cout << x << "|"; + for (size_t i = 0; i < height; i++) + { + std::cout << "*"; + } + std::cout << std::endl; + } + } + else + { + for (size_t x : bins) + { + if (x < 100) + { + std::cout << " "; + if (x < 10) + { + std::cout << " "; + } + } + std::cout << x << "|"; + for (size_t i = 0; i < x; i++) + { + std::cout << "*"; + } + std::cout << std::endl; + } + } +} diff --git a/text.h b/text.h new file mode 100644 index 0000000..c63e4df --- /dev/null +++ b/text.h @@ -0,0 +1,7 @@ +#ifndef TEXT_H_INCLUDED +#define TEXT_H_INCLUDED +#include + +void show_histogram_text(const std::vector &bins); + +#endif // TEXT_H_INCLUDED