diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..bd8cadf --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,35 @@ +#include "histogram.h" +#include +using namespace std; + +static void find_minmax(const vector& numbers, double& min, double& max) { + max = numbers[0]; + min = numbers[1]; + for (double x : numbers) { + if (x < min) min = x; + else if (x > max) max = x; + } +} + +vector make_histogram(vector numbers, size_t bin_count) { + double min, max; + find_minmax(numbers, min, max); + double bin_size = (max - min) / bin_count; + vector 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 = min + j * bin_size; + auto hi = min + (j + 1) * bin_size; + if ((lo <= numbers[i]) && (numbers[i] < hi)) { + bins[j]++; + found = true; + } + } + if (!found) { + bins[bin_count - 1]++; + } + } + return bins; +} diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..1ee7f4d --- /dev/null +++ b/histogram.h @@ -0,0 +1,9 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED + +#include +using namespace std; + +vector make_histogram(vector numbers, size_t bin_count); + +#endif diff --git a/main.cpp b/main.cpp index bc6780d..1457c4c 100644 --- a/main.cpp +++ b/main.cpp @@ -1,123 +1,34 @@ #include #include - +#include "histogram.h" +#include "text.h" using namespace std; struct Input { vector numbers; - size_t bin_count{}; + size_t bin_count; }; -Input input_data(){ - size_t number_count, bin_count; +Input input_data() { + Input in; + size_t number_count; cerr << "Enter number count: "; cin >> number_count; - Input in; - in.numbers.resize(number_count); - for (size_t i = 0; i < number_count; i++) - { - cerr << "Enter Num[" << i << "]: "; + in.numbers.resize(number_count); + cerr << "Enter numbers: "; + for (size_t i = 0; i < number_count; i++) { cin >> in.numbers[i]; } + cerr << "Enter bin count: "; cin >> in.bin_count; return in; } -void find_minmax(const vector &numbers, double &min, double &max) -{ - max = numbers[0]; - min = numbers[1]; - for (double x: numbers) - { - if (x < min) - { - min = x; - } - else if (x > max) - { - max = x; - } - } - return; -} - -vector make_histogram (vector numbers, size_t bin_count) -{ - double min, max; - find_minmax (numbers, min, max); - double bin_size = (max - min) / bin_count; - vector 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 = min + j * bin_size; - auto hi = min + (j + 1) * bin_size; - if ((lo <= numbers[i]) && (numbers[i] < hi)) - { - bins[j]++; - found = true; - } - } - if (!found) - { - bins[bin_count - 1]++; - } - } - return bins; -} - -void show_histogram_text(vector bins, size_t bin_count) -{ - const size_t SCREEN_WIDTH = 80; - const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; - double max_bin; - size_t height; - max_bin = bins[0]; - height = bins[0]; - for (size_t i = 0; i < bin_count; i++) - { - if (bins[i] > max_bin){ - max_bin = bins[i]; - } - } - - bool flag = false; - if ( max_bin > 80) - { - flag = true; - } - for (size_t i = 0; i < bin_count; i++) - { - if (bins[i] < 100) - cout << " "; - if (bins[i] < 10) - cout << " "; - cout << bins[i] << "|"; - if( flag == true) - { - height = MAX_ASTERISK * (static_cast(bins[i]) / max_bin); - } - else - { - height = bins[i]; - } - for (size_t j = 0; j < height; j++) - { - cout << "*"; - } - cout << endl; - } -} - -int main() -{ +int main() { auto in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); show_histogram_text(bins, in.bin_count); return 0; } - diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..9f8573c --- /dev/null +++ b/text.cpp @@ -0,0 +1,37 @@ +#include "text.h" +#include +using namespace std; + +void show_histogram_text(vector bins, size_t bin_count) { + const size_t SCREEN_WIDTH = 80; + const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + double max_bin; + size_t height; + max_bin = bins[0]; + height = bins[0]; + for (size_t i = 0; i < bin_count; i++) { + if (bins[i] > max_bin) { + max_bin = bins[i]; + } + } + + bool flag = false; + if (max_bin > 80) { + flag = true; + } + for (size_t i = 0; i < bin_count; i++) { + if (bins[i] < 100) cout << " "; + if (bins[i] < 10) cout << " "; + cout << bins[i] << "|"; + if (flag == true) { + height = MAX_ASTERISK * (static_cast(bins[i]) / max_bin); + } + else { + height = bins[i]; + } + for (size_t j = 0; j < height; j++) { + cout << "*"; + } + cout << endl; + } +} diff --git a/text.h b/text.h new file mode 100644 index 0000000..f5c2730 --- /dev/null +++ b/text.h @@ -0,0 +1,9 @@ +#ifndef TEXT_H_INCLUDED +#define TEXT_H_INCLUDED + +#include +using namespace std; + +void show_histogram_text(vector bins, size_t bin_count); + +#endif