From 6cabc222cab361f8d4c063666980610ff882457d Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 22 May 2024 01:01:15 +0300 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D0=BE=D1=82=D0=BE=D0=B2?= =?UTF-8?q?=D0=BA=D0=B0=20=D1=81=D0=BE=D0=B1=D1=81=D1=82=D0=B2=D0=B5=D0=BD?= =?UTF-8?q?=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=B2=D0=B0=D1=80=D0=B8=D0=B0=D0=BD?= =?UTF-8?q?=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 4 +-- histogram.h | 2 +- main.cpp | 3 ++- svg.cpp | 67 ++++++++++++++++++++++++++++++++++++++++----------- svg.h | 2 +- 5 files changed, 59 insertions(+), 19 deletions(-) diff --git a/histogram.cpp b/histogram.cpp index 19be3a2..12b2907 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -17,13 +17,13 @@ void find_minmax(const std::vector& numbers, double& min, double& max) } } -std::vector make_histogram(const std::vector &numbers, std::size_t bin_count) +std::vector make_histogram(const std::vector &numbers, std::size_t bin_count) { double min = numbers[0]; double max = numbers[0]; find_minmax(numbers, min, max); double bin_size = (max - min) / bin_count; - std::vectorbins(bin_count); + std::vectorbins(bin_count); for (std::size_t i = 0; i < numbers.size(); i++) { diff --git a/histogram.h b/histogram.h index 6d24980..3723495 100644 --- a/histogram.h +++ b/histogram.h @@ -1,5 +1,5 @@ #ifndef HISTOGRAM_H_INCLUDED #define HISTOGRAM_H_INCLUDED #include -std::vector make_histogram(const std::vector& numbers, std::size_t bin_count); +std::vector make_histogram(const std::vector& numbers, std::size_t bin_count); #endif // HISTOGRAM_H_INCLUDED diff --git a/main.cpp b/main.cpp index b5028a4..a6b9874 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,7 @@ #include #include "histogram.h" #include "text.h" +#include "svg.h" using namespace std; @@ -32,6 +33,6 @@ int main() { auto in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); - show_histogram_text(bins); + show_histogram_svg(bins); return 0; } diff --git a/svg.cpp b/svg.cpp index 3e04594..21b6928 100644 --- a/svg.cpp +++ b/svg.cpp @@ -2,7 +2,13 @@ #include #include "svg.h" -void svg_begin(double width, double height) { +void InCol (){ + std::string color; + std::cin >> color; +} + +void svg_begin(double width, double height) +{ std::cout << "\n"; std::cout << "\n"; } -void svg_end() { +void svg_end() +{ std::cout << "\n"; } -void svg_text(double left, double baseline, std::string text) { - std::cout << ""<< text <<""; +void svg_text(double left, double baseline, std::string text) +{ + std::cout << ""<< text <<""; } -void svg_rect(double x, double y, double width, double height) { - std::cout << ""; +void svg_rect(double x, double y, double width, double height, std::string color) +{ + std::cout << ""; } -void show_histogram_svg(const std::vector& bins) { +void show_histogram_svg(const std::vector& bins) +{ + const auto IMAGE_WIDTH = 400; const auto IMAGE_HEIGHT = 300; const auto TEXT_LEFT = 20; @@ -32,14 +43,42 @@ void show_histogram_svg(const std::vector& bins) { const auto BIN_HEIGHT = 30; const auto BLOCK_WIDTH = 10; - svg_begin(400, 300); + std::string color; + InCol(); + + const std::size_t MAX_ASTERISK = IMAGE_WIDTH - TEXT_WIDTH; + svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); double top = 0; - for (size_t bin : bins) { - const double bin_width = BLOCK_WIDTH * bin; - svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin)); - svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT); - top += BIN_HEIGHT; + + std::size_t maxbin=bins[0]; + for (std::size_t i=0; i < bins.size(); i++) + { + if (maxbin < bins[i]) + { + maxbin=bins[i]; + } } - svg_end(); + if (maxbin<=76) + { + for (std::size_t i=0; i < bins.size(); i++) + { + double bin_width= MAX_ASTERISK * (static_cast(bins[i]) / maxbin); + svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bins[i])); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, color); + top += BIN_HEIGHT; + } + svg_end(); + } + else + { + for (std::size_t i=0; i < bins.size(); i++) + { + double bin_width = BLOCK_WIDTH * bins[i]; + svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bins[i])); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, color); + top += BIN_HEIGHT; + } + svg_end(); + } } diff --git a/svg.h b/svg.h index c01c807..b29b7fd 100644 --- a/svg.h +++ b/svg.h @@ -1,5 +1,5 @@ #ifndef SVG_H_INCLUDED #define SVG_H_INCLUDED #include -void show_histogram_svg(const std::vector& bins); +void show_histogram_svg(const std::vector& bins); #endif // SVG_H_INCLUDED