diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..f66701b --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,44 @@ +#include "histogram.h" + +using namespace std; + +static 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; + } + } +} + +vector +make_histogram(const vector& numbers, size_t bin_count){ + size_t number_count = numbers.size(); + vector bins(bin_count); + double min, max; + find_minmax(numbers, min, max); + + double bin_size = (max - min) / bin_count; + + for (size_t i = 0; i < number_count; 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..a86c9a3 --- /dev/null +++ b/histogram.h @@ -0,0 +1,5 @@ +#pragma once +#include + +std::vector +make_histogram(const std::vector& numbers, size_t bin_count); diff --git a/lab01.cbp b/lab01.cbp index fb64a0d..81cae27 100644 --- a/lab01.cbp +++ b/lab01.cbp @@ -33,6 +33,10 @@ + + + diff --git a/main.cpp b/main.cpp index 92b7230..1ecb83b 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,8 @@ #include #include +#include "histogram.h" +#include "text.h" + using namespace std; struct Input { @@ -23,97 +26,6 @@ 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; - } - } -} - -vector -make_histogram(const vector& numbers, size_t bin_count){ - size_t number_count = numbers.size(); - vector bins(bin_count); - double min, max; - find_minmax(numbers, min, max); - - double bin_size = (max - min) / bin_count; - - for (size_t i = 0; i < number_count; 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(const vector& bins){ - const size_t SCREEN_WIDTH = 80; - const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; - - auto bin_count = bins.size(); - - size_t max_count = 0; - for (size_t i = 0; i < bin_count; i++){ - size_t Count = bins[i]; - if (max_count < Count){ - max_count = Count; - } - } - if (max_count <= MAX_ASTERISK){ - for (size_t i = 0; i < bin_count; i++){ - size_t Count = bins[i]; - if (Count < 100){ - cout << " "; - } - if (Count < 10){ - cout << " "; - } - cout << Count << "|"; - for (size_t j = 0; j < Count; j++){ - cout << "*"; - } - cout << "\n"; - } - } - else{ - for (size_t i = 0; i < bin_count; i++){ - size_t Count = bins[i]; - size_t height = 76 * (static_cast(Count) / max_count); - if (Count < 100){ - cout << " "; - } - if (Count < 10){ - cout << " "; - } - cout << Count << "|"; - for (size_t j = 0; j < height; j++){ - cout << "*"; - } - cout << "\n"; - } - } -} - - int main() { auto in = input_data(); diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..76b4785 --- /dev/null +++ b/text.cpp @@ -0,0 +1,53 @@ +#include "text.h" +#include + +using namespace std; + +void +show_histogram_text(const vector& bins){ + const size_t SCREEN_WIDTH = 80; + const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + + auto bin_count = bins.size(); + + size_t max_count = 0; + for (size_t i = 0; i < bin_count; i++){ + size_t Count = bins[i]; + if (max_count < Count){ + max_count = Count; + } + } + if (max_count <= MAX_ASTERISK){ + for (size_t i = 0; i < bin_count; i++){ + size_t Count = bins[i]; + if (Count < 100){ + cout << " "; + } + if (Count < 10){ + cout << " "; + } + cout << Count << "|"; + for (size_t j = 0; j < Count; j++){ + cout << "*"; + } + cout << "\n"; + } + } + else{ + for (size_t i = 0; i < bin_count; i++){ + size_t Count = bins[i]; + size_t height = 76 * (static_cast(Count) / max_count); + if (Count < 100){ + cout << " "; + } + if (Count < 10){ + cout << " "; + } + cout << Count << "|"; + for (size_t j = 0; j < height; j++){ + cout << "*"; + } + cout << "\n"; + } + } +} diff --git a/text.h b/text.h new file mode 100644 index 0000000..7906f83 --- /dev/null +++ b/text.h @@ -0,0 +1,5 @@ +#pragma once +#include + +void +show_histogram_text(const std::vector& bins);