From d11a01d781509dfdd09c87d5f90376346c6539d7 Mon Sep 17 00:00:00 2001 From: KalenskovaDA Date: Wed, 11 Jun 2025 11:11:17 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=203=20=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 22 +++++++++------- laba1.depend | 36 ++++++++++++++++++++++++++ main.cpp | 23 +++++------------ svg.cpp | 69 +++++++++++++++++++++---------------------------- unittest.cpp | 8 +++++- unittest.depend | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 159 insertions(+), 67 deletions(-) diff --git a/histogram.cpp b/histogram.cpp index 25caf89..592f288 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -2,19 +2,21 @@ #include "histogram_internal.h" #include using std::vector; -void find_minmax(const std::vector& numbers, double& min, double& max) { - min = numbers[0]; +void find_minmax(const vector& numbers, double& min, double& max) { + 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; } } + std::vector make_histogram(const std::vector& numbers, size_t bin_count) { std::vector bins(bin_count, 0); diff --git a/laba1.depend b/laba1.depend index c01c32e..6748395 100644 --- a/laba1.depend +++ b/laba1.depend @@ -78,3 +78,39 @@ "histogram_internal.h" +1748875465 source:c:\users\aleks\onedrive\Рабочий стол\laba1 — копияbb\histogram.cpp + "histogram.h" + "histogram_internal.h" + + +1748386910 c:\users\aleks\onedrive\Рабочий стол\laba1 — копияbb\histogram.h + + +1748392232 c:\users\aleks\onedrive\Рабочий стол\laba1 — копияbb\histogram_internal.h + + +1749627705 source:c:\users\aleks\onedrive\Рабочий стол\laba1 — копияbb\main.cpp + + + + "histogram.h" + "text.h" + "svg.h" + "histogram_internal.h" + +1748730472 c:\users\aleks\onedrive\Рабочий стол\laba1 — копияbb\text.h + + +1749627429 c:\users\aleks\onedrive\Рабочий стол\laba1 — копияbb\svg.h + + + +1748808306 source:c:\users\aleks\onedrive\Рабочий стол\laba1 — копияbb\text.cpp + "text.h" + + + +1748876656 source:c:\users\aleks\onedrive\Рабочий стол\laba1 — копияbb\svg.cpp + "svg.h" + + diff --git a/main.cpp b/main.cpp index 1eabfad..23e7f85 100644 --- a/main.cpp +++ b/main.cpp @@ -1,24 +1,25 @@ #include #include +#include #include "histogram.h" #include "text.h" #include "svg.h" +#include "histogram_internal.h" using namespace std; - - struct Input { - std::vector numbers; + vector numbers; + size_t bin_count; }; -// Ввод данных + Input input_data() { Input in; size_t number_count; + cerr << "Enter number count: "; cin >> number_count; - in.numbers.resize(number_count); cerr << "Enter numbers: "; @@ -27,21 +28,11 @@ Input input_data() { } cerr << "Enter bin count: "; - cin >> in.bin_count; +cin >> in.bin_count; return in; } -// Поиск минимума и максимума - - -// Расчёт гистограммы - - -// Вывод гистограммы - - -// Основная функция int main() { auto in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); diff --git a/svg.cpp b/svg.cpp index a3c3f71..21b455e 100644 --- a/svg.cpp +++ b/svg.cpp @@ -1,66 +1,55 @@ #include "svg.h" -#include +#include using namespace std; -void -svg_begin(double width, double height) { + +void svg_begin(double width, double height) { cout << "\n"; - cout << "\n"; + cout << "\n"; + + // Стили для SVG + cout << "\n"; } -void -svg_end() { +void svg_end() { cout << "\n"; } -void -svg_text(double left, double baseline, string text) -{ - cout << "" << text << ""; -} - -void -svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black") -{ - cout << ""; +void svg_text(double left, double baseline, string text) { + cout << "" + << text << ""; } +void svg_rect(double x, double y, double width, double height) { + cout << ""; +} - -void -show_histogram_svg(const vector& bins) -{ +void show_histogram_svg(const vector& bins) { 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 YELLOW = "yellow"; - const auto PURPLE = "purple"; - 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; - double max_count = bins[0]; - for (size_t i = 0; i < bins.size(); i++) - { - if (max_count max_count) max_count = count; } - for (size_t bin : bins) - { - double bin_width = (MAX_WIDTH)*(bin/max_count); + for (size_t bin : bins) { + const double bin_width = MAX_WIDTH * (static_cast(bin) / max_count); svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); - svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, YELLOW, PURPLE); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT); top += BIN_HEIGHT; } diff --git a/unittest.cpp b/unittest.cpp index 37e5da4..bc923dd 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -53,4 +53,10 @@ TEST_CASE("vector with zero") { CHECK(min == -1); CHECK(max == 1); } - +TEST_CASE("empty vector") { + double min = 0; + double max = 0; + find_minmax({}, min, max); + CHECK(min == 0); + CHECK(max == 0); +} diff --git a/unittest.depend b/unittest.depend index f345164..f7178a6 100644 --- a/unittest.depend +++ b/unittest.depend @@ -66,3 +66,71 @@ +1748875465 source:c:\users\aleks\onedrive\Рабочий стол\laba1 — копияbb\histogram.cpp + "histogram.h" + "histogram_internal.h" + + +1748386910 c:\users\aleks\onedrive\Рабочий стол\laba1 — копияbb\histogram.h + + +1748392232 c:\users\aleks\onedrive\Рабочий стол\laba1 — копияbb\histogram_internal.h + + +1748876656 source:c:\users\aleks\onedrive\Рабочий стол\laba1 — копияbb\svg.cpp + "svg.h" + + +1748810901 c:\users\aleks\onedrive\Рабочий стол\laba1 — копияbb\svg.h + + + +1748875527 source:c:\users\aleks\onedrive\Рабочий стол\laba1 — копияbb\unittest.cpp + "doctest.h" + "histogram_internal.h" + +1748392747 c:\users\aleks\onedrive\Рабочий стол\laba1 — копияbb\doctest.h + + + + + + + "doctest_fwd.h" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +