From 214a060068f0fa1774ef482d186a5e912bf499b9 Mon Sep 17 00:00:00 2001 From: GoloshchapovDY Date: Wed, 24 Apr 2024 14:27:54 +0300 Subject: [PATCH] =?UTF-8?q?code:=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 145 ++++++++++++++++--------------------------------------- 1 file changed, 42 insertions(+), 103 deletions(-) diff --git a/main.cpp b/main.cpp index 5052807..7f736cf 100644 --- a/main.cpp +++ b/main.cpp @@ -1,109 +1,48 @@ #include #include - +#include "histogram.h" +#include "text.h" 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; - cerr << "Enter number count and bin count:"; - cin >> numbers_count >> bin_count; - vector numbers ( numbers_count ); - for (size_t i=0; i < numbers_count; i++){ - cin >> numbers[i]; - } - vector bins ( bin_count ); - double minx = numbers[0]; - double maxx = numbers[0]; - for ( double x : numbers ){ - if ( x < minx ){ - minx = x; - } else if ( x > maxx ){ - maxx = x; - } - } - double bin_size = ( maxx - minx ) / bin_count; - for (size_t i=0; i < numbers_count; 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 )){ - bins[j]++; - found = true; - } - } - if ( !found ){ - bins[bin_count-1]++; - } - } - size_t maxbin = bins[0]; - for (size_t i=1; i < bin_count; i++){ - if (maxbin < bins[i]){ - maxbin = bins[i]; - } - } - if (maxbin <=MAX_ASTERISK){ - for ( size_t i=0; i < bin_count; i++ ){ - if ((bins[i] < 1000)&&(bins[i] > 99)){ - cout << bins[i] << "|"; - for ( size_t j=0; j < bins[i]; j++ ){ - cout << "*"; - } - cout << endl; - } else if ((bins[i] < 100)&&(bins[i]>9)) { - cout << " " << bins[i] << "|"; - for ( size_t j=0; j < bins[i]; j++ ){ - cout << "*"; - } - cout << endl; - } else if ( bins[i] < 10 ){ - cout << " " << bins[i]<< "|"; - for ( size_t j=0; j < bins[i]; j++ ){ - cout << "*"; - } - cout << endl; - } - } - } else { - for (size_t i=0; i < bin_count; i++){ - if (bins[i] == maxbin){ - if ((bins[i] < 1000)&&(bins[i] > 99)){ - cout << bins[i] << "|"; - for ( size_t j=0; j < MAX_ASTERISK; j++ ){ - cout << "*"; - } - cout << endl; - } else if ((bins[i] < 100)&&(bins[i]>=MAX_ASTERISK)) { - cout << " " << bins[i] << "|"; - for ( size_t j=0; j < bins[i]; j++ ){ - cout << "*"; - } - cout << endl; - } - } else { - size_t heightG= MAX_ASTERISK * (static_cast(bins[i]) / maxbin); - if ((bins[i] < 1000)&&(bins[i] > 99)){ - cout << bins[i] << "|"; - for ( size_t j=0; j < heightG; j++ ){ - cout << "*"; - } - cout << endl; - } else if ((bins[i] < 100)&&(bins[i]>9)) { - cout << " " << bins[i] << "|"; - for ( size_t j=0; j < heightG; j++ ){ - cout << "*"; - } - cout << endl; - } else if ( bins[i] < 10 ){ - cout << " " << bins[i]<< "|"; - for ( size_t j=0; j < heightG; j++ ){ - cout << "*"; - } - cout << endl; - } - } - } + +struct Input { + vector numbers; + size_t bin_count{}; +}; + +Input input_data() { + + size_t number_count; + + cerr << "Enter number count: "; + cin >> number_count; + + Input in; + + cerr << "Enter number of bins: "; + cin >> in.bin_count; + + in.numbers.resize(number_count); + + cerr << "Enter numbers: "; + for (size_t i = 0; i < number_count; i++) + { + cin >> in.numbers[i]; } + + return in; } + + + + +int main(){ + + Input in = input_data(); + + auto bins = make_histogram(in.numbers, in.bin_count); + + show_histogram_text(bins); + + + }