From b65861bb2320245d21c25b9d65996ca652b6b24a Mon Sep 17 00:00:00 2001 From: artemzelenov Date: Mon, 22 May 2023 14:01:08 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=B7=D0=B0=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BF=D0=BE=D1=82=D0=BE=D0=BA=20cin=20=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=B2=D1=8B=D0=B1=D0=BE=D1=80=D0=BE=D1=87=D0=BD=D1=8B=D0=B9=20?= =?UTF-8?q?=D0=BF=D0=BE=D1=82=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- project/histogram.cpp | 70 +++++++++++++++++++++++++++---------------- project/main.cpp | 18 ++++++----- project/project.cbp | 24 +++++++++++++++ project/svg.cpp | 6 ++-- unittest.cpp | 40 +++++++++++++++++++------ 5 files changed, 114 insertions(+), 44 deletions(-) diff --git a/project/histogram.cpp b/project/histogram.cpp index a73e02e..68ddf1e 100644 --- a/project/histogram.cpp +++ b/project/histogram.cpp @@ -2,42 +2,62 @@ #include #include "histogram.h" using namespace std; -void -find_minmax(vector numbers, double& min, double& max) { - min = numbers[0]; - max = numbers[0]; - for (double x : numbers) { - if (x < min) { - min = x; +bool +find_minmax(vector numbers, double& min, double& max) +{ + bool check; + if(numbers.size()==0) + { + + check = false; } - else if (x > max) { - max = x; + else + { + min = numbers[0]; + max = numbers[0]; + for (double x : numbers) + { + if (x < min) + { + min = x; + } + else if (x > max) + { + max = x; + } + } } -} + return check; } vector -make_histogram(vector numbers, size_t bin_count) { +make_histogram(vector numbers, size_t bin_count) +{ vector bins(bin_count); double min, max; - find_minmax(numbers, min, max); + bool check; + check = find_minmax(numbers, min, max); size_t countt; double bin_size = (max - min) / 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; + 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]++; } } - if (!found) { - bins[bin_count - 1]++; - } - } - return bins; + return bins; } diff --git a/project/main.cpp b/project/main.cpp index d67f5e7..717dfb8 100644 --- a/project/main.cpp +++ b/project/main.cpp @@ -3,29 +3,33 @@ #include "histogram.h" #include "text.h" #include "svg.h" +#include "emptiness.h" using namespace std; -struct Input { +struct Input +{ vector numbers; size_t bin_count{}; }; Input -input_data(){ +input_data(istream& tin) +{ size_t number_count; cerr << "Enter number count: "; - cin >> number_count; + tin >> number_count; Input in; in.numbers.resize(number_count); - for(int i;i> in.numbers[i]; + tin >> in.numbers[i]; } cerr << "Enter bin count: "; - cin >> in.bin_count; + tin >> in.bin_count; return in; } int main() -{ Input in = input_data(); +{ + Input in = input_data(cin); auto bins = make_histogram(in.numbers, in.bin_count); show_histogram_svg(bins); return 0; diff --git a/project/project.cbp b/project/project.cbp index c4697a9..2d966f4 100644 --- a/project/project.cbp +++ b/project/project.cbp @@ -32,7 +32,31 @@ + + + + + + + + + + + + + + + + + + diff --git a/project/svg.cpp b/project/svg.cpp index 5437a34..57110ae 100644 --- a/project/svg.cpp +++ b/project/svg.cpp @@ -4,6 +4,7 @@ #include #include #include "svg.h" +#include "emptiness.h" using namespace std; void @@ -41,7 +42,7 @@ 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; + const double MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH; svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT); @@ -54,8 +55,7 @@ double max_count = bins[0]; } for (size_t bin : bins) { const double bin_width = MAX_WIDTH * (bin/max_count); - const double emptiness_width = MAX_WIDTH - bin_width; - svg_rect(0, top, emptiness_width, BIN_HEIGHT, "white", "#ffffff"); + double emptiness_width = emptiness(MAX_WIDTH,bin_width); svg_text(TEXT_LEFT + MAX_WIDTH, top + TEXT_BASELINE, to_string(bin)); svg_rect(emptiness_width, top, bin_width, BIN_HEIGHT, "red", "#aaffaa"); top += BIN_HEIGHT; diff --git a/unittest.cpp b/unittest.cpp index 2cbf15c..bd8811b 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -1,16 +1,38 @@ #define DOCTEST_CONFIG_NO_MULTITHREADING #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #include "doctest.h" +#include "project\emptiness.h" #include "project\histogram_internal.h" -TEST_CASE("distinct positive numbers") { +TEST_CASE("difference check") { + double a = 20; + double b = 10; + double c = emptiness(a,b); + CHECK(c == 10); + CHECK(c > 0); + } +TEST_CASE("more then 0 check") { + double a = 20; + double b = 10; + double c = emptiness(a,b); + CHECK(c > 0); + } +TEST_CASE("less then 0 check") { + double a = 10; + double b = 20; + double c = emptiness(a,b); + CHECK(c < 0); + } +TEST_CASE("not 0 check") { + double a = 20; + double b = 10; + double c = emptiness(a,b); + CHECK(c != 0); + } +TEST_CASE("is massif full") { double min = 0; double max = 0; - std::vectorv{2,1}; - CHECK(v.size() != 0); - CHECK(v.size() != 1); - find_minmax({1, 2}, min, max); - CHECK(min == 1); - CHECK(max == 2); - CHECK(min != max); -} + std::vectorv{}; + bool c = find_minmax(v,min,max); + CHECK(c == false); + }