From 21c648187d862c7b31eab040730d7419d7ffc2d3 Mon Sep 17 00:00:00 2001 From: "Smirnoff (SmirnovDA)" Date: Sat, 17 May 2025 16:03:26 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=A0=D0=B0=D0=B7=D0=B1=D0=B8=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BD=D0=B0=20=D1=84=D0=B0=D0=B9=D0=BB?= =?UTF-8?q?=D1=8B=20=D1=87=D1=82=D0=BE(?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ histogram.h | 5 +++++ text.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ text.h | 6 ++++++ 4 files changed, 125 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..941af36 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,56 @@ +#include +#include +#include "histogram.h" + +using namespace std; + +bool find_minmax(const vector &numbers, double &min, double &max) +{ + + if(numbers.empty()){ + return false; + } + else{ + min = numbers[0]; + max = numbers[0]; + for (double x : numbers) + { + if (x < min) + { + min = x; + } + else if (x > max) + { + max = x; + } + } +} +} +vector make_histogram(const vector &numbers, size_t bin_count) +{ + double min, max; + bool fl; + find_minmax(numbers, min, max); + double bin_size = (max - min) / bin_count; + vector bins (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/histogram.h b/histogram.h new file mode 100644 index 0000000..2b8887f --- /dev/null +++ b/histogram.h @@ -0,0 +1,5 @@ +#pragma once +#include + +std::vector make_histogram(const std::vector& numbers, std::size_t bin_count); + diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..ebeb410 --- /dev/null +++ b/text.cpp @@ -0,0 +1,58 @@ +#include +#include +#include "histogram.h" +#include "text.h" + +using namespace std; + +void show_histogram_text(const vector& bins, size_t& bin_count, double bin_size) +{ + const size_t SCREEN_WIDTH = 80; + const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + size_t max_bin_count = bins[0]; + for(size_t i = 0; i < bin_count; i++) + { + if(bins[i] > max_bin_count) + { + max_bin_count = bins[i]; + } + } + + for(size_t i = 0; i < bin_count; i++) + { + if(bins[i]<10) + { + cout<< " " << bins[i] << "|"; + } + else + { + if(bins[i]<100) + { + cout<< " " << bins[i] << "|"; + } + else + { + if(bins[i]<1000) + { + cout<< bins[i] << "|"; + } + + } + } + + size_t height; + if(max_bin_count <= MAX_ASTERISK) + { + height = bins[i]; + } + else + { + height = MAX_ASTERISK * (static_cast(bins[i]) / max_bin_count); + } + for(size_t j = 0; j < height; j ++) + { + cout<<"*"; + } + cout< + +void show_histogram_text(const std::vector& bins, std::size_t& bin_count, double bin_size); + +