From 4ed40a534562b20e6033850a534e5ab2325a1fa4 Mon Sep 17 00:00:00 2001 From: PodlovchenkoDD Date: Sun, 27 Apr 2025 20:15:54 +0300 Subject: [PATCH] =?UTF-8?q?3=20=D0=A0=D0=B0=D0=B7=D0=B4=D0=B5=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BF=D1=80=D0=BE=D0=B3=D1=80=D0=B0=D0=BC?= =?UTF-8?q?=D0=BC=D1=8B=20=D0=BD=D0=B0=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 --- TWOPO.cpp | 63 +++++++------------------------------------- histogram.cpp | 36 +++++++++++++++++++++++++ histogram.h | 10 +++++++ histogram_internal.h | 10 +++++++ text.cpp | 16 +++++++++++ text.h | 8 ++++++ 6 files changed, 90 insertions(+), 53 deletions(-) create mode 100644 histogram.cpp create mode 100644 histogram.h create mode 100644 histogram_internal.h create mode 100644 text.cpp create mode 100644 text.h diff --git a/TWOPO.cpp b/TWOPO.cpp index f0650f1..9ea8754 100644 --- a/TWOPO.cpp +++ b/TWOPO.cpp @@ -1,5 +1,9 @@ #include #include +#include "histogram.h" +#include "text.h" +#include "histogram_internal.h" + using namespace std; @@ -14,65 +18,18 @@ Input input_data() { cout << "Введите количество оценок: "; cin >> number_count; - Input in; // Экземпляр структуры Input - in.numbers.resize(number_count); // Изменяем размер вектора + Input in; + in.numbers.resize(number_count); cout << "Введите оценки:\n"; for (size_t i = 0; i < number_count; i++) { - cin >> in.numbers[i]; // Вводим элементы вектора + cin >> in.numbers[i]; } cout << "Введите количество корзин: "; - cin >> in.bin_count; // Вводим количество корзин - - 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(const vector& numbers, size_t bin_count) { - double min, max; - find_minmax(numbers, min, max); + cin >> in.bin_count; - double bin_size = (max - min) / bin_count; - vector bins(bin_count, 0); - - for (double number : numbers) { - size_t bin_index = static_cast((number - min) / bin_size); - if (bin_index >= bin_count) { - bin_index = bin_count - 1; - } - bins[bin_index]++; - } - - return bins; -} - -//вывод -void show_histogram_text(const vector& bins, double min, double bin_size) { - for (size_t i = 0; i < bins.size(); i++) { - double lo = min + i * bin_size; - double up = min + (i + 1) * bin_size; - cout << bins[i] << " | "; - - for (size_t j = 0; j < bins[i]; j++) { - cout << "*"; - } - cout << endl; - } + return in; } int main() { @@ -86,7 +43,7 @@ int main() { double bin_size = (max - min) / in.bin_count; - show_histogram_text(bins, min, bin_size); + show_histogram_text(bins, bin_size); return 0; } diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..4920da2 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,36 @@ +#include +#include +#include "histogram.h" +using namespace std; + +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, max; + find_minmax(numbers, min, max); + + double bin_size = (max - min) / bin_count; + vector bins(bin_count, 0); + + for (double number : numbers) { + size_t bin_index = static_cast((number - min) / bin_size); + if (bin_index >= bin_count) { + bin_index = bin_count - 1; + } + bins[bin_index]++; + } + + return bins; +} diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..c66f9e7 --- /dev/null +++ b/histogram.h @@ -0,0 +1,10 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED + +#include + +std::vector +make_histogram(const std::vector& numbers, size_t bin_count); + + +#endif // HISTOGRAM_H_INCLUDED diff --git a/histogram_internal.h b/histogram_internal.h new file mode 100644 index 0000000..ee7778f --- /dev/null +++ b/histogram_internal.h @@ -0,0 +1,10 @@ +#ifndef HISTOGRAM_INTERNAL_H_INCLUDED +#define HISTOGRAM_INTERNAL_H_INCLUDED + +#include + +std::vector +find_minmax(const std::vector& numbers, double& min, double& max); + + +#endif // HISTOGRAM_INTERNAL_H_INCLUDED diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..cde5b82 --- /dev/null +++ b/text.cpp @@ -0,0 +1,16 @@ +#include +#include +#include "text.h" +using namespace std; + +//вывод +void show_histogram_text(const vector& bins, double bin_size) { + for (size_t i = 0; i < bins.size(); i++) { + cout << bins[i] << " | "; + + for (size_t j = 0; j < bins[i]; j++) { + cout << "*"; + } + cout << endl; + } +} diff --git a/text.h b/text.h new file mode 100644 index 0000000..2b075a1 --- /dev/null +++ b/text.h @@ -0,0 +1,8 @@ +#ifndef TEXT_H_INCLUDED +#define TEXT_H_INCLUDED + +#include + +void show_histogram_text(const std::vector& bins, double bin_size); + +#endif // TEXT_H_INCLUDED