diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..534f64c --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#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; + } + if (x > max) + { + max = x; + } + } +} + +vector make_histogram(const vector& numbers, size_t bin_count) +{ + double min_number, max_number; + find_minmax(numbers, min_number, max_number); + vector bins(bin_count); + for (int i = 0; i < bin_count; i++) + { + bins[i] = 0; + } + double bin_size = (max_number - min_number) / 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_number + j * bin_size; + auto hi = min_number + (j + 1) * bin_size; + if ((lo <= numbers[i]) && (numbers[i] < hi)) + { + bins[j]++; + found = true; + } + } + if (!found) + { + bins[bin_count - 1]++; + } + } + return bins; +} \ No newline at end of file diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..1991ac2 --- /dev/null +++ b/histogram.h @@ -0,0 +1,4 @@ +#pragma once +#include + +std::vector make_histogram(const std::vector& numbers, size_t bin_count); \ No newline at end of file diff --git a/lab03.vcxproj b/lab03.vcxproj index 3678d76..097c62d 100644 --- a/lab03.vcxproj +++ b/lab03.vcxproj @@ -19,7 +19,15 @@ + + + + + + false + + 17.0 diff --git a/lab1.cpp b/lab1.cpp index 0694a6d..8db375e 100644 --- a/lab1.cpp +++ b/lab1.cpp @@ -1,6 +1,8 @@ #include #include #include +#include "histogram.h" +#include "text.h" using namespace std; struct Input { vector numbers; @@ -19,100 +21,6 @@ Input 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; - } - if (x > max) - { - max = x; - } - } -} - -vector make_histogram(vector& numbers, size_t bin_count) -{ - double min_number, max_number; - find_minmax(numbers, min_number, max_number); - vector bins(bin_count); - for (int i = 0; i < bin_count; i++) - { - bins[i] = 0; - } - double bin_size = (max_number - min_number) / 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_number + j * bin_size; - auto hi = min_number + (j + 1) * bin_size; - if ((lo <= numbers[i]) && (numbers[i] < hi)) - { - bins[j]++; - found = true; - } - } - if (!found) - { - bins[bin_count - 1]++; - } - } - return bins; -} - -void find_max_capacity(vector& bins, size_t& max_bin_capacity) -{ - max_bin_capacity = bins[0]; - for (size_t x : bins) - { - if (x > max_bin_capacity) - { - max_bin_capacity = x; - } - } -} - -void show_histogram_text(vector& bins, size_t max_asterisk) -{ - size_t max_bin_capacity{}; - find_max_capacity(bins, max_bin_capacity); - size_t height = 0; - for (int i = 0; i < bins.size(); i++) - { - if (bins[i] < 10) - { - cout << " " << bins[i] << "|"; - } - if ((bins[i] < 100) && (bins[i] > 9)) - { - cout << " " << bins[i] << "|"; - } - if (bins[i] >= 100) - { - cout << bins[i] << "|"; - } - if (max_bin_capacity > max_asterisk) - { - height = max_asterisk * (static_cast(bins[i]) / max_bin_capacity); - } - else - { - height = bins[i]; - } - for (int j = 0; j < height; j++) - { - cout << "*"; - } - cout << "\n"; - } -} - int main() { const size_t SCREEN_WIDTH = 80; diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..b14d021 --- /dev/null +++ b/text.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include "text.h" +using namespace std; + +static void find_max_capacity(const vector& bins, size_t& max_bin_capacity) +{ + max_bin_capacity = bins[0]; + for (size_t x : bins) + { + if (x > max_bin_capacity) + { + max_bin_capacity = x; + } + } +} + +void show_histogram_text(const vector& bins, size_t max_asterisk) +{ + size_t max_bin_capacity{}; + find_max_capacity(bins, max_bin_capacity); + size_t height = 0; + for (int i = 0; i < bins.size(); i++) + { + if (bins[i] < 10) + { + cout << " " << bins[i] << "|"; + } + if ((bins[i] < 100) && (bins[i] > 9)) + { + cout << " " << bins[i] << "|"; + } + if (bins[i] >= 100) + { + cout << bins[i] << "|"; + } + if (max_bin_capacity > max_asterisk) + { + height = max_asterisk * (static_cast(bins[i]) / max_bin_capacity); + } + else + { + height = bins[i]; + } + for (int j = 0; j < height; j++) + { + cout << "*"; + } + cout << "\n"; + } +} \ No newline at end of file diff --git a/text.h b/text.h new file mode 100644 index 0000000..e5ab877 --- /dev/null +++ b/text.h @@ -0,0 +1,4 @@ +#pragma once +#include + +void show_histogram_text(const std::vector& bins, std::size_t max_asterisk); \ No newline at end of file