From 408986795073b982bd13576108e2b08126070ea9 Mon Sep 17 00:00:00 2001 From: artemzelenov <artemzeleznov40@gmail.com> Date: Fri, 21 Apr 2023 19:43:06 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BE=D0=B4=D0=B0=20=D0=B4=D0=BE=20?= =?UTF-8?q?=D0=BF=D1=83=D0=BD=D0=BA=D1=82=D0=B0=205?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- project/histogram.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ project/histogram.h | 8 ++++++++ project/text.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ project/text.h | 7 +++++++ 4 files changed, 100 insertions(+) create mode 100644 project/histogram.cpp create mode 100644 project/histogram.h create mode 100644 project/text.cpp create mode 100644 project/text.h diff --git a/project/histogram.cpp b/project/histogram.cpp new file mode 100644 index 0000000..e3fabd9 --- /dev/null +++ b/project/histogram.cpp @@ -0,0 +1,43 @@ +#include <iostream> +#include <vector> +#include "histogram.h" +using namespace std; +static void +find_minmax(vector<double> numbers, double& min, double& max) { + min = numbers[0]; + max = numbers[0]; + for (double x : numbers) { + if (x < min) { + min = x; + } + else if (x > max) { + max = x; + } +} +} + +vector<size_t> +make_histogram(vector<double> numbers, size_t bin_count) { + vector <size_t> bins(bin_count); + double min, max; + find_minmax(numbers, min, max); + size_t countt; + 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]++; + } + } + return bins; + + +} diff --git a/project/histogram.h b/project/histogram.h new file mode 100644 index 0000000..ed9e7b4 --- /dev/null +++ b/project/histogram.h @@ -0,0 +1,8 @@ +#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/project/text.cpp b/project/text.cpp new file mode 100644 index 0000000..74f8841 --- /dev/null +++ b/project/text.cpp @@ -0,0 +1,42 @@ +#include <iostream> +#include <vector> +#include "text.h" +#include "histogram.h" +using namespace std; +void +show_histogram_text(vector<size_t> bins, size_t bin_count){ + const size_t SCREEN_WIDTH = 80; + const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + size_t max_count = 0; + for (size_t i = 0; i < bin_count; i++){ + if(bins[i]>max_count) + max_count = bins[i]; + } + size_t height; + for (size_t i = 0; i < bin_count; i++) + { + if (bins[i] < 100) + cout << " "; + if (bins[i] < 10) + cout << " "; + cout<<bins[i]<<"|"; + if(max_count>MAX_ASTERISK){ + for (size_t j = 0; j <(height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count)) ; j++) + { + cout<<"*"; + } + cout<<endl; + } + else + { + for (size_t j = 0; j <bins[i] ; j++) + { + cout<<"*"; + } + cout<<endl; + } + } + return; + + +} diff --git a/project/text.h b/project/text.h new file mode 100644 index 0000000..bd0f200 --- /dev/null +++ b/project/text.h @@ -0,0 +1,7 @@ +#ifndef TEXT_H_INCLUDED +#define TEXT_H_INCLUDED + +void +show_histogram_text(std::vector<size_t> bins, size_t bin_count); + +#endif // TEXT_H_INCLUDED