From ca853cef182a729803aab5db76a7c5e6b7fc9fbb Mon Sep 17 00:00:00 2001 From: EvdochenkoNV Date: Mon, 5 May 2025 14:30:39 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=B7=D0=B0=D1=89=D0=B8=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cs-lab34.cbp | 8 ++++++++ histogram.h | 1 + main.cpp | 22 ++++++++++++++++++--- svg.cpp | 47 ++++++++++++++++----------------------------- svg.h | 4 ++-- text.h | 1 + unittest.cbp | 4 ++++ unittest.depend | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 102 insertions(+), 36 deletions(-) diff --git a/cs-lab34.cbp b/cs-lab34.cbp index 49b0653..7cb92d9 100644 --- a/cs-lab34.cbp +++ b/cs-lab34.cbp @@ -32,7 +32,15 @@ + + + + + + + + diff --git a/histogram.h b/histogram.h index 2b63e9a..92a9d68 100644 --- a/histogram.h +++ b/histogram.h @@ -3,3 +3,4 @@ using namespace std; vector make_histogram(size_t number, vector vec); + diff --git a/main.cpp b/main.cpp index 81f0456..70445d7 100644 --- a/main.cpp +++ b/main.cpp @@ -5,24 +5,40 @@ struct Input { vector vec; size_t korz{}; + size_t block_width{}; }; + Input input_data() { Input in; size_t n, korz; - cerr << "Enter the number of elements: "; + cerr << "Number of elements: "; cin >> n; in.vec.resize(n); - cerr << "Enter elements: "; + cerr << "Elements: "; for (size_t i = 0; i < n; i++) cin >> in.vec[i]; cerr << "Enter bin count: "; cin >> in.korz; + + + cerr << "Enter block width in px (3-30): "; + cin >> in.block_width; + + while (in.block_width < 3 || in.block_width > 30) { + if (in.block_width < 3) { + cerr << "Block width too small! Minimum is 3px. Please enter again: "; + } else { + cerr << "Block width too large! Maximum is 30px. Please enter again: "; + } + cin >> in.block_width; + } + return in; } int main() { auto in = input_data(); auto bins = make_histogram(in.korz, in.vec); - show_histogram_svg(bins); + show_histogram_svg(bins, in.block_width); } diff --git a/svg.cpp b/svg.cpp index c1c2240..a697b75 100644 --- a/svg.cpp +++ b/svg.cpp @@ -2,9 +2,7 @@ using namespace std; -void -svg_begin(double width, double height) -{ +void svg_begin(double width, double height) { cout << "\n"; cout << "\n"; } -void -svg_end() -{ +void svg_end() { cout << "\n"; } -void -svg_text(double left, double baseline, string text) -{ +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") -{ +void svg_rect(double x, double y, double width, double height, string stroke, string fill) { cout << ""; - } - - -void -show_histogram_svg(const vector& bins) -{ +void show_histogram_svg(const vector& bins, size_t block_width) { 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 BLOCK_WIDTH = 10; const auto BLACK = "black"; - const auto PURPLE = "purple"; + const auto RED = "red"; 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 < bins[i]) - { + double max_count = bins.empty() ? 0 : bins[0]; + for (size_t i = 1; i < bins.size(); i++) { + if (bins[i] > max_count) { max_count = bins[i]; } } - for (size_t bin : bins) - { - double bin_width = (MAX_WIDTH) * (bin/max_count); + for (size_t bin : bins) { + double bin_width = static_cast(block_width) * bin; + if (bin_width > MAX_WIDTH) { + bin_width = MAX_WIDTH; + } svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); - svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, BLACK, PURPLE); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, BLACK, RED); top += BIN_HEIGHT; } diff --git a/svg.h b/svg.h index fc0aa19..6f8197f 100644 --- a/svg.h +++ b/svg.h @@ -2,6 +2,6 @@ #include #include #include - using namespace std; -void show_histogram_svg(const vector& bins); + +void show_histogram_svg(const vector& bins, size_t block_width); diff --git a/text.h b/text.h index 9db8b30..abc07c0 100644 --- a/text.h +++ b/text.h @@ -2,3 +2,4 @@ #include void show_histogram(std::vector bins); + diff --git a/unittest.cbp b/unittest.cbp index 7f9621b..5a9c8c0 100644 --- a/unittest.cbp +++ b/unittest.cbp @@ -31,6 +31,10 @@ + + + + diff --git a/unittest.depend b/unittest.depend index f1f075b..914d5b4 100644 --- a/unittest.depend +++ b/unittest.depend @@ -6,3 +6,54 @@ +1746276122 source:c:\users\natasha\desktop\Проги С2\cs-lab34\unittest.cpp + "doctest.h" + "histogram_internal.h" + +1746276122 c:\users\natasha\desktop\Проги С2\cs-lab34\doctest.h + + + + + + + "doctest_fwd.h" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1746276025 c:\users\natasha\desktop\Проги С2\cs-lab34\histogram_internal.h +