diff --git a/hist_proc.cpp b/hist_proc.cpp new file mode 100644 index 0000000..6f825d9 --- /dev/null +++ b/hist_proc.cpp @@ -0,0 +1,18 @@ +#include +#include +#include "hist_proc.h" +using namespace std; +vector make_histogram_proc(const vector numbers, size_t bin_count, vector bins){ + vector procent(bin_count); + double sum = 0; + for (size_t i = 0; i < bin_count; i++){ + sum = sum + bins[i]; + } + double ost = 100; + for (size_t i = 0; i< bin_count-1; i++){ + procent[i]=(bins[i]/sum)*100; + ost = ost - procent[i]; + } + procent[bin_count-1]=ost; + return procent; +} diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..7a6778a --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,41 @@ +#include +#include +#include "histogram.h" +using namespace std; + +static void +find_minmax(const vector& numbers, double& min, double& max) { + min = numbers[0]; + for (double x : numbers) { + if (x < min) { + min = x; + } + else if (x > max) { + max = x; + } + } +} + +vector make_histogram(const vector numbers, size_t bin_count){ + vector bins(bin_count); + double max, min = 0; + find_minmax(numbers, min, max); + double bin_size = (max - min) / 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/main.cpp b/main.cpp index a373d1b..c2ead34 100644 --- a/main.cpp +++ b/main.cpp @@ -1,8 +1,11 @@ #include #include +#include +#include "histogram.h" +#include + + -const size_t SCREEN_WIDTH = 80; -const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; using namespace std; @@ -14,6 +17,7 @@ struct Input { }; + Input input_data() { size_t number_count; @@ -30,115 +34,13 @@ cin >> in.bin_count; return in; } -void -find_minmax(const vector& numbers, double& min, double& max) { - min = numbers[0]; - for (double x : numbers) { - if (x < min) { - min = x; - } - else if (x > max) { - max = x; - } - } -} - -vector make_histogram(const vector numbers, size_t bin_count){ - vector bins(bin_count); - double max, min = 0; - find_minmax(numbers, min, max); - double bin_size = (max - min) / 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; -} - -vector make_histogram_proc(const vector numbers, size_t bin_count, vector bins){ - vector procent(bin_count); - double sum = 0; - for (size_t i = 0; i < bin_count; i++){ - sum = sum + bins[i]; - } - double ost = 100; - for (size_t i = 0; i< bin_count-1; i++){ - procent[i]=(bins[i]/sum)*100; - ost = ost - procent[i]; - } - procent[bin_count-1]=ost; - return procent; -} - -void show_histogram_text(vector bins, size_t bin_count, vector procent){ - - size_t max_count = 0; - for (size_t x: bins) { - if (x > max_count) { - max_count = x; - } - } - - if (max_count > MAX_ASTERISK) { - for (size_t i = 0; i < bin_count ; i++) { - size_t height = MAX_ASTERISK * (static_cast(bins[i]) / max_count); - if (bins[i] < 10) { - cout << " " << procent[i] << "%|"; - } - else if (bins[i] >= 100) { - cout << procent[i] << "%|"; - } - - else { - cout << " " << procent[i] << "%|"; - } - - for (size_t i = 0; i < height; i++) { - cout << "*"; - } - cout << "\n"; - } - } - - else { - for (size_t i = 0; i < bin_count; i++) { - - if (procent[i] < 10) { - cout << " " << procent[i] << "%|"; - } - else if (procent[i] < 100){ - cout << " " << procent[i] << "%|"; - } - else if (procent[i] = 100){ - cout << procent[i] << "%|"; - } - - for (size_t r = 0; r < bins[i]; r++) - cout << "*"; - cout << "\n"; - } - - } - -} - int main() { auto in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); auto procent = make_histogram_proc(in.numbers, in.bin_count, bins); - show_histogram_text(bins, in.bin_count, procent); + show_histogram(in.bin_count, bins, procent); + + return 0; } diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..5531212 --- /dev/null +++ b/text.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +using namespace std; + +const size_t SCREEN_WIDTH = 80; +const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + +void +show_histogram(size_t bin_count, std::vector bins, std::vector procent){ + +size_t max_count = 0; + for (size_t x: bins) { + if (x > max_count) { + max_count = x; + } + } + + if (max_count > MAX_ASTERISK) { + for (size_t i = 0; i < bin_count ; i++) { + size_t height = MAX_ASTERISK * (static_cast(bins[i]) / max_count); + if (bins[i] < 10) { + cout << " " << procent[i] << "%|"; + } + else if (bins[i] >= 100) { + cout << procent[i] << "%|"; + } + + else { + cout << " " << procent[i] << "%|"; + } + + for (size_t i = 0; i < height; i++) { + cout << "*"; + } + cout << "\n"; + } + } + + else { + for (size_t i = 0; i < bin_count; i++) { + + if (procent[i] < 10) { + cout << " " << procent[i] << "%|"; + } + else if (procent[i] < 100){ + cout << " " << procent[i] << "%|"; + } + else if (procent[i] = 100){ + cout << procent[i] << "%|"; + } + + for (size_t r = 0; r < bins[i]; r++) + cout << "*"; + cout << "\n"; + } + + } + +}