From 024a4b0024b122be58ba552c81a0fe7e6d9b954a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D0=BE=D0=BD=D0=BE=D0=B2=20=D0=90=D0=BB=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B4=D1=80?= Date: Sat, 22 Apr 2023 21:21:51 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A4=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=BD=D0=B5=D1=81=D0=B5=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=B2=20=D0=BE=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D0=B5?= =?UTF-8?q?=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 --- .gitignore | 3 +- histogram.cpp | 47 ++++++++++++++++++++++++++++ histogram.h | 6 ++++ main.cpp | 86 ++------------------------------------------------- text.cpp | 41 ++++++++++++++++++++++++ text.h | 7 +++++ 6 files changed, 105 insertions(+), 85 deletions(-) create mode 100644 histogram.cpp create mode 100644 histogram.h create mode 100644 text.cpp create mode 100644 text.h diff --git a/.gitignore b/.gitignore index 30a706c..4493497 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ main.dSYM/ .vscode/ -main \ No newline at end of file +main +.DS_Store \ No newline at end of file diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..7bdb82e --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,47 @@ +#include "histogram.h" + +static void find_minmax(const std::vector &numbers, double &min, double &max) +{ + min = numbers[0]; + max = 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, const size_t &bin_count) +{ + double min, max; + find_minmax(numbers, min, max); + + std::vector bins(bin_count); + double bin_size = (max - min) / 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]++; + } + } + return bins; +} \ No newline at end of file diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..c1fb9f8 --- /dev/null +++ b/histogram.h @@ -0,0 +1,6 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED +#include + +std::vector make_histogram(const std::vector &numbers,const size_t& bin_count); +#endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index 8ba0c00..c78ce6b 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,7 @@ #include #include +#include "histogram.h" +#include "text.h" struct Input { @@ -25,90 +27,6 @@ Input input_data() return in; } -void find_minmax(const std::vector &numbers, double &min, double &max) -{ - min = numbers[0]; - max = 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, const size_t &bin_count) -{ - double min, max; - find_minmax(numbers, min, max); - - std::vector bins(bin_count); - double bin_size = (max - min) / 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]++; - } - } - return bins; -} - -void show_histogram_text(const std::vector &bins, const std::vector &numbers) -{ - const size_t SCREEN_WIDTH = 80; - const size_t MAX_ASTERISK = SCREEN_WIDTH - 5; - size_t max_count = 0; - for (auto bin : bins) - { - if (bin > max_count) - max_count = bin; - } - - for (auto bin : bins) - { - size_t height; - if (max_count <= 76) - { - height = bin; - } - else - { - height = MAX_ASTERISK * (static_cast(bin) / max_count); - } - - int fraction = bin * 100 / numbers.size(); - - if (fraction < 100) - std::cout << ' '; - if (fraction < 10) - std::cout << ' '; - std::cout << fraction << "|"; - - for (size_t j = 0; j < height; j++) - { - std::cout << '*'; - } - std::cout << std::endl; - } -} int main() { diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..fb8a544 --- /dev/null +++ b/text.cpp @@ -0,0 +1,41 @@ +#include "text.h" + +void show_histogram_text(const std::vector &bins, const std::vector &numbers) +{ + + const size_t SCREEN_WIDTH = 80; + const size_t MAX_ASTERISK = SCREEN_WIDTH - 5; + size_t max_count = 0; + for (auto bin : bins) + { + if (bin > max_count) + max_count = bin; + } + + for (auto bin : bins) + { + size_t height; + if (max_count <= 76) + { + height = bin; + } + else + { + height = MAX_ASTERISK * (static_cast(bin) / max_count); + } + + int fraction = bin * 100 / numbers.size(); + + if (fraction < 100) + std::cout << ' '; + if (fraction < 10) + std::cout << ' '; + std::cout << bin << "|"; + + for (size_t j = 0; j < height; j++) + { + std::cout << '*'; + } + std::cout << std::endl; + } +} \ No newline at end of file diff --git a/text.h b/text.h new file mode 100644 index 0000000..fc4cae5 --- /dev/null +++ b/text.h @@ -0,0 +1,7 @@ +#ifndef TEXT_H +#define TEXT_H +#include +#include + +void show_histogram_text(const std::vector &bins, const std::vector &numbers); +#endif \ No newline at end of file