From 4df9810e0c8f188653510fda530694a8e4c46dde Mon Sep 17 00:00:00 2001 From: Artem Date: Mon, 6 May 2024 15:00:09 +0300 Subject: [PATCH] code: refactoring --- histogram.cpp | 28 ++++++++++++++++++---------- histogram_internal.h | 3 +-- main.cpp | 25 ++++++++++++++----------- my_project.cbp | 4 ++++ svg.cpp | 10 +++++----- unittest.cbp | 5 +++++ unittest.cpp | 31 +++++++++++++++++++++++++++++++ 7 files changed, 78 insertions(+), 28 deletions(-) diff --git a/histogram.cpp b/histogram.cpp index 8d1d766..109a2f8 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -1,18 +1,26 @@ #include "histogram.h" -void +bool find_minmax(const std::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; + 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; + } + } diff --git a/histogram_internal.h b/histogram_internal.h index 7cfb60d..7d546c7 100644 --- a/histogram_internal.h +++ b/histogram_internal.h @@ -3,8 +3,7 @@ #include #include -void -find_minmax(const std::vector& numbers, double& min, double& max); +bool find_minmax(const std::vector& numbers, double& min, double& max); std::string bin_color(size_t bin, size_t max_count); diff --git a/main.cpp b/main.cpp index d74026d..9680804 100644 --- a/main.cpp +++ b/main.cpp @@ -11,29 +11,32 @@ using namespace std; }; Input -input_data(){ +input_data(istream& in, bool prompt){ + + if (prompt == true) cerr << "Enter number_count: "; size_t number_count; - cerr << "Enter number_count: "; - cin >> number_count; + in >> number_count; + + Input data; + data.numbers.resize(number_count); + + if (prompt == true) cerr << "Enter numbers: "; - Input in; - in.numbers.resize(number_count); - cerr << "Enter numbers: "; for (size_t i = 0; i < number_count; i++){ - cin >> in.numbers[i]; + in >> data.numbers[i]; } - cerr << "Enter bin count: "; - cin >> in.bin_count; + if (prompt == true) cerr << "Enter bin count: "; - return in; + in >> data.bin_count; + return data; } 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/my_project.cbp b/my_project.cbp index 86dd073..f72deb7 100644 --- a/my_project.cbp +++ b/my_project.cbp @@ -39,6 +39,10 @@ + + + diff --git a/svg.cpp b/svg.cpp index 89fe2fe..3897a52 100644 --- a/svg.cpp +++ b/svg.cpp @@ -28,9 +28,9 @@ svg_rect(double x, double y, double width, double height, string stroke, string cout << ""; } -string bin_color(size_t bin, size_t max_count){ - return ("#" + to_string(10 - (bin * 9) / max_count) + to_string(10 - (bin * 9) / max_count) + to_string(10 - (bin * 9) / max_count)); -} +//string bin_color(size_t bin, size_t max_count){ +// return ("#" + to_string(10 - (bin * 9) / max_count) + to_string(10 - (bin * 9) / max_count) + to_string(10 - (bin * 9) / max_count)); +//} void show_histogram_svg(const vector& bins) { @@ -53,9 +53,9 @@ show_histogram_svg(const vector& bins) { double top = 0; for (size_t bin : bins) { const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH) * bin / max_count; - const auto color = bin_color(bin,max_count); + //const auto color = bin_color(bin,max_count); svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); - svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT,"grey", color); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT,"red", "#ffeeee"); top += BIN_HEIGHT; } svg_end(); diff --git a/unittest.cbp b/unittest.cbp index 7f9621b..9085664 100644 --- a/unittest.cbp +++ b/unittest.cbp @@ -31,6 +31,11 @@ + + + + + diff --git a/unittest.cpp b/unittest.cpp index dbb684b..d7cb394 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -13,6 +13,37 @@ TEST_CASE("distinct positive numbers") { CHECK(max == 2); } +TEST_CASE("one element vector") { + double min = 0; + double max = 0; + find_minmax({1}, min, max); + CHECK(min == max); +} + +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("identical elements vector") { + double min = 0; + double max = 0; + find_minmax({3,3,3,3}, min, max); + CHECK(min == 3); + CHECK(max == 3); +} + +TEST_CASE("empty vector") { + double min = 0; + double max = 0; + bool result = find_minmax({}, min, max); + CHECK(result == false); +} + TEST_CASE("bin color") { size_t max_bin = 10; size_t min_bin = 3;