diff --git a/main.cpp b/main.cpp index f3ee515..892980d 100644 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,11 @@ #include #include #include +#include "histogram.h" using namespace std; -struct -Input +struct Input { vector numbers; size_t bin_count{}; @@ -16,87 +16,25 @@ input_data() { size_t number_count; + cerr << "vvedite col-vo numbers:"; cin >> number_count; Input in; in.numbers.resize(number_count); for (size_t i = 0; i < number_count; i++) { + cerr << "N[" << i << "]="; cin >> in.numbers[i]; } + cerr << "vvedite col-vo count:"; cin >> in.bin_count; return in; } -void -find_minmax(const vector& numbers, double& min, double& max) -{ - max = numbers[0]; - min = numbers[0]; - for(size_t i = 0; i < numbers.size(); i++) - { - if (numbers[i] > max) max = numbers[i]; - if (numbers[i] < min) min = numbers[i]; - } -} - -void -make_histogram(vector& bins, Input in) -{ - double min, max; - find_minmax(in.numbers, min, max); - size_t cor_size = (max - min)/in.bin_count; - - for (size_t i = 0; i < in.numbers.size(); i++) - { - bool flag = false; - for (size_t j = 0; (j < in.bin_count) && !flag; j++) - { - auto L = min + j*cor_size; - auto H = min + (j+1)*cor_size; - if ((L <= in.numbers[i]) && (in.numbers[i] <= H)) - { - flag = true; - bins[j]++; - } - } - if (!flag) bins[in.bin_count-1]++; - } -} - -void -show_histogram_text(vector bins, Input in) -{ - const size_t MAX_ASTERISK = 76; - int maxb = bins[0]; - size_t H; - - for (size_t j = 1; j < in.bin_count; j++) - { - if (bins[j] > maxb) maxb = bins[j]; - } - - for (size_t j = 0; j < in.bin_count; j++) - { - if (maxb > MAX_ASTERISK) - { - H = MAX_ASTERISK * (static_cast(bins[j]) / maxb); - } - else H = bins[j]; - if (bins[j] < 100) cout << " "; - if (bins[j] < 10) cout << " "; - cout << bins[j] << "|"; - for (size_t i = 0; i < H; i++) cout << "*"; - cout << endl; - } -} - int main() { auto in = input_data(); - vector bins; - bins.resize(in.bin_count); - make_histogram(bins, in); - show_histogram_text(bins, in); + auto bins = make_histogram(in.numbers, in.bin_count); + show_histogram_text(bins, in.numbers, in.bin_count); }