From 8f0cc94d7b11da755e31ffabc92c888f4ef95180 Mon Sep 17 00:00:00 2001 From: SidoraDA <SidoraDA@mpei.ru> Date: Sun, 14 May 2023 18:22:19 +0400 Subject: [PATCH] =?UTF-8?q?code:=20cpp=20=D1=84=D0=B0=D0=B9=D0=BB=20=D0=BB?= =?UTF-8?q?=D1=801?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- laba1codeblocks.cpp | 101 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 laba1codeblocks.cpp diff --git a/laba1codeblocks.cpp b/laba1codeblocks.cpp new file mode 100644 index 0000000..8f14304 --- /dev/null +++ b/laba1codeblocks.cpp @@ -0,0 +1,101 @@ +#include <iostream> +#include <vector> + + +using namespace std; +const size_t SCREEN_WIDTH = 80; +const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + +int main() +{ + size_t i, j; //ԅ(≖‿≖ԅ)// + size_t histo_height; + size_t number_count; + size_t bin_count; + cerr << "Enter number count: "; + cin >> number_count; + vector<double> numbers(number_count); + for (i = 0; i < number_count; i++) + { + cerr << "Number[" << i << "] is "; + cin >> numbers[i]; + } + + + double max = numbers[0]; + double min = numbers[0]; + for (i = 0; i < number_count; i++) + { + if (numbers[i] < min) + min = numbers[i]; + + else if (numbers[i] > max) + max = numbers[i]; + } + cerr << "Enter bin count: "; + cin >> bin_count; + + cerr << "Enter histo height: "; + cin >> histo_height; + size_t table_height = histo_height / bin_count; + + vector<size_t> bins(bin_count); + double bin_size = (max - min) / bin_count; + size_t max_count = 0; + for (i = 0; i < number_count; i++) + { + bool found = false; + for (j = 0; (j < bin_count - 1) && !found; j++) + { + double lo = min + j * bin_size; + double hi = min + (j + 1) * bin_size; + if ((lo <= numbers[i]) && (numbers[i] < hi)) + { + bins[j]++; + found = true; + } + } + if (!found) + { + bins[bin_count - 1]++; + } + for (size_t i = 0; i < bin_count; i++) + { + if (bins[i] > max_count) + max_count = bins[i]; + + } + } + size_t height = 0; + for (i = 0; i < bin_count; i++) + { + + for (size_t a = 0; a < table_height; a++) + { + if (bins[i] < 100) + cout << " "; + if (bins[i] < 10) + cout << " "; + cout << bins[i] << "|"; + if (max_count > MAX_ASTERISK) + { + for (j = 0; j < (height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count)); j++) + { + cout << "*"; + + } + cout << endl; + } + else + { + for (j = 0; j < bins[i]; j++) + { + cout << "*"; + } + cout << endl; + } + + } + + } +}