From 57baf2a046fb08d34034fb6fb0cd0269858b85ab Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 21 May 2024 21:26:55 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B3=D1=80=D0=B0=D0=BC?= =?UTF-8?q?=D0=BC=D0=B0=20=D1=80=D0=B0=D0=B7=D0=B1=D0=B8=D1=82=D0=B0=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 57 +++++++++++++++++++++++++++ histogram.h | 5 +++ main.cpp | 106 +------------------------------------------------- text.cpp | 52 +++++++++++++++++++++++++ text.h | 5 +++ 5 files changed, 121 insertions(+), 104 deletions(-) create mode 100644 histogram.cpp create mode 100644 histogram.h create mode 100644 text.cpp create mode 100644 text.h 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..6d24980 --- /dev/null +++ b/histogram.h @@ -0,0 +1,5 @@ +#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 1ca3faa..b5028a4 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,7 @@ #include #include +#include "histogram.h" +#include "text.h" using namespace std; @@ -26,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..4c08da5 --- /dev/null +++ b/text.h @@ -0,0 +1,5 @@ +#ifndef TEXT_H_INCLUDED +#define TEXT_H_INCLUDED +#include +void show_histogram_text(const std::vector &bins); +#endif // TEXT_H_INCLUDED