From f5b20f01bde02e4d530d164bc07c0534e7e07871 Mon Sep 17 00:00:00 2001 From: YaroslavS Date: Mon, 28 Apr 2025 01:07:53 +0300 Subject: [PATCH] add show_histogram_text in text.cpp --- Histogram/text.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Histogram/text.cpp b/Histogram/text.cpp index 8b13789..6e74549 100644 --- a/Histogram/text.cpp +++ b/Histogram/text.cpp @@ -1 +1,27 @@ +#include "text.h" +#include +#include +void show_histogram_text(const std::vector& bins) { + const size_t SCREEN_WIDTH = 80; + const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + size_t max_bin = 0; + for (size_t bin : bins) { + if (bin > max_bin) { + max_bin = bin; + } + } + for (size_t bin_value : bins) { + if (bin_value < 100) std::cout << " "; + if (bin_value < 10) std::cout << " "; + std::cout << bin_value << " |"; + size_t height = bin_value; + if (max_bin > MAX_ASTERISK) { + height = static_cast(MAX_ASTERISK * (static_cast(bin_value) / max_bin)); + } + for (size_t i = 0; i < height; ++i) { + std::cout << "*"; + } + std::cout << "\n"; + } +};