From 20bcfffcc5984ef8d55686bb2ec5d323ee74c97d Mon Sep 17 00:00:00 2001 From: AkinshinaDA Date: Fri, 21 Mar 2025 00:52:33 +0300 Subject: [PATCH] =?UTF-8?q?build:=203.=20=D0=A0=D0=B0=D0=B7=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D1=80=D0=BE=D0=B3=D1=80?= =?UTF-8?q?=D0=B0=D0=BC=D0=BC=D1=8B=20=D0=BD=D0=B0=20=D1=84=D0=B0=D0=B9?= =?UTF-8?q?=D0=BB=D1=8B.=20=D0=9A=D0=BE=D0=B4=20=D1=80=D0=B0=D0=B7=D0=BD?= =?UTF-8?q?=D0=B5=D1=81=D0=B5=D0=BD=20=D0=BD=D0=B0=20=D1=82=D1=80=D0=B8=20?= =?UTF-8?q?=D1=84=D0=B0=D0=B9=D0=BB=D0=B0,=20=D0=B4=D0=BE=D0=B1=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D1=8B=20=D0=B7=D0=B0=D0=B3=D0=BE=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=D0=BE=D1=87=D0=BD=D1=8B=D0=B5=20=D1=84=D0=B0=D0=B9?= =?UTF-8?q?=D0=BB=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 40 +++++++++++++++++++++++++++ histogram.h | 7 +++++ main.cpp | 76 ++------------------------------------------------- text.cpp | 46 +++++++++++++++++++++++++++++++ text.h | 7 +++++ 5 files changed, 102 insertions(+), 74 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..2c4bfb7 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include "histogram.h" +using namespace std; +static void find_minmax(const 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; + } + } +} +vector make_histogram(const vector& numbers, size_t& bin_count){ + double min, max; + find_minmax(numbers, min, max); + vector bins(bin_count); + double bin_size = (max - min) / bin_count; + size_t number_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; +} diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..227f292 --- /dev/null +++ b/histogram.h @@ -0,0 +1,7 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED +#include + +std::vector make_histogram(const std::vector& numbers, size_t& bin_count); + +#endif // HISTOGRAM_H_INCLUDED diff --git a/main.cpp b/main.cpp index 1e7b47c..11fa617 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,8 @@ #include #include #include +#include "histogram.h" +#include "text.h" using namespace std; struct Input { vector numbers; @@ -19,80 +21,6 @@ input_data(){ cin >> in.bin_count; return in; } -void find_minmax(const 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; - } - } -} -vector make_histogram(const vector& numbers, size_t& bin_count){ - double min, max; - find_minmax(numbers, min, max); - vector bins(bin_count); - double bin_size = (max - min) / bin_count; - size_t number_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(size_t& bin_count, vector& bins){ - vector counts(bin_count); - for (size_t i = 0; i < bin_count; i++){ - for (size_t j = 0; j < bins[i]; j++){ - counts[i]++; - } - } - size_t max_count = counts[0]; - for (size_t i=0; imax_count){ - max_count = counts[i]; - } - } - const size_t screen_width = 80; - const size_t max_asterisk = screen_width - 3 - 1; - vector heights(bin_count); - size_t height; - for (size_t i=0; i 76){ - height = (max_asterisk*(static_cast(counts[i])/max_count)); - } - else { - height = counts[i]; - } - heights[i] = height; - } - for (size_t i = 0; i < bin_count; i++){ - if(bins[i]<100){ - cout << " "; - } - if (bins[i]<10){ - cout << " "; - } - cout << bins[i] << "|"; - for (size_t j = 0; j < heights[i]; j++){ - cout << "*"; - } - cout << "\n"; - } -} int main(){ auto in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..a155626 --- /dev/null +++ b/text.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include "text.h" +using namespace std; +void show_histogram_text(size_t& bin_count, vector& bins){ + vector counts(bin_count); + for (size_t i = 0; i < bin_count; i++){ + for (size_t j = 0; j < bins[i]; j++){ + counts[i]++; + } + } + size_t max_count = counts[0]; + for (size_t i=0; imax_count){ + max_count = counts[i]; + } + } + const size_t screen_width = 80; + const size_t max_asterisk = screen_width - 3 - 1; + vector heights(bin_count); + size_t height; + for (size_t i=0; i 76){ + height = (max_asterisk*(static_cast(counts[i])/max_count)); + } + else { + height = counts[i]; + } + heights[i] = height; + } + for (size_t i = 0; i < bin_count; i++){ + if(bins[i]<100){ + cout << " "; + } + if (bins[i]<10){ + cout << " "; + } + cout << bins[i] << "|"; + for (size_t j = 0; j < heights[i]; j++){ + cout << "*"; + } + cout << "\n"; + } +} diff --git a/text.h b/text.h new file mode 100644 index 0000000..1b67e1b --- /dev/null +++ b/text.h @@ -0,0 +1,7 @@ +#ifndef TEXT_H_INCLUDED +#define TEXT_H_INCLUDED +#include + +void show_histogram_text(size_t& bin_count, std::vector& bins); + +#endif // TEXT_H_INCLUDED