From 68b28203036a7a22b0795772eb6ca68dcd627d82 Mon Sep 17 00:00:00 2001 From: LedovskojMM Date: Sun, 23 Apr 2023 21:02:36 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B3=D1=80=D0=B0=D0=BC?= =?UTF-8?q?=D0=BC=D0=B0=20=D0=B4=D0=BE=205=20=D0=BF=D1=83=D0=BD=D0=BA?= =?UTF-8?q?=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogam.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ histogam.h | 10 ++++++++ lab1.depend | 9 ++++++++ main.cpp | 60 +----------------------------------------------- 4 files changed, 84 insertions(+), 59 deletions(-) create mode 100644 histogam.cpp create mode 100644 histogam.h diff --git a/histogam.cpp b/histogam.cpp new file mode 100644 index 0000000..e3fc6aa --- /dev/null +++ b/histogam.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include "histogam.h" +using namespace std; + +static find_minmax(vector numbers, double& min, double& max) { + min = numbers[0]; + for (auto i = 0; i < numbers.size(); i++) { + if (numbers[i] < min) { + min = numbers[i]; + } + } + + max = numbers[0]; + for (auto i = 0; i < numbers.size(); i++) { + if (numbers[i] > max) { + max = numbers[i]; + } + } + +} + +vector make_histogram(const vector& numbers, size_t bin_count) { + + vector bins(bin_count); + vector binss(bin_count); + + double max, min; + 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]++; + } + } + + int max_count = bins[0]; + for (size_t i = 0; i < bin_count; i++) { + if (bins[i] > max_count) { + max_count = bins[i]; + } + } + + if (max_count > 76) { + + for (size_t i = 0; i < bin_count; i++) { + int count = bins[i]; + size_t height = 76 * (static_cast(count) / max_count); + bins[i] = height; + } + } + return bins; +} diff --git a/histogam.h b/histogam.h new file mode 100644 index 0000000..994a9c4 --- /dev/null +++ b/histogam.h @@ -0,0 +1,10 @@ +#ifndef HISTOGAM_H_INCLUDED +#define HISTOGAM_H_INCLUDED + +#include + +std::vector +make_histogram(const std::vector& numbers, size_t bin_count); + + +#endif // HISTOGAM_H_INCLUDED diff --git a/lab1.depend b/lab1.depend index 10bbee7..ab181f3 100644 --- a/lab1.depend +++ b/lab1.depend @@ -3,3 +3,12 @@ +1682272330 source:c:\users\admin\onedrive\Рабочий стол\lab1\main.cpp + + + + "histogam.h" + +1682271932 c:\users\admin\onedrive\Рабочий стол\lab1\histogam.h + + diff --git a/main.cpp b/main.cpp index fb7714e..7d61636 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,7 @@ #include #include #include - +#include "histogam.h" using namespace std; @@ -27,64 +27,6 @@ input_data() { return in; } -void find_minmax(const vector& numbers, double& min, double& max) { - min = numbers[0]; - for (auto i = 0; i < numbers.size(); i++) { - if (numbers[i] < min) { - min = numbers[i]; - } - } - - max = numbers[0]; - for (auto i = 0; i < numbers.size(); i++) { - if (numbers[i] > max) { - max = numbers[i]; - } - } - -} - -vector make_histogram(const vector& numbers, size_t bin_count) { - - vector bins(bin_count); - vector binss(bin_count); - - double max, min; - 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]++; - } - } - - int max_count = bins[0]; - for (size_t i = 0; i < bin_count; i++) { - if (bins[i] > max_count) { - max_count = bins[i]; - } - } - - if (max_count > 76) { - - for (size_t i = 0; i < bin_count; i++) { - int count = bins[i]; - size_t height = 76 * (static_cast(count) / max_count); - bins[i] = height; - } - } - return bins; -} void show_histogram_text(vector bins, size_t bin_count ) {