From 32c0cacf2448668070a63b9c805990709ff11a43 Mon Sep 17 00:00:00 2001 From: StepanishchevVR Date: Tue, 7 May 2024 16:53:25 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=BF=D1=80=D0=BE=D0=B3=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BC=D0=B0=20=D1=80=D0=B0=D0=B7=D0=B1=D0=B8=D1=82=D0=B0?= =?UTF-8?q?=20=D0=BD=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/main.cpp b/main.cpp index 24204b9..b8cef5c 100644 --- a/main.cpp +++ b/main.cpp @@ -1,8 +1,5 @@ #include #include - -const size_t SCREEN_WIDTH = 80; -const size_t MAX_STAR = SCREEN_WIDTH - 3 - 1; using namespace std; struct Input @@ -14,7 +11,7 @@ struct Input Input input_data() { Input in; - size_t number_count, bin_count; + size_t number_count; cin >> number_count; in.numbers.resize(number_count); @@ -44,24 +41,22 @@ void find_minmax(const vector& numbers, double& min, double& max) } } -int main() +vector make_histogram(const vector &numbers, size_t bin_count) { - Input in = input_data(); - - vectorbins(in.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); - double min, max; - find_minmax(in.numbers, min, max); - - double bin_size = (max - min) / in.bin_count; - for (size_t i = 0; i < in.numbers.size(); i++) + for (size_t i = 0; i < numbers.size(); i++) { bool found = false; - for (size_t j = 0; (j < in.bin_count - 1) && !found; j++) + 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 <= in.numbers[i]) && (in.numbers[i] < hi)) + if ((lo <= numbers[i]) && (numbers[i] < hi)) { bins[j]++; found = true; @@ -69,12 +64,11 @@ int main() } if (!found) { - bins[in.bin_count - 1]++; + bins[bin_count - 1]++; } } - double b; - for (size_t i = 0; i < in.bin_count-1; i++) + for (size_t i = 0; i < bin_count-1; i++) { if (bins[i] > bins[i+1]) { @@ -83,8 +77,15 @@ int main() 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]; - size_t max_star_search = bins[-1]; // поиск наибольшего кол-ва из корзины if (max_star_search > MAX_STAR) { for (size_t x : bins) @@ -126,5 +127,12 @@ int main() cout << endl; } } +} + +int main() +{ + auto in = input_data(); + auto bins = make_histogram(in.numbers, in.bin_count); + show_histogram_text(bins); return 0; }