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<double>& 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 << "<?xml version='1.0' encoding='UTF-8'?>\n"; std::cout << "<svg "; - std::cout << "width='" << width << "' "; + std::cout << "width23='" << width << "' "; std::cout << "height='" << height << "' "; std::cout << "viewBox='0 0 " << width << " " << height << "' "; std::cout << "xmlns='http://www.w3.org/2000/svg'>\n"; @@ -15,14 +15,28 @@ void svg_end() { std::cout << "</svg>\n"; } -void svg_text(double left, double baseline, std::string text) { - std::cout << "<text x='" << left << "' y='"<< baseline <<"'>"<< text <<"</text>"; +void svg_text(double left, double baseline, std::string text, std::size_t font_size) { + std::cout << "<text x='" << left << "' y='" << baseline << "' font-size='" << font_size << "'>" << text << "</text>"; } void svg_rect(double x, double y, double width, double height, std::string stroke = "black", std::string fill = "black") { std::cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << " ' stroke='#000000' fill='#F8B496' ></rect>"; } +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<std::size_t>& bins) { const auto IMAGE_WIDTH = 400; @@ -32,6 +46,7 @@ void show_histogram_svg(const std::vector<std::size_t>& 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<std::size_t>& bins) { if (maxbin<=76) { for (std::size_t i=0; i < bins.size(); i++) { double bin_width= MAX_ASTERISK * (static_cast<double>(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<std::size_t>& 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<double> numbers; + find_minmax(numbers, min, max); + CHECK(min == 0); + CHECK(max == 0); +} +