From 0bac1c131701b9d2cfd92b72a8e9843683a2bb5d Mon Sep 17 00:00:00 2001 From: VasilevIN Date: Sat, 4 May 2024 23:54:23 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD?= =?UTF-8?q?=D1=8B=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 | 136 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 85 insertions(+), 51 deletions(-) diff --git a/main.cpp b/main.cpp index 5727fac..3631058 100644 --- a/main.cpp +++ b/main.cpp @@ -3,77 +3,110 @@ #include using namespace std; -int main() -{ - size_t SCREEN_WIDTH; +struct Input { + vector numbers; + size_t bin_count{}; + size_t SCREEN_WIDTH{}; + size_t number_count{}; + +}; +Input +input_data() { - size_t number_count; - cerr << "Enter number count: "; - cin >> number_count; +Input in; +cerr << "Enter number count: "; +cin >> in.number_count; +in.numbers.resize(in.number_count); + for (size_t i = 0; i < in.number_count; i++) { + cin >> in.numbers[i]; + } +cerr<<"Enter bin count:"; +cin >> in.bin_count; - while (true) { + + while (true) { cerr << "Screen width: "; - cin >> SCREEN_WIDTH; - if (SCREEN_WIDTH < 7) { + cin >> in.SCREEN_WIDTH; + if (in.SCREEN_WIDTH < 7) { cerr << "<7"; cerr << endl; continue; } - if (SCREEN_WIDTH > 80) { + if (in.SCREEN_WIDTH > 80) { cerr << ">80"; cerr << endl; continue; } - if (SCREEN_WIDTH < (number_count / 3)){ + if (in.SCREEN_WIDTH < (in.number_count / 3)){ cerr << "& 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, size_t bin_count) { + vector bins(bin_count); + + double min = 0, max = 0; + find_minmax(numbers, min, max); + + double bin_size = (max - min) / bin_count; + for (int i = 0; i < numbers.size(); i++) { + bool found = false; + for (int 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; +} + +int main() +{ + auto in = input_data(); + + double min, max; + + find_minmax(in.numbers, min, max); + + + + const size_t MAX_ASTERISK = in.SCREEN_WIDTH - 3 - 1; + + + +auto bins = make_histogram(in.numbers, in.bin_count); + + - vector numbers(number_count); - cerr << "Enter numbers: "; - for (int i = 0; i < number_count; i++) - { - cin >> numbers[i]; - } - double min = numbers[0]; - double max = numbers[0]; - for (double x : numbers) - { - if (x < min) - min = x; - else if (x > max) - max = x; - } - size_t bin_count; - cerr<<"Enter bin count:"; - cin >> bin_count; - vector bins(bin_count); - double bin_size = (max - min) / bin_count; - for (size_t i = 0; i < number_count; i++) - { - bool found = false; - for (size_t j = 0; (j < bin_count - 1) && !found; j++) - { - auto low = min + j * bin_size; - auto high = min + (j + 1) * bin_size; - if ((low <= numbers[i]) && (numbers[i] < high)) - { - bins[j]++; - found = true; - } - } - if (!found) - { - bins[bin_count - 1]++; - } - } double mxbins = bins[0]; @@ -88,7 +121,7 @@ int main() k = MAX_ASTERISK / mxbins; else k = 1; - for (size_t i = 0; i < bin_count; i++) + for (size_t i = 0; i < in.bin_count; i++) { if (bins[i] < 10) { cout << " " << bins[i] << "|"; @@ -108,3 +141,4 @@ int main() cout << endl; } } +