From 1795647c6547218a2472eaf96af2d60886e0c6a5 Mon Sep 17 00:00:00 2001 From: Zakhar Date: Wed, 25 Sep 2024 17:26:56 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=BE=20=D1=80=D0=B0=D0=B7=D0=B1=D0=B8?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D0=B9=20=D0=BD=D0=B0=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 98 ++++++-------------------------------------------------- 1 file changed, 9 insertions(+), 89 deletions(-) diff --git a/main.cpp b/main.cpp index c8529f0..1b42dfd 100644 --- a/main.cpp +++ b/main.cpp @@ -1,9 +1,9 @@ #include #include - +#include "histogram.h" +#include "text.h" using namespace std; -const size_t SCREEN_WIDTH = 80; -const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + struct Input { vector numbers; @@ -33,95 +33,15 @@ Input input_data() { return in; } -void find_minmax(const vector &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; - } - } -} + + int main(){ Input in = input_data(); - vector bins ( in.bin_count ); - double min = in.numbers[0]; - double max = in.numbers[0]; - - find_minmax(in.numbers, min, max); - - - double bin_size = ( max - min ) / in.bin_count; - for (size_t i=0; i < in.numbers.size(); i++ ){ - bool found = false; - for (size_t j = 0; ( j < in.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 )){ - bins[j]++; - found = true; - } - } - if ( !found ){ - bins[in.bin_count-1]++; - } - } - // - size_t maxbin = bins[0]; - 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 < in.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 < in.bin_count; i++){ - 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; - } - } - } + auto bins = make_histogram(in.numbers, in.bin_count); + + show_histogram_text(bins); + }