From 48586b26501ff8258912193c8e6e8c6df96e10a2 Mon Sep 17 00:00:00 2001 From: "Andrew (ShabatovAA)" Date: Sun, 19 May 2024 17:58:04 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BF=D0=B0=D1=80=D0=B0=D0=BC=D0=B0=D1=82=D0=B5?= =?UTF-8?q?=D1=80=20promt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 31 +++++++++++++++++++++---------- histogram_internal.h | 2 +- main.cpp | 42 +++++++++++++++++++++++++++++------------- svg.cpp | 7 +++---- unittest.cpp | 33 ++++++++++++++++++++++++++++++++- 5 files changed, 86 insertions(+), 29 deletions(-) diff --git a/histogram.cpp b/histogram.cpp index 6aceb65..c6c178c 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -1,20 +1,31 @@ #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 x : numbers) { - if (x < min) { - min = x; - } - else if (x > max) { - max = x; + +bool +find_minmax(const std::vector& numbers, double& min, double& max){ + if (numbers.size() != 0) { + min = numbers[0]; + max = numbers[0]; + + for (double x : numbers) { + if (x < min) { + min = x; + } + else if (x > max) { + max = x; + } } + return true; + } + + else { + return false; } + } + vector make_histogram(const vector& numbers, size_t bin_count) { double min,max; diff --git a/histogram_internal.h b/histogram_internal.h index 5cd1715..ba8a1eb 100644 --- a/histogram_internal.h +++ b/histogram_internal.h @@ -1,6 +1,6 @@ #ifndef HISTOGRAM_INTERNAL_H_INCLUDED #define HISTOGRAM_INTERNAL_H_INCLUDED #include -void find_minmax(const std::vector& numbers, double& min, double& max); +bool find_minmax(const std::vector& numbers, double& min, double& max); #endif // HISTOGRAM_INTERNAL_H_INCLUDED diff --git a/main.cpp b/main.cpp index 72a5d83..532bb1e 100644 --- a/main.cpp +++ b/main.cpp @@ -10,28 +10,44 @@ struct Input{ size_t bin_count{}; }; -Input input_data() +Input input_data(istream& in, bool promt) { - size_t number_count; - cerr << "Enter number count: "; - cin >> number_count; + if (promt){ + size_t number_count; + cerr << "Enter number count: "; + in >> number_count; - Input in; + Input In; - in.numbers.resize(number_count); - cerr << "Enter numbers: "; - for(size_t i = 0; i < number_count; i++){ - cin >> in.numbers[i]; + In.numbers.resize(number_count); + cerr << "Enter numbers: "; + for(size_t i = 0; i < number_count; i++){ + in >> In.numbers[i]; + } + + cerr << "Enter bin count: "; + in >> In.bin_count; + return In; } + else{ + size_t number_count; + in >> number_count; + + Input In; - cerr << "Enter bin count: "; - cin >> in.bin_count; - return in; + In.numbers.resize(number_count); + for(size_t i = 0; i < number_count; i++){ + in >> In.numbers[i]; + } + + in >> In.bin_count; + return In; + } } int main() { - auto in = input_data(); + auto in = input_data(cin,false); auto bins = make_histogram(in.numbers, in.bin_count); show_histogram_svg(bins); return 0; diff --git a/svg.cpp b/svg.cpp index 301be4b..537ccad 100644 --- a/svg.cpp +++ b/svg.cpp @@ -2,7 +2,6 @@ #include #include #include -const size_t x=20; using namespace std; void @@ -35,7 +34,7 @@ void show_histogram_svg(const vector& bins) { const auto IMAGE_WIDTH = 400; const auto IMAGE_HEIGHT = 300; - const auto TEXT_LEFT = IMAGE_WIDTH-30; + const auto TEXT_LEFT = 20; const auto TEXT_BASELINE = 20; const auto TEXT_WIDTH = 50; const auto BIN_HEIGHT = 30; @@ -54,7 +53,7 @@ show_histogram_svg(const vector& bins) { for (size_t bin : bins) { const double bin_width = space * bin / max_count; svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); - svg_rect(TEXT_LEFT - bin_width - 20, top, bin_width, BIN_HEIGHT, "black", "blue"); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", "blue"); top += BIN_HEIGHT; } svg_end(); @@ -63,7 +62,7 @@ show_histogram_svg(const vector& bins) { for (size_t bin : bins) { const double bin_width = BLOCK_WIDTH * bin; svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); - svg_rect(TEXT_LEFT - bin_width - 20, top, bin_width, BIN_HEIGHT, "black", "blue"); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", "blue"); top += BIN_HEIGHT; } svg_end(); diff --git a/unittest.cpp b/unittest.cpp index e783068..9a85f56 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -4,9 +4,40 @@ #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("negative numbers") { double min = 0; double max = 0; find_minmax({-1, -2 , -5}, min, max); - CHECK(min == -3); + CHECK(min == -5); CHECK(max == -1); } + +TEST_CASE("one number") { + double min = 0; + double max = 0; + find_minmax({1}, min, max); + CHECK(min == 1); + CHECK(max == 1); +} + +TEST_CASE("equal elements") { + double min = 0; + double max = 0; + find_minmax({1,1,1,1}, min, max); + CHECK(min == 1); + CHECK(max == 1); +} + +TEST_CASE("zero vector") { + double min = 0; + double max = 0; + CHECK(!find_minmax({}, min, max)); +} +