From 32335cc93c33631f69f2d561cac0aa74e4fdced4 Mon Sep 17 00:00:00 2001 From: "Artyom (StepanovAV)" Date: Mon, 22 Apr 2024 19:27:35 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D1=8B=D0=B9=20=D0=B2=D0=B0=D1=80=D0=B8=D0=B0=D0=BD?= =?UTF-8?q?=D1=82=20=D0=BE=D0=B1=D1=89=D0=B5=D0=B3=D0=BE=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab34/histogram.cpp | 45 +++++++++++++++++++++++++++++++++ lab34/histogram.h | 6 +++++ lab34/lab34.cbp | 43 ++++++++++++++++++++++++++++++++ lab34/main.cpp | 35 ++++++++++++++++++++++++++ lab34/svg.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++ lab34/svg.h | 5 ++++ lab34/text.cpp | 43 ++++++++++++++++++++++++++++++++ lab34/text.h | 5 ++++ 8 files changed, 243 insertions(+) create mode 100644 lab34/histogram.cpp create mode 100644 lab34/histogram.h create mode 100644 lab34/lab34.cbp create mode 100644 lab34/main.cpp create mode 100644 lab34/svg.cpp create mode 100644 lab34/svg.h create mode 100644 lab34/text.cpp create mode 100644 lab34/text.h diff --git a/lab34/histogram.cpp b/lab34/histogram.cpp new file mode 100644 index 0000000..44da864 --- /dev/null +++ b/lab34/histogram.cpp @@ -0,0 +1,45 @@ +#include "histogram.h" +#include +#include +#include + +void +find_minmax(const std::vector& numbers, double& min, double& max, bool& res){ + if (numbers.size() == 0){ + res = false; + return; + } + min = numbers[0]; + max = numbers[0]; + for (auto x : numbers) { + if (x < min) { + min = x; + } + else if (x > max) { + max = x; + } + } +} +std::vector +make_histogram(const std::vector& numbers, size_t bin_count){ + double min, max; + bool res = true; + find_minmax (numbers, min, max, res); + if (res == false){ + std::cerr << "Number of elements cannot be equal to zero"; + exit(1); + } + double bin_size = (max - min) / bin_count; + std::vector bins(bin_count); + for (auto x : numbers) { + bool found = false; + for (size_t j = 0; (j < bin_count - 1) && !found ; j++) { + if ((min + j * bin_size <= x) && (x < min + (j + 1) * bin_size)) { + bins[j] += 1; + found = true; + } + } + if (!found) bins[bin_count - 1]++; + } + return bins; +} diff --git a/lab34/histogram.h b/lab34/histogram.h new file mode 100644 index 0000000..71ce8fa --- /dev/null +++ b/lab34/histogram.h @@ -0,0 +1,6 @@ +#pragma once +#include + +std::vector +make_histogram(const std::vector& numbers, size_t bin_count); + diff --git a/lab34/lab34.cbp b/lab34/lab34.cbp new file mode 100644 index 0000000..5cfcdaa --- /dev/null +++ b/lab34/lab34.cbp @@ -0,0 +1,43 @@ + + + + + + diff --git a/lab34/main.cpp b/lab34/main.cpp new file mode 100644 index 0000000..fd75335 --- /dev/null +++ b/lab34/main.cpp @@ -0,0 +1,35 @@ +#include +#include +#include "histogram.h" +#include "text.h" +#include "svg.h" + +using namespace std; + +struct Input { + vector numbers; + size_t bin_count{}; +}; + +Input +input_data(){ + size_t number_count; + cin >> number_count; + Input in; + in.numbers.resize(number_count); + vector numbers(number_count); + for (size_t i = 0; i < number_count; i++) { + cin >> in.numbers[i]; + } + cin >> in.bin_count; + return in; +} + + +int +main() { + auto in = input_data(); + auto bins = make_histogram(in.numbers, in.bin_count); + show_histogram_svg(bins); + return 0; +} diff --git a/lab34/svg.cpp b/lab34/svg.cpp new file mode 100644 index 0000000..c8cd4e1 --- /dev/null +++ b/lab34/svg.cpp @@ -0,0 +1,61 @@ +#include "svg.h" +#include +#include + +using namespace std; + +void +svg_begin(double width, double height) { + cout << "\n"; + cout << "\n"; +} + +void +svg_end() { + cout << "\n"; +} + +void +svg_text(double left, double baseline, string text) { + cout << "" << text << ""; +} + +void +svg_rect(double x, double y, double width, double height, + string stroke = "black", string fill = "orange"){ + cout << ""; +} + + +void +show_histogram_svg(const vector& bins) { + const auto IMAGE_WIDTH = 400; + const auto IMAGE_HEIGHT = 300; + const auto TEXT_LEFT = 20; + const auto TEXT_BASELINE = 20; + const auto TEXT_WIDTH = 50; + const auto BIN_HEIGHT = 30; + const auto BLOCK_WIDTH = 10; + svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); + double top = 0; + double maxbin = bins[0]; + for (size_t bin : bins) { + if (maxbin < bin){ + maxbin = bin; + } + } + for (size_t bin : bins) { + double bin_width = BLOCK_WIDTH * bin; + if ((IMAGE_WIDTH - TEXT_WIDTH) < (bin * BLOCK_WIDTH)){ + bin_width = (IMAGE_WIDTH - TEXT_WIDTH) * ( bin / maxbin ); + } + svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT); + top += BIN_HEIGHT; + } + svg_end(); +} diff --git a/lab34/svg.h b/lab34/svg.h new file mode 100644 index 0000000..5e3353c --- /dev/null +++ b/lab34/svg.h @@ -0,0 +1,5 @@ +#pragma once +#include + +void +show_histogram_svg(const std::vector& bins); diff --git a/lab34/text.cpp b/lab34/text.cpp new file mode 100644 index 0000000..1e1fdca --- /dev/null +++ b/lab34/text.cpp @@ -0,0 +1,43 @@ +#include "text.h" +#include +#include + +using namespace std; + +const size_t SCREEN_WIDTH = 80; +const size_t MAX_LENGTH = SCREEN_WIDTH - 3 - 1; + +void +show_histogram_text(vector bins, size_t bin_count ){ + auto max_count = bins[0]; + for (auto x : bins) { + if (x > max_count) { + max_count = x; + } + } + for (size_t i = 0; i < bin_count; i++) { + size_t j = 100; + while (bins[i] < j) { + cout << " "; + j /= 10; + } + cout << bins[i] << "|"; + if (max_count > MAX_LENGTH) { + auto count = bins[i]; + size_t height = MAX_LENGTH * (static_cast(count) / max_count); + j = 0; + while (j < height) { + cout << "*"; + j++; + } + } + else { + j = 0; + while (j < bins[i]) { + cout << "*"; + j++; + } + } + cout << endl; + } +} diff --git a/lab34/text.h b/lab34/text.h new file mode 100644 index 0000000..52f62a7 --- /dev/null +++ b/lab34/text.h @@ -0,0 +1,5 @@ +#pragma once +#include + +void +show_histogram_text(std::vector bins, size_t bin_count );