From 8d9236c66a42b73955aaa4d78fae6a0c4456e533 Mon Sep 17 00:00:00 2001 From: StepanishchevVR Date: Mon, 13 May 2024 11:35:59 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=B2=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=81=D0=B2=D0=BE=D0=B5=D0=B3=D0=BE?= =?UTF-8?q?=20=D0=B2=D0=B0=D1=80=D0=B8=D0=B0=D0=BD=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 5 +++++ main.cpp | 3 ++- svg.cpp | 25 ++++++++++++++++++++----- unittest.cpp | 17 +++++++++++++++++ 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/histogram.cpp b/histogram.cpp index e308905..2838ce7 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -3,6 +3,11 @@ void find_minmax(const std::vector& numbers, double& min, double& max) { + if (numbers.size()==0) + { + return; + } + min = numbers[0]; max = numbers[0]; for (double x : numbers) diff --git a/main.cpp b/main.cpp index ee97d47..4d8de74 100644 --- a/main.cpp +++ b/main.cpp @@ -16,6 +16,7 @@ Input input_data() { Input in; size_t number_count; + cerr << "Enter num count >>"; cin >> number_count; in.numbers.resize(number_count); @@ -25,6 +26,7 @@ Input input_data() cin >> in.numbers[i]; } + cerr << "Enter bin count >>"; cin >> in.bin_count; return in; } @@ -34,5 +36,4 @@ int main() auto in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); show_histogram_svg(bins); - } diff --git a/svg.cpp b/svg.cpp index 875eb2f..fba8a7a 100644 --- a/svg.cpp +++ b/svg.cpp @@ -5,7 +5,7 @@ void svg_begin(double width, double height) { std::cout << "\n"; std::cout << "\n"; @@ -15,14 +15,28 @@ 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::size_t font_size) { + std::cout << "" << text << ""; } void svg_rect(double x, double y, double width, double height, std::string stroke = "black", std::string fill = "black") { std::cout << ""; } +std::size_t in_font_size() { + std::size_t font_size; + do{ + std::cerr << "Enter font size >>"; + std::cin >> font_size; + if (font_size < 8 || font_size > 32) { + std::cerr << "Please, observe the range 8-32!" << std::endl; + } + }while(font_size < 8 || font_size > 32); + + return font_size; +} + + void show_histogram_svg(const std::vector& bins) { const auto IMAGE_WIDTH = 400; @@ -32,6 +46,7 @@ void show_histogram_svg(const std::vector& bins) { const auto TEXT_WIDTH = 50; const auto BIN_HEIGHT = 30; const auto BLOCK_WIDTH = 10; + std::size_t font_size = in_font_size(); const std::size_t MAX_ASTERISK = IMAGE_WIDTH - TEXT_WIDTH; svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); @@ -47,7 +62,7 @@ void show_histogram_svg(const std::vector& bins) { 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_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bins[i]), font_size); svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT); top += BIN_HEIGHT; } @@ -56,7 +71,7 @@ void show_histogram_svg(const std::vector& bins) { 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_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bins[i]), font_size); svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT); top += BIN_HEIGHT; } diff --git a/unittest.cpp b/unittest.cpp index 4eea04a..0f3861e 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -19,4 +19,21 @@ TEST_CASE("distinct negative numbers") { CHECK(max == -1); } +TEST_CASE("distinct one number") { + double min = 0; + double max = 0; + find_minmax({-1}, min, max); + CHECK(min == -1); + CHECK(max == -1); +} + +TEST_CASE("distinct empty array") { + double min = 0; + double max = 0; + std::vector numbers; + find_minmax(numbers, min, max); + CHECK(min == 0); + CHECK(max == 0); +} +