From 69a7b410ddb078b93cd6491bee5308d320c3915b Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 21 May 2024 21:19:02 +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=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 | 134 +++++++++++++++++++++++++++---------------------------- 1 file changed, 65 insertions(+), 69 deletions(-) diff --git a/main.cpp b/main.cpp index e776e8b..1ca3faa 100644 --- a/main.cpp +++ b/main.cpp @@ -3,31 +3,32 @@ using namespace std; -int main() +struct Input { - const size_t SCREEN_WIDTH = 80; - const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; - - int max_count = 0; + vector numbers; + size_t bin_count{}; +}; +Input input_data() +{ + Input in; size_t number_count; - cerr << "Enter number count: "; cin >> number_count; - vector numbers(number_count); + + in.numbers.resize(number_count); for (size_t i = 0; i < number_count; i++) { - cin >> numbers[i]; + cin >> in.numbers[i]; } - size_t bin_count; - cerr << "Enter bin count: "; - cin >> bin_count; - vector bins(bin_count); - - double min = numbers[0]; - double max = numbers[0]; + cin >> in.bin_count; + return in; +} +void find_minmax(const vector& numbers, double& min, double& max) +{ + min = numbers[0]; for (double x : numbers) { if (x < min) @@ -39,10 +40,16 @@ int main() 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; - - for (size_t i = 0; i < number_count; i++) + 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++) @@ -54,7 +61,6 @@ int main() bins[j]++; found = true; } - } if (!found) { @@ -62,82 +68,72 @@ int main() } } - for (size_t i = 0; i < bin_count; i++) + double b; + for (size_t i = 0; i < bin_count-1; i++) { - if (bins[i] > max_count) + if (bins[i] > bins[i+1]) { - max_count = bins[i]; + b = bins[i]; + bins[i] = bins[i + 1]; + bins[i + 1] = b; } } + return bins; +} - if (max_count > MAX_ASTERISK) +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 i = 0; i < bin_count; i++) + for (size_t x : bins) { - size_t height = MAX_ASTERISK * (static_cast(bins[i]) / max_count); - if (bins[i] < 10) - { - cout << " " << bins[i] << "|"; - for (int j = 0; j < height; j++) - { - cout << "*"; - } - cout << endl; - } - else if (bins[i] < 100) + size_t height = MAX_STAR * (static_cast(x) / max_star_search); + if (x < 100) { - cout << " " << bins[i] << "|"; - for (int j = 0; j < height; j++) + cout << " "; + if (x < 10) { - cout << "*"; + cout << " "; } - cout << endl; } - else + cout << x << "|"; + for (size_t i = 0; i < height; i++) { - cout << bins[i] << "|"; - for (int j = 0; j < height; j++) - { - cout << "*"; - } - cout << endl; + cout << "*"; } - + cout << endl; } } else { - for (size_t i = 0; i < bin_count; i++) + for (size_t x : bins) { - if (bins[i] < 10) + if (x < 100) { - cout << " " << bins[i] << "|"; - for (int j = 0; j < bins[i]; j++) + cout << " "; + if (x < 10) { - cout << "*"; + cout << " "; } - cout << endl; } - else if (bins[i] < 100) + cout << x << "|"; + for (size_t i = 0; i < x; i++) { - cout << " " << bins[i] << "|"; - for (int j = 0; j < bins[i]; j++) - { - cout << "*"; - } - cout << endl; + cout << "*"; } - else - { - cout << bins[i] << "|"; - for (int j = 0; j < bins[i]; j++) - { - cout << "*"; - } - cout << endl; - } - + cout << endl; } } +} + +int main() +{ + auto in = input_data(); + auto bins = make_histogram(in.numbers, in.bin_count); + show_histogram_text(bins); return 0; }