From 560d9287118af0b1666d52cf10027985caa97c55 Mon Sep 17 00:00:00 2001 From: ArtyushinaVV Date: Sun, 23 Apr 2023 23:09:13 +0300 Subject: [PATCH] code: final --- histogram.cpp | 33 +++++++++++++++++-------------- histogram.h | 2 +- histogram_internal.h | 5 +++++ svg.cpp | 46 ++++++++++++++++++++++++++++++++------------ svg.h | 10 ++++++++++ unittest.cpp | 29 +++++++++++++--------------- 6 files changed, 82 insertions(+), 43 deletions(-) create mode 100644 histogram_internal.h diff --git a/histogram.cpp b/histogram.cpp index 7aa16f5..8b5b2c8 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -4,19 +4,22 @@ #include "histogram.h" using namespace std; -void -find_minmax(const vector &numbers, double& min, double& max) +bool +find_minmax(const vector& numbers, double& min, double& max) { - - min = numbers[0]; + if (numbers.size() == 0) { + return false; + } + else { max = numbers[0]; - for (size_t i = 0; i < numbers.size(); i++) + min = numbers[0]; + } + for(size_t i = 0; i < numbers.size(); i++) { - if (numbers[i] > max) - max = numbers[i]; - if (numbers[i] < min) - min = numbers[i]; + if (numbers[i] > max) max = numbers[i]; + if (numbers[i] < min) min = numbers[i]; } + return true; } vector make_histogram (const vector& numbers, size_t &bin_count) @@ -26,7 +29,9 @@ vector make_histogram (const vector& numbers, size_t &bin_count) double min, max; float low, hi; - find_minmax(numbers, min, max); + if (!find_minmax(numbers, min, max)) + cout << "error in find_minmax: empty vector"; + double bin_size = (max - min) / bin_count; low = min; @@ -34,19 +39,19 @@ vector make_histogram (const vector& numbers, size_t &bin_count) for (size_t i = 0; i < numbers.size(); i++) { - bool found = false; + bool flag = false; - for (size_t j = 0; (j < bin_count - 1) && !found; j++) + for (size_t j = 0; (j < bin_count - 1) && !flag; j++) { low = min + j * bin_size; hi = min + (j + 1) * bin_size; if ((low <= numbers[i]) && (numbers[i] < hi)) { bins[j]++; - found = true; + flag = true; } } - if (found==false) + if (flag==false) { bins[bin_count - 1]++; } diff --git a/histogram.h b/histogram.h index 0623f24..b6691e1 100644 --- a/histogram.h +++ b/histogram.h @@ -1,5 +1,5 @@ #pragma once #include -std::vector +;std::vector make_histogram(const std::vector &numbers, size_t &bin_count); diff --git a/histogram_internal.h b/histogram_internal.h new file mode 100644 index 0000000..95fd608 --- /dev/null +++ b/histogram_internal.h @@ -0,0 +1,5 @@ +#pragma once +#include + +bool +find_minmax(const std::vector &numbers, double& min, double& max) diff --git a/svg.cpp b/svg.cpp index 76d2591..78ffcb8 100644 --- a/svg.cpp +++ b/svg.cpp @@ -29,8 +29,16 @@ cout << "& bins) { + const auto IMAGE_WIDTH = 400; const auto IMAGE_HEIGHT = 300; const auto TEXT_LEFT = 20; @@ -38,23 +46,37 @@ show_histogram_svg(const vector& bins) { const auto TEXT_WIDTH = 50; const auto BIN_HEIGHT = 30; const auto BLOCK_WIDTH = 10; - const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH; - - svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT); + svg_begin(400, 300); double top = 0; -double max_count = bins[0]; - for (size_t i = 0; i < bins.size(); i++) { - if (bins[i] > max_count) - max_count = bins[i]; + const size_t MAX_ASTERISK = IMAGE_WIDTH - TEXT_WIDTH; + int maxb = bins[0]; + int color; + + for (size_t j = 1; j < bins.size(); j++) + { + if (bins[j] > maxb) maxb = bins[j]; } + + int maxb1 = maxb * BLOCK_WIDTH; + for (size_t bin : bins) { - const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH)*(bin/max_count); - svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); - svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "red", "#aaffaa"); - top += BIN_HEIGHT; - } + + double bin_width; + + color_find(bin, maxb, color); + + if (maxb1 > MAX_ASTERISK) + { + bin_width = BLOCK_WIDTH * MAX_ASTERISK * (bin / maxb1); + } + else bin_width = BLOCK_WIDTH * bin; + + svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "violet", to_string(color)); + top += BIN_HEIGHT; +} svg_end(); } diff --git a/svg.h b/svg.h index 32e5299..a0d983d 100644 --- a/svg.h +++ b/svg.h @@ -1,12 +1,22 @@ #pragma once #include using namespace std; + +void +color_find(double bin, int maxb, int& color); + void svg_begin(double width, double height); void svg_end(); +void +svg_text(double left, double baseline, string text); + +void +svg_rect(double x, double y, double width, double height, string stroke, string fill); + void show_histogram_svg(const vector& bins); diff --git a/unittest.cpp b/unittest.cpp index a7e0865..df65d28 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -8,32 +8,29 @@ TEST_CASE("distinct positive numbers") { double min = 0; double max = 0; - find_minmax({1, 2}, min, max); - CHECK(min == 1); - CHECK(max == 2); + CHECK(find_minmax({}, min, max) == false); } - -TEST_CASE("unit vector") { +TEST_CASE("distinct identical numbers") { double min = 0; double max = 0; - find_minmax({1}, min, max); - CHECK(min == 1); - CHECK(max == 1); + find_minmax({5, 5}, min, max); + CHECK(min == 5); + CHECK(max == 5); } -TEST_CASE("distinct negative numbers") { +TEST_CASE("distinct one number") { double min = 0; double max = 0; - find_minmax({-1,-2}, min, max); - CHECK(min == -2); - CHECK(max == -1); + find_minmax({3}, min, max); + CHECK(min == 3); + CHECK(max == 3); } -TEST_CASE("same nambers") { +TEST_CASE("distinct negative numbers") { double min = 0; double max = 0; - find_minmax({1,1}, min, max); - CHECK(min == 1); - CHECK(max == 1); + find_minmax({-3, -1}, min, max); + CHECK(min == -3); + CHECK(max == -1); }