From 33c7a23266d57b33d95b2f2df8a26da152f7350f Mon Sep 17 00:00:00 2001 From: "Platon (MiachinPY)" Date: Thu, 1 May 2025 13:03:56 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20unittest.cbp(cpp)=20=D1=84=D0=B0=D0=B9=D0=BB?= =?UTF-8?q?=D1=8B,=20doctest.h=20=D0=B8=20histogram=5Finternal.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 2 +- histogram_internal.h | 6 ++++ historgam.cpp | 52 ++++++++++++++++++++++++++++ lab01.cbp | 11 ++++++ lab01.depend | 80 ++++++++++++++++++++++++++++++++++++++++++++ lab01.layout | 15 +++++++++ main.cpp | 40 +--------------------- text.cpp | 44 ++++++++++++++++++++++++ text.h | 6 ++++ unittest.cbp | 38 +++++++++++++++++++++ unittest.cpp | 26 ++++++++++++++ unittest.depend | 66 ++++++++++++++++++++++++++++++++++++ 12 files changed, 346 insertions(+), 40 deletions(-) create mode 100644 histogram_internal.h create mode 100644 historgam.cpp create mode 100644 lab01.depend create mode 100644 lab01.layout create mode 100644 text.cpp create mode 100644 text.h create mode 100644 unittest.cbp create mode 100644 unittest.cpp create mode 100644 unittest.depend diff --git a/histogram.cpp b/histogram.cpp index 728e7a3..4d98aed 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -3,7 +3,7 @@ #include "histogram.h" using namespace std; -void static find_minmax(const vector &numbers, double &min, double &max) +void find_minmax(const vector &numbers, double &min, double &max) { min = numbers[0]; max = numbers[0]; diff --git a/histogram_internal.h b/histogram_internal.h new file mode 100644 index 0000000..8e35594 --- /dev/null +++ b/histogram_internal.h @@ -0,0 +1,6 @@ +#ifndef HISTOGRAM_INTERNAL_H_INCLUDED +#define HISTOGRAM_INTERNAL_H_INCLUDED +#include +void find_minmax(const std::vector &numbers, double &min, double &max); + +#endif // HISTOGRAM_INTERNAL_H_INCLUDED diff --git a/historgam.cpp b/historgam.cpp new file mode 100644 index 0000000..4d98aed --- /dev/null +++ b/historgam.cpp @@ -0,0 +1,52 @@ +#include +#include +#include "histogram.h" +using namespace std; + +void find_minmax(const vector &numbers, double &min, double &max) +{ + min = numbers[0]; + max = numbers[0]; + for(double number : numbers) + { + if(number < min) + { + min = number; + } + if(number > max) + { + max = number; + } + } + return; + +} + +std::vector make_histogram(std::vector& numbers, size_t bin_count) +{ + + double min; + double max; + find_minmax (numbers, min, max); + double bin_size = (max - min) / bin_count; + vector bins(bin_count); + for (size_t i = 0; i < numbers.size(); i++) + { + 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 <= numbers[i]) && (numbers[i] < hi)) + { + bins[j]++; + found = true; + } + } + if (!found) + { + bins[bin_count-1]++; + } + } + return bins; +} diff --git a/lab01.cbp b/lab01.cbp index 5f11f48..a390f01 100644 --- a/lab01.cbp +++ b/lab01.cbp @@ -32,7 +32,18 @@ + + + + + + + + + + + diff --git a/lab01.depend b/lab01.depend new file mode 100644 index 0000000..381bce7 --- /dev/null +++ b/lab01.depend @@ -0,0 +1,80 @@ +# depslib dependency file v1.0 +1745609797 source:c:\users\платон\desktop\project\lab01\main.cpp + + + "histogram.h" + "text.h" + +1745611606 source:c:\users\платон\desktop\project\lab01\historgam.cpp + + + "histogram.h" + +1745607609 c:\users\платон\desktop\project\lab01\histogram.h + + +1745609703 source:c:\users\платон\desktop\project\lab01\text.cpp + + + "text.h" + +1745609691 c:\users\платон\desktop\project\lab01\text.h + + +1745609785 source:c:\users\платон\desktop\project\lab01\unittest.cpp + "doctest.h" + "histogram_internal.h" + +1745609083 c:\users\платон\desktop\project\lab01\doctest.h + + + + + + + "doctest_fwd.h" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1745611606 c:\users\платон\desktop\project\lab01\histogram_internal.h + + +1746093073 source:c:\users\платон\desktop\project\lab01\histogram.cpp + + + "histogram.h" + diff --git a/lab01.layout b/lab01.layout new file mode 100644 index 0000000..0244e65 --- /dev/null +++ b/lab01.layout @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + diff --git a/main.cpp b/main.cpp index f42e42a..507cd57 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,7 @@ #include #include #include "histogram.h" +#include "text.h" using namespace std; @@ -26,45 +27,6 @@ input_data() { cin >> in.bin_count; return in; } - -void show_histogram_text(vector bins, size_t bin_count){ - const size_t SCREEN_WIDTH = 80; - const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; - - size_t max_bin = bins[0]; - for(size_t i = 0; i < bin_count; i++) - { - if(bins[i] > max_bin) - { - max_bin = bins[i]; - } - } - - for (size_t bin: bins) - { - size_t height = bin; - - if (max_bin > MAX_ASTERISK) - { - height = MAX_ASTERISK * (static_cast(bin) / max_bin); - } - - if (bin < 100) - { - cout << ' '; - } - if (bin < 10) - { - cout << ' '; - } - cout << bin << "|"; - for(size_t i = 0; i < height; i++) - { - cout << "*"; - } - cout << endl; - } -} int main() { auto in = input_data(); diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..03aa0c5 --- /dev/null +++ b/text.cpp @@ -0,0 +1,44 @@ +#include +#include +#include "text.h" + +using namespace std; + +void show_histogram_text(vector bins, size_t bin_count){ + const size_t SCREEN_WIDTH = 80; + const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + + size_t max_bin = bins[0]; + for(size_t i = 0; i < bin_count; i++) + { + if(bins[i] > max_bin) + { + max_bin = bins[i]; + } + } + + for (size_t bin: bins) + { + size_t height = bin; + + if (max_bin > MAX_ASTERISK) + { + height = MAX_ASTERISK * (static_cast(bin) / max_bin); + } + + if (bin < 100) + { + cout << ' '; + } + if (bin < 10) + { + cout << ' '; + } + cout << bin << "|"; + for(size_t i = 0; i < height; i++) + { + cout << "*"; + } + cout << endl; + } +} diff --git a/text.h b/text.h new file mode 100644 index 0000000..d73526b --- /dev/null +++ b/text.h @@ -0,0 +1,6 @@ +#ifndef TEXT_H_INCLUDED +#define TEXT_H_INCLUDED +#include +void show_histogram_text(std::vector bins, size_t bin_count); + +#endif // TEXT_H_INCLUDED diff --git a/unittest.cbp b/unittest.cbp new file mode 100644 index 0000000..7f9621b --- /dev/null +++ b/unittest.cbp @@ -0,0 +1,38 @@ + + + + + + diff --git a/unittest.cpp b/unittest.cpp new file mode 100644 index 0000000..26f2524 --- /dev/null +++ b/unittest.cpp @@ -0,0 +1,26 @@ +#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; + find_minmax({1, 2}, min, max); + CHECK(min == 1); + CHECK(max == 2); +} +TEST_CASE("distinct negative numbers"){ + double min = 0; + double max = 0; + find_minmax({-1, -2}, min, max); + CHECK(min == -2); + CHECK(max == -1); +} +TEST_CASE("vector of the same elements"){ + double min = 0; + double max = 0; + find_minmax({3,3,3}, min, max); + CHECK(min == 3); + CHECK(max == 3); +} diff --git a/unittest.depend b/unittest.depend new file mode 100644 index 0000000..e2d3ad4 --- /dev/null +++ b/unittest.depend @@ -0,0 +1,66 @@ +# depslib dependency file v1.0 +1745608562 source:c:\users\платон\desktop\project\lab01\historgam.cpp + + + "histogram.h" + +1745607609 c:\users\платон\desktop\project\lab01\histogram.h + + +1746092640 source:c:\users\платон\desktop\project\lab01\unittest.cpp + "doctest.h" + "histogram_internal.h" + +1745609083 c:\users\платон\desktop\project\lab01\doctest.h + + + + + + + "doctest_fwd.h" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1746092862 c:\users\платон\desktop\project\lab01\histogram_internal.h + + +1746092126 source:c:\users\платон\desktop\project\lab01\histogram.cpp + + + "histogram.h" +