From 68dc612a91336cb2aa22b59398431a4a63734479 Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 17 Apr 2023 15:28:58 +0300 Subject: [PATCH] final --- .gitignore | 10 ++++++++++ histogram.cpp | 12 +++++++++--- histogram_internal.h | 2 +- svg.cpp | 15 ++++++++++++--- svg_internal.h | 4 ++++ unittest.cpp | 4 +--- unittest1.cbp | 38 ++++++++++++++++++++++++++++++++++++++ unittest1.cpp | 28 ++++++++++++++++++++++++++++ 8 files changed, 103 insertions(+), 10 deletions(-) create mode 100644 .gitignore create mode 100644 svg_internal.h create mode 100644 unittest1.cbp create mode 100644 unittest1.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2b52cd8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +/bin +/obj +/lab01.depend +/lab01.layout +/main.exe +/main.o +/unittest.depend +/unittest.layout +/unittest1.depend + diff --git a/histogram.cpp b/histogram.cpp index 2ecc7ea..2cc95d2 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -5,24 +5,30 @@ using namespace std; -void +bool find_minmax(const vector& numbers, double& min1, double& max1) { + if (numbers.size() == 0) { + return false; + } + else { max1 = numbers[0]; min1 = numbers[0]; + } for(size_t i = 0; i < numbers.size(); i++) { if (numbers[i] > max1) max1 = numbers[i]; if (numbers[i] < min1) min1 = numbers[i]; } - return; + return true; } vector make_histogram(const vector& numbers, size_t bin_count) { double min1, max1; - find_minmax(numbers, min1, max1); + if (!find_minmax(numbers, min1, max1)) + cout << "error in find_minmax: empty vector"; vector bins; bins.resize(bin_count); diff --git a/histogram_internal.h b/histogram_internal.h index 627b9f6..86c66f8 100644 --- a/histogram_internal.h +++ b/histogram_internal.h @@ -1,5 +1,5 @@ #pragma once #include -void +bool find_minmax(const std::vector& numbers, double& min1, double& max1); diff --git a/svg.cpp b/svg.cpp index 41250c6..7ce4b88 100644 --- a/svg.cpp +++ b/svg.cpp @@ -1,6 +1,7 @@ #include "svg.h" #include #include +#include using namespace std; @@ -28,6 +29,13 @@ void svg_rect(double x, double y, double width, double height, string stroke = " cout << ""; } +void color_find(double bin, int maxb, int& color){ + color = (10 - (bin * 9) / maxb); + if (color < 0) { cout << "cant take color"; + color = 9; + } +} + void show_histogram_svg(const vector& bins) { @@ -43,20 +51,21 @@ show_histogram_svg(const vector& bins) { double top = 0; const size_t MAX_ASTERISK = IMAGE_WIDTH - TEXT_WIDTH; - double maxb = bins[0]; + int maxb = bins[0]; + int color; for (size_t j = 1; j < bins.size(); j++) { if (bins[j] > maxb) maxb = bins[j]; } - double maxb1 = maxb * BLOCK_WIDTH; + int maxb1 = maxb * BLOCK_WIDTH; for (size_t bin : bins) { double bin_width; - int color = (10 - (bin * 9) / maxb); + color_find(bin, maxb, color); if (maxb1 > MAX_ASTERISK) { diff --git a/svg_internal.h b/svg_internal.h new file mode 100644 index 0000000..72a94c9 --- /dev/null +++ b/svg_internal.h @@ -0,0 +1,4 @@ +#pragma once + +void +color_find(double bin, int maxb, int& color); diff --git a/unittest.cpp b/unittest.cpp index 981aa6f..b166477 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -6,9 +6,7 @@ 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("distinct identical numbers") { diff --git a/unittest1.cbp b/unittest1.cbp new file mode 100644 index 0000000..76dc570 --- /dev/null +++ b/unittest1.cbp @@ -0,0 +1,38 @@ + + + + + + diff --git a/unittest1.cpp b/unittest1.cpp new file mode 100644 index 0000000..f9c85bf --- /dev/null +++ b/unittest1.cpp @@ -0,0 +1,28 @@ +#define DOCTEST_CONFIG_NO_MULTITHREADING +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "doctest.h" +#include "svg_internal.h" + +TEST_CASE("color standart") { + int color = 0; + color_find(1, 5, color); + CHECK(color == 8); +} + +TEST_CASE("color equal") { + int color = 0; + color_find(5, 5, color); + CHECK(color == 1); +} + +TEST_CASE("color sr") { + int color = 0; + color_find(3, 5, color); + CHECK(color == 4); +} + +TEST_CASE("color nonsr") { + int color = 0; + color_find(7, 5, color); + CHECK(color == 9); +}