From 1a8a3225288bad854b1cd1617b80fb153f86cb80 Mon Sep 17 00:00:00 2001 From: EfimovaLA <YefimovaLA@mpei.ru> Date: Sat, 1 Jun 2024 20:50:58 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82=D1=83?= =?UTF-8?q?=D1=80=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 85 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 28 deletions(-) diff --git a/main.cpp b/main.cpp index 0f991b3..ab3376a 100644 --- a/main.cpp +++ b/main.cpp @@ -3,57 +3,86 @@ using namespace std; -int main() -{ - size_t bin_count, numbers_count; - const size_t SCREEN_WIDTH = 80; - const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; +const size_t SCREEN_WIDTH = 80; +const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + +struct Input { + vector<double> numbers; + size_t bin_count{}; +}; +Input input_data() +{ + size_t number_count; cerr << "Enter number count:"; - cin >> numbers_count; + cin >> number_count; - vector<double> numbers ( numbers_count ); - for (size_t i=0; i < numbers_count; i++){ - cin >> numbers[i]; - } - cin >> bin_count; +Input in; - vector<size_t> bins ( bin_count ); - double minx = numbers[0]; - double maxx = numbers[0]; + in.numbers.resize(number_count); + cerr << "Enter numbers: "; + for(size_t i = 0; i < number_count; i++) + cin >> in.numbers[i]; + + cerr << "Enter number of bins: "; + cin >> in.bin_count; + + return in; + +} +void find_minmax(vector<double>& numbers, double& min, double& max) { + min = numbers[0]; + max = numbers[0]; + // (����� ��� ������ �������� ����������) for (double x : numbers){ - if (x < minx){ - minx = x; - } else if (x > maxx){ - maxx = x; + if (x < min){ + min = x; + } else if (x > max){ + max = x; } } +} + +vector<size_t> make_histogram(vector<double> &numbers, size_t bin_count) { + double min = numbers[0]; + double max = numbers[0]; + find_minmax(numbers, min, max); - double bin_size = (maxx - minx) / bin_count; - for (size_t i=0; i < numbers_count; i++ ){ + double bin_size = (max - min) / bin_count; + vector<size_t> bins (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 = minx + j * bin_size; - auto hi = minx + ( j + 1 ) * bin_size; - if (lo <= numbers[i] && ( numbers[i] < hi )){ + 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 ){ + if (!found){ bins[bin_count-1]++; } } + return bins; +} + +int main() +{ + Input in = input_data(); + auto bins = make_histogram(in.numbers, in.bin_count); size_t maxbin = bins[0]; - for (size_t i=1; i < bin_count; i++){ + for (size_t i=1; i < in.bin_count; i++){ if (maxbin < bins[i]){ maxbin = bins[i]; } } if (maxbin <= MAX_ASTERISK){ - for (size_t i=0; i < bin_count; i++){ + for (size_t i=0; i < in.bin_count; i++){ if ((bins[i] < 1000)&&(bins[i] > 99)){ cout << bins[i] << "|"; for ( size_t j=0; j < bins[i]; j++ ){ @@ -77,7 +106,7 @@ int main() } } } else { - for (size_t i=0; i < bin_count; i++){ + for (size_t i=0; i < in.bin_count; i++){ size_t heightG= MAX_ASTERISK * (static_cast<double>(bins[i]) / maxbin); if ((bins[i] < 1000)&&(bins[i] > 99)){