From c71c68aea16127c5de3a742ed5546acc7bc072b4 Mon Sep 17 00:00:00 2001 From: YeremenkoMS Date: Thu, 21 Aug 2025 19:53:27 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=A0=20=E2=84=961:=20=D1=80=D0=B5=D1=84?= =?UTF-8?q?=D0=B0=D0=BA=D1=82=D0=BE=D1=80=D0=B8=D0=BD=D0=B3=20-=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B7=D0=B4=D0=B5=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=B4=D0=B0=20=D0=BD=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA?= =?UTF-8?q?=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 6 ++++-- histogram.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ histogram.h | 12 ++++++++++++ text.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ text.h | 9 +++++++++ 5 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 histogram.cpp create mode 100644 histogram.h create mode 100644 text.cpp create mode 100644 text.h diff --git a/.gitignore b/.gitignore index 5d0b009..a9b0f2b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ bin/ obj/ -*.o -*.exe +*.cbp +*.depend +*.layout +*.user diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..e716dca --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,47 @@ +#include "histogram.h" +#include + +using namespace std; + +// Реализация find_minmax +void +find_minmax(const vector& numbers, double& min, double& max) { + if (numbers.empty()) { + return; + } + + min = numbers[0]; + max = numbers[0]; + + for (double x : numbers) { + if (x < min) { + min = x; + } + if (x > max) { + max = x; + } + } +} + +// Реализация make_histogram +vector +make_histogram(const vector& numbers, size_t bin_count) { + double min, max; + find_minmax(numbers, min, max); + + vector bins(bin_count); + double bin_size = (max - min) / bin_count; + + for (double x : numbers) { + size_t bin_index = 0; + if (x == max) { + bin_index = bin_count - 1; + } + else { + bin_index = static_cast((x - min) / bin_size); + } + bins[bin_index]++; + } + + return bins; +} \ No newline at end of file diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..ebb7cb1 --- /dev/null +++ b/histogram.h @@ -0,0 +1,12 @@ +#pragma once +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED + +#include + +// Объявляем ОБЕ функции: и find_minmax, и make_histogram +void find_minmax(const std::vector& numbers, double& min, double& max); +std::vector make_histogram(const std::vector& numbers, size_t bin_count); + +#endif // HISTOGRAM_H_INCLUDED + diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..b1b6647 --- /dev/null +++ b/text.cpp @@ -0,0 +1,44 @@ +#include "text.h" +#include +#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; + + size_t max_count = 0; + for (size_t count : bins) { + if (count > max_count) { + max_count = count; + } + } + + for (size_t count : bins) { + // Выводим количество с выравниванием + if (count < 10) { + cout << " " << count << "|"; + } + else if (count < 100) { + cout << " " << count << "|"; + } + else { + cout << count << "|"; + } + + size_t height = 0; + if (max_count <= MAX_ASTERISK) { + height = count; + } + else { + height = MAX_ASTERISK * (static_cast(count) / max_count); + } + + for (size_t i = 0; i < height; i++) { + cout << "*"; + } + cout << "\n"; + } +} \ No newline at end of file diff --git a/text.h b/text.h new file mode 100644 index 0000000..cebae0a --- /dev/null +++ b/text.h @@ -0,0 +1,9 @@ +#pragma once +#ifndef TEXT_H_INCLUDED +#define TEXT_H_INCLUDED + +#include + +void show_histogram_text(const std::vector& bins); + +#endif // TEXT_H_INCLUDED \ No newline at end of file