From 9e04c05397a5b27da5e772ca8845cf9624c8cf72 Mon Sep 17 00:00:00 2001 From: ChirkaAR <ChirkaAR@mpei.ru> Date: Sun, 27 Apr 2025 00:42:37 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=83=D0=BD=D0=BA=D1=82=203:=20=D0=A0?= =?UTF-8?q?=D0=B0=D0=B7=D0=B4=D0=B5=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=B3=D1=80=D0=B0=D0=BC=D0=BC=D1=8B=20=D0=BD=D0=B0?= =?UTF-8?q?=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 59 ++++++++++++++++++++++++++++ histogram.h | 9 +++++ main.cpp | 107 +------------------------------------------------- text.cpp | 54 +++++++++++++++++++++++++ text.h | 6 +++ 5 files changed, 130 insertions(+), 105 deletions(-) create mode 100644 histogram.cpp create mode 100644 histogram.h create mode 100644 text.cpp create mode 100644 text.h diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..9deefeb --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,59 @@ +#include "histogram.h" +#include <iostream> +#include <vector> +using namespace std; + +static void find_minmax(const vector<double>& numbers, double& minN, double& maxN) +{ + minN = numbers[0]; + maxN = numbers[0]; + + for (double x: numbers) + { + if (minN > x) + { + minN = x; + } + if (maxN < x) + { + maxN = x; + } + } +} + +vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) +{ + double minN, maxN; + find_minmax( numbers, minN, maxN); + + vector <size_t> bins(bin_count); + double diff = (maxN - minN) / bin_count; + size_t max_count = 0; + 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 = minN + j * diff; + auto hi = minN + (j + 1) * diff; + if ((lo <= numbers[i]) && (hi > numbers[i])) + { + bins[j]++; + if (bins[j] > max_count) + { + max_count = bins[j]; + } + found = true; + } + } + if(!found) + { + bins[bin_count - 1]++; + if (bins[bin_count - 1] > max_count) + { + max_count = bins[bin_count - 1]; + } + } + } + return bins; +} diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..0d8dc8c --- /dev/null +++ b/histogram.h @@ -0,0 +1,9 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED + +#include <vector> + +std::vector<size_t> +make_histogram(const std::vector<double>& numbers, size_t bin_count); + +#endif // HISTOGRAM_H_INCLUDED diff --git a/main.cpp b/main.cpp index 604f7d8..33dbb41 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,7 @@ #include <iostream> #include <vector> +#include "histogram.h" +#include "text.h" using namespace std; @@ -28,111 +30,6 @@ input_data() return in; } -void find_minmax( const vector<double>& numbers, double& minN, double& maxN) -{ - minN = numbers[0]; - maxN = numbers[0]; - - for (double x: numbers) - { - if (minN > x) - { - minN = x; - } - if (maxN < x) - { - maxN = x; - } - } -} - -vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) -{ - double minN, maxN; - find_minmax( numbers, minN, maxN); - - vector <size_t> bins(bin_count); - double diff = (maxN - minN) / bin_count; - size_t max_count = 0; - 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 = minN + j * diff; - auto hi = minN + (j + 1) * diff; - if ((lo <= numbers[i]) && (hi > numbers[i])) - { - bins[j]++; - if (bins[j] > max_count) - { - max_count = bins[j]; - } - found = true; - } - } - if(!found) - { - bins[bin_count - 1]++; - if (bins[bin_count - 1] > max_count) - { - max_count = bins[bin_count - 1]; - } - } - } - return bins; -} - -void show_histogram_text(const vector <size_t>& bins,size_t bin_count, size_t max_count) -{ - const size_t SCREEN_WIDTH = 80; - const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; - - bool scaling = false; - - if (max_count > MAX_ASTERISK) - { - scaling = true; - } - - for (size_t i = 0; i < bin_count; i++) - { - if (bins[i] < 10) - { - cout << " "; - } - else if (bins[i] < 100) - { - cout << " "; - } - else - { - cout << ""; - } - cout << bins[i] << '|'; - - size_t number_of_stars = bins[i]; - - if (scaling) - { - if (bins[i] == max_count) - { - number_of_stars = MAX_ASTERISK * 1.0; - } - else - { - number_of_stars = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count); - } - } - - for (size_t j = 0; j < number_of_stars; j++) - { - cout << '*'; - } - cout << endl; - } -} - int main() { size_t max_count; diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..6b55eef --- /dev/null +++ b/text.cpp @@ -0,0 +1,54 @@ +#include "text.h" +#include <iostream> +#include <vector> +using namespace std; + +void show_histogram_text(const vector <size_t>& bins,size_t bin_count, size_t max_count) +{ + const size_t SCREEN_WIDTH = 80; + const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + + bool scaling = false; + + if (max_count > MAX_ASTERISK) + { + scaling = true; + } + + for (size_t i = 0; i < bin_count; i++) + { + if (bins[i] < 10) + { + cout << " "; + } + else if (bins[i] < 100) + { + cout << " "; + } + else + { + cout << ""; + } + cout << bins[i] << '|'; + + size_t number_of_stars = bins[i]; + + if (scaling) + { + if (bins[i] == max_count) + { + number_of_stars = MAX_ASTERISK * 1.0; + } + else + { + number_of_stars = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count); + } + } + + for (size_t j = 0; j < number_of_stars; j++) + { + cout << '*'; + } + cout << endl; + } +} diff --git a/text.h b/text.h new file mode 100644 index 0000000..a619492 --- /dev/null +++ b/text.h @@ -0,0 +1,6 @@ +#ifndef TEXT_H_INCLUDED +#define TEXT_H_INCLUDED +#include <vector> +void show_histogram_text(const std::vector <size_t>& bins,size_t bin_count, size_t max_count); + +#endif // TEXT_H_INCLUDED