From 4af56ed93a04edf0a23aed3980f4dae4f6b97ce1 Mon Sep 17 00:00:00 2001 From: DevyatovaMY Date: Sun, 21 Apr 2024 16:30:51 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D1=8B=D0=B5=20=D1=82=D0=B5=D1=81=D1=82=D1=8B;=20=D1=83?= =?UTF-8?q?=D0=BB=D1=83=D1=87=D1=88=D0=B5=D0=BD=D1=8B=20=D1=84=D1=83=D0=BD?= =?UTF-8?q?=D0=BA=D1=86=D0=B8=D0=B8=20=D1=81=20=D1=80=D0=B0=D1=81=D1=87?= =?UTF-8?q?=D0=B5=D1=82=D0=BE=D0=BC=20=D0=B8=20=D0=BF=D0=B5=D1=87=D0=B0?= =?UTF-8?q?=D1=82=D1=8C=D1=8E=20=D0=B3=D0=B8=D1=81=D1=82=D0=BE=D0=B3=D1=80?= =?UTF-8?q?=D0=B0=D0=BC=D0=BC=D1=8B=20=D0=B4=D0=BB=D1=8F=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D1=8B=20=D1=81=20=D0=BF=D1=83=D1=81=D1=82?= =?UTF-8?q?=D1=8B=D0=BC=20=D0=B2=D0=B5=D0=BA=D1=82=D0=BE=D1=80=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 68 ++++++++++++++++++++++++++++---------------- histogram_internal.h | 9 ++++++ lab01.cbp | 7 +++++ text.cpp | 7 ++++- unittest.cpp | 54 +++++++++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+), 26 deletions(-) create mode 100644 histogram_internal.h create mode 100644 unittest.cpp diff --git a/histogram.cpp b/histogram.cpp index 4d33c05..c5fad9a 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -2,20 +2,27 @@ using namespace std; -static void -find_minmax(const vector& numbers, double& min, double& max) +void +find_minmax(const vector& numbers, double& min, double& max, bool& empty_vector) { - min = numbers[0]; - max = numbers[0]; - for (double x : numbers) + if (numbers.empty()) { - if (x > max ) - { - max = x; - } - if (x < min) + empty_vector = true; + } + else + { + min = numbers[0]; + max = numbers[0]; + for (double x : numbers) { - min = x; + if (x > max ) + { + max = x; + } + if (x < min) + { + min = x; + } } } } @@ -25,25 +32,36 @@ make_histogram(const vector& numbers, size_t& bin_count) { double min = 0; double max = 0; - find_minmax(numbers, min, max); - auto bin_size = (max - min)/bin_count; + bool empty_vector = false; + find_minmax(numbers, min, max, empty_vector); vector bins(bin_count); - for (double number : numbers) + if (empty_vector) { - bool found = false; - for (size_t j = 0; (j < bin_count - 1) && !found; j++) + for (size_t y : bins) { - auto lo = min + j * bin_size; - auto hi = min + (j + 1) * bin_size; - if ((lo <= number) && (number < hi)) - { - bins[j]++; - found = true; - } + y = 0; } - if (!found) + } + else + { + auto bin_size = (max - min)/bin_count; + for (double number : numbers) { - bins[bin_count - 1]++; + bool found = false; + for (size_t j = 0; (j < bin_count - 1) && !found; j++) + { + auto lo = min + j * bin_size; + auto hi = min + (j + 1) * bin_size; + if ((lo <= number) && (number < hi)) + { + bins[j]++; + found = true; + } + } + if (!found) + { + bins[bin_count - 1]++; + } } } return bins; diff --git a/histogram_internal.h b/histogram_internal.h new file mode 100644 index 0000000..8f2b497 --- /dev/null +++ b/histogram_internal.h @@ -0,0 +1,9 @@ +#ifndef HISTOGRAM_INTERNAL_H_INCLUDED +#define HISTOGRAM_INTERNAL_H_INCLUDED + +#include + +void +find_minmax(const std::vector& numbers, double& min, double& max, bool& empty_vector); + +#endif // HISTOGRAM_INTERNAL_H_INCLUDED diff --git a/lab01.cbp b/lab01.cbp index 68d2c6d..a79945d 100644 --- a/lab01.cbp +++ b/lab01.cbp @@ -36,7 +36,14 @@ + + + + + diff --git a/text.cpp b/text.cpp index 29c73d5..28aa71e 100644 --- a/text.cpp +++ b/text.cpp @@ -4,7 +4,8 @@ using namespace std; void -show_histogram_text(const vector& bins, size_t& bin_count) { +show_histogram_text(const vector& bins, size_t& bin_count) +{ const size_t SCREEN_WIDTH = 80; const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; size_t max_count = 0; @@ -15,6 +16,10 @@ show_histogram_text(const vector& bins, size_t& bin_count) { max_count = bins[s]; } } + if (max_count == 0) + { + max_count = 1; + } auto scale_factor = static_cast(MAX_ASTERISK) / max_count; if (scale_factor > 1) { diff --git a/unittest.cpp b/unittest.cpp new file mode 100644 index 0000000..5c3f8bd --- /dev/null +++ b/unittest.cpp @@ -0,0 +1,54 @@ +#define DOCTEST_CONFIG_NO_MULTITHREADING +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "doctest.h" +#include "histogram_internal.h" + +TEST_CASE("distinct positive numbers") { + double min = 0; + double max = 0; + bool empty_vector = false; + find_minmax({1, 2}, min, max, empty_vector); + CHECK(empty_vector == false); + CHECK(min == 1); + CHECK(max == 2); +} + +TEST_CASE("the only number") { + double min = 0; + double max = 0; + bool empty_vector = false; + find_minmax({1}, min, max, empty_vector); + CHECK(empty_vector == false); + CHECK(min == 1); + CHECK(max == 1); +} + +TEST_CASE("negative numbers") { + double min = 0; + double max = 0; + bool empty_vector = false; + find_minmax({-1, -2}, min, max, empty_vector); + CHECK(empty_vector == false); + CHECK(min == -2); + CHECK(max == -1); +} + +TEST_CASE("identical numbers") { + double min = 0; + double max = 0; + bool empty_vector = false; + find_minmax({2, 2}, min, max, empty_vector); + CHECK(empty_vector == false); + CHECK(min == 2); + CHECK(max == 2); +} + +TEST_CASE("empty vector") { + double min = 0; + double max = 0; + bool empty_vector = false; + find_minmax({}, min, max, empty_vector); + CHECK(empty_vector == true); + CHECK(min == 0); + CHECK(max == 0); +}