From e713fc3cc0fdbfccc1f2feece5da00fa2e6469e1 Mon Sep 17 00:00:00 2001 From: "Arina (SokolAA)" Date: Wed, 28 May 2025 01:34:23 +0300 Subject: [PATCH] code: zashita + pravki --- doctest.h | 2 +- histogram.cpp | 19 ++++++++++--------- histogram_internal.h | 2 +- lab01.cbp | 8 ++++++++ main.cpp | 40 +++++++++++++++++++++++++++------------- svg.cpp | 7 +++---- svg.h | 6 +++--- unittest.cpp | 9 ++++++--- 8 files changed, 59 insertions(+), 34 deletions(-) diff --git a/doctest.h b/doctest.h index 34b40e6..826f0f5 100644 --- a/doctest.h +++ b/doctest.h @@ -2179,7 +2179,7 @@ int registerReporter(const char* name, int priority, bool isReporter) { } \ DOCTEST_INLINE_NOINLINE void der::f() // NOLINT(misc-definitions-in-headers) -#define DOCTEST_CREATE_AND_REGISTER_FUNCTION(f, decorators); \ +#define DOCTEST_CREATE_AND_REGISTER_FUNCTION(f, decorators) \ static void f(); \ DOCTEST_REGISTER_FUNCTION(DOCTEST_EMPTY, f, decorators) \ static void f() diff --git a/histogram.cpp b/histogram.cpp index fe78155..aaeab05 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -3,16 +3,17 @@ using std::vector; // Поиск минимума и максимума void find_minmax(const std::vector& numbers, double& min, double& max) { - min = numbers[0]; + if (numbers.empty()) + { + min = 0; + max = 0; + return; + } max = numbers[0]; - - for (double number : numbers) { - if (number < min) { - min = number; - } - if (number > max) { - max = number; - } + min = numbers[0]; + for (double x : numbers) { + if (x < min) min = x; + else if (x > max) max = x; } } diff --git a/histogram_internal.h b/histogram_internal.h index eacd4c3..743a766 100644 --- a/histogram_internal.h +++ b/histogram_internal.h @@ -2,6 +2,6 @@ #define HISTOGRAM_INTERNAL_H_INCLUDED #include using std::vector; -void find_minmax(const std::vector& numbers, double& min, double& max) +void find_minmax(const std::vector& numbers, double& min, double& max); #endif // HISTOGRAM_INTERNAL_H_INCLUDED diff --git a/lab01.cbp b/lab01.cbp index d541ad7..4a51ce2 100644 --- a/lab01.cbp +++ b/lab01.cbp @@ -32,7 +32,15 @@ + + + + + + + + diff --git a/main.cpp b/main.cpp index ff116cf..1a2425b 100644 --- a/main.cpp +++ b/main.cpp @@ -2,22 +2,22 @@ #include #include "histogram.h" #include "text.h" +#include "svg.h" using namespace std; struct Input { vector numbers; - size_t bin_count{}; + size_t bin_count; + size_t image_width; }; -// Ввод данных Input input_data() { + Input in; size_t number_count; cerr << "Enter number count: "; cin >> number_count; - Input in; in.numbers.resize(number_count); - cerr << "Enter numbers: "; for (size_t i = 0; i < number_count; i++) { cin >> in.numbers[i]; @@ -26,18 +26,32 @@ Input input_data() { cerr << "Enter bin count: "; cin >> in.bin_count; + const size_t BLOCK_WIDTH = 10; + const size_t MIN_WIDTH = 70; + const size_t MAX_WIDTH = 800; + size_t required_min_width = number_count * BLOCK_WIDTH / 3; + + while (true) { + cerr << "Enter image width (" << MIN_WIDTH << "-" << MAX_WIDTH << "), minimum " << required_min_width << ": "; + cin >> in.image_width; + + if (in.image_width < MIN_WIDTH) { + cerr << "Width is too small (minimum " << MIN_WIDTH << ")\n"; + } else if (in.image_width > MAX_WIDTH) { + cerr << "Width is too large (maximum " << MAX_WIDTH << ")\n"; + } else if (in.image_width < required_min_width) { + cerr << "Width is less than 1/3 of numbers count * block width (" << required_min_width << ")\n"; + } else { + break; + } + } + return in; } - - -// Вывод гистограммы - - -// Основная функция int main() { - Input in = input_data(); - vector bins = make_histogram(in.numbers, in.bin_count); - show_histogram_text(bins); + auto in = input_data(); + auto bins = make_histogram(in.numbers, in.bin_count); + show_histogram_svg(bins, in.image_width); return 0; } diff --git a/svg.cpp b/svg.cpp index f17f5a8..284d0aa 100644 --- a/svg.cpp +++ b/svg.cpp @@ -27,21 +27,20 @@ void svg_rect(double x, double y, double width, double height, string stroke = " << "' stroke='" << stroke << "' fill='" << fill << "' />\n"; } -void show_histogram_svg(const vector& bins) { +void show_histogram_svg(const vector& bins, size_t image_width) { if (bins.empty()) { cerr << "Error: Empty bins vector\n"; return; } - const auto IMAGE_WIDTH = 400; const auto IMAGE_HEIGHT = 300; const auto TEXT_LEFT = 20; const auto TEXT_BASELINE = 20; const auto TEXT_WIDTH = 50; const auto BIN_HEIGHT = 30; - const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH; + const auto MAX_WIDTH = image_width - TEXT_WIDTH; - svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); + svg_begin(image_width, IMAGE_HEIGHT); double top = 0; size_t max_count = bins[0]; diff --git a/svg.h b/svg.h index 44245d0..9709dea 100644 --- a/svg.h +++ b/svg.h @@ -2,8 +2,8 @@ #define SVG_H_INCLUDED #include -#include +#include // Для size_t -void show_histogram_svg(const std::vector& bins); +void show_histogram_svg(const std::vector& bins, size_t image_width); -#endif // SVG_H_INCLUDED +#endif diff --git a/unittest.cpp b/unittest.cpp index 9532d19..ed1a5f7 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -27,12 +27,15 @@ TEST_CASE("vector of the same elements"){ TEST_CASE("empty vector") { double min = 0; double max = 0; - CHECK_THROWS(find_minmax({}, min, max)); + find_minmax({}, min, max); + CHECK(min == 0); + CHECK(max == 0); } TEST_CASE("vector of one elements"){ double min = 0; double max = 0; - find_minmax({3}, min, max); - CHECK(min == max); + find_minmax({7}, min, max); + CHECK(min == 7); + CHECK(max == 7); }