commit 0b0a09baf18ef1f39eaeec4b6e8bb7390154ab2d Author: EfimovaLA Date: Sat Jun 1 11:53:09 2024 +0300 code: исходная программа diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..0f991b3 --- /dev/null +++ b/main.cpp @@ -0,0 +1,109 @@ +#include +#include + +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:"; + cin >> numbers_count; + + vector numbers ( numbers_count ); + for (size_t i=0; i < numbers_count; i++){ + cin >> numbers[i]; + } + cin >> bin_count; + + 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++){ + 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; + } + } + } +} + +