From 8700520ebc460292d905ef01d2aafd71d7744895 Mon Sep 17 00:00:00 2001 From: KozlovVV Date: Mon, 7 Oct 2024 01:05:34 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=BF=D1=80=D0=BE=D0=B3=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BC=D0=B0=20=D1=80=D0=B0=D0=B7=D0=B1=D0=B8=D1=82=D0=B0?= =?UTF-8?q?=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 --- histogram.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ histogram.h | 14 ++++++++++++++ text.cpp | 35 +++++++++++++++++++++++++++++++++++ text.h | 11 +++++++++++ 4 files changed, 111 insertions(+) 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..1df7c99 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,51 @@ +#include "histogram.h" + +vector +make_histogram(const vector& numbers, size_t & bin_count, size_t & number_count, size_t & max_count) { + double min, max; + find_minmax(numbers, min, max); + double bin_size = (max - min) / bin_count; + vector bins(bin_count); + max_count = bins[0]; + for (size_t i = 0; i < number_count; 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 (bins[j] > max_count){ + max_count = bins[j]; + } + } + if (!found){ + bins[bin_count - 1]++; + } + if (bins[bin_count - 1] > max_count){ + max_count = bins[bin_count - 1]; + } + } + return bins; +} + +void +find_minmax(const vector& numbers, double& min, double& max) { + if (!numbers.empty()) { + min = numbers[0]; + max = numbers[0]; + } else { + min = 0; + max = 0; + } + for (double x : numbers) + { + if (x < min){ + min = x; + } + else if (x > max){ + max = x; + } + } +} diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..7e3aefa --- /dev/null +++ b/histogram.h @@ -0,0 +1,14 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED + +#include + +using namespace std; + +vector +make_histogram(const vector& numbers, size_t & bin_count, size_t & number_count, size_t & max_count); + +void +find_minmax(const vector& numbers, double& min, double& max); + +#endif // HISTOGRAM_H_INCLUDED diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..4e136f9 --- /dev/null +++ b/text.cpp @@ -0,0 +1,35 @@ +#include "text.h" +#include +#include +using namespace std; +void +show_histogram_text(vector& bins, size_t & max_count,size_t & bin_count) { +const size_t SCREEN_WIDTH = 80; +const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; +if (max_count > MAX_ASTERISK){ + vector hights(bin_count); + for (size_t i = 0; i < bin_count; i++){ + size_t height = MAX_ASTERISK * (static_cast(bins[i]) / max_count); + hights[i] = height; + } + + for (size_t i = 0; i < bin_count; i++){ + printf("%3d|", bins[i]); + for (size_t j = 0; j < hights[i]; j++){ + cout<<"*"; + } + cout << endl; + } + } + else{ + for (size_t i = 0; i < bin_count; i++) + { + printf("%3d|", 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..985bf75 --- /dev/null +++ b/text.h @@ -0,0 +1,11 @@ +#ifndef SHOW-SVG_H_INCLUDED +#define SHOW-SVG_H_INCLUDED + +#include + +using namespace std; + +void +show_histogram_text(vector& bins, size_t & max_count,size_t & bin_count); + +#endif // SHOW-SVG_H_INCLUDED