From 4eb9fb549be342b6117da382d77a7f6ebd6ea2ef Mon Sep 17 00:00:00 2001 From: EvdochenkoNV Date: Sat, 24 May 2025 16:19:38 +0300 Subject: [PATCH] =?UTF-8?q?code:=20cin=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BD=D0=B0=20in,=20=D0=B2=D0=BE=D0=B7=D0=B2?= =?UTF-8?q?=D1=80=D0=B0=D1=82=20=D0=BA=20=D0=B4=D0=BE=D0=B7=D0=B0=D1=89?= =?UTF-8?q?=D0=B8=D1=82=D0=BD=D0=BE=D0=BC=D1=83=20=D0=B2=D0=B0=D1=80=D0=B8?= =?UTF-8?q?=D0=B0=D0=BD=D1=82=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + histogram.cpp | 7 ------- histogram.h | 2 -- histogram_internal.h | 2 +- main.cpp | 35 ++++++++----------------------- svg.cpp | 49 +++++++++++++++++++++++++++++--------------- svg.h | 4 ++-- text.h | 1 - 8 files changed, 45 insertions(+), 56 deletions(-) diff --git a/.gitignore b/.gitignore index 093f6b6..af0feb5 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /cs-lab34.layout /main.exe /main.o +/unittest.layout diff --git a/histogram.cpp b/histogram.cpp index c8330f7..46c6086 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -18,13 +18,6 @@ bool find_minmax(vector vec, double& min, double& max) { } return true; } - -bool width(size_t block_width){ - if (block_width > 3 && block_width<30 ) { - return true; - } -} - vector make_histogram(size_t number, vector vec) { vector bins(number); double mn, mx; diff --git a/histogram.h b/histogram.h index 4267de0..2b63e9a 100644 --- a/histogram.h +++ b/histogram.h @@ -3,5 +3,3 @@ using namespace std; vector make_histogram(size_t number, vector vec); - -bool width(size_t block_width); diff --git a/histogram_internal.h b/histogram_internal.h index 3d4f536..fd3bef4 100644 --- a/histogram_internal.h +++ b/histogram_internal.h @@ -1,2 +1,2 @@ bool find_minmax(std::vector vec, double& min, double& max); -bool width(size_t block_width); +// diff --git a/main.cpp b/main.cpp index 57e7695..0672980 100644 --- a/main.cpp +++ b/main.cpp @@ -5,41 +5,24 @@ struct Input { vector vec; size_t korz{}; - size_t block_width{}; }; -Input input_data() { - Input in; +Input input_data(istream& in) { + Input lin; size_t n, korz; cerr << "Number of elements: "; - cin >> n; - in.vec.resize(n); - cerr << "Elements: "; + in >> n; + lin.vec.resize(n); for (size_t i = 0; i < n; i++) - cin >> in.vec[i]; + in >> lin.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; + in >> lin.korz; + return lin; } - int main() { - auto in = input_data(); + auto in = input_data(cin); auto bins = make_histogram(in.korz, in.vec); - show_histogram_svg(bins, in.block_width); + show_histogram_svg(bins); } diff --git a/svg.cpp b/svg.cpp index f79d6f7..dcbc4a8 100644 --- a/svg.cpp +++ b/svg.cpp @@ -1,8 +1,10 @@ #include "svg.h" +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, string fill) { +void +svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black") +{ cout << ""; + } -void show_histogram_svg(const vector& bins, size_t block_width) { + + +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 BLACK = "black"; + const auto BLOCK_WIDTH = 10; + const auto GREEN = "green"; 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.empty() ? 0 : bins[0]; - for (size_t i = 1; i < bins.size(); i++) { - if (bins[i] > max_count) { + double max_count = bins[0]; + for (size_t i = 0; i < bins.size(); i++) + { + if (max_count < bins[i]) + { max_count = bins[i]; } } - for (size_t bin : bins) { - double bin_width = static_cast(block_width) * bin; - if (bin_width > MAX_WIDTH) { - bin_width = MAX_WIDTH; - } + for (size_t bin : bins) + { + double bin_width = (MAX_WIDTH) * (bin/max_count); svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); - svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, BLACK, RED); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, GREEN, RED); top += BIN_HEIGHT; } diff --git a/svg.h b/svg.h index 6f8197f..fc0aa19 100644 --- a/svg.h +++ b/svg.h @@ -2,6 +2,6 @@ #include #include #include -using namespace std; -void show_histogram_svg(const vector& bins, size_t block_width); +using namespace std; +void show_histogram_svg(const vector& bins); diff --git a/text.h b/text.h index abc07c0..9db8b30 100644 --- a/text.h +++ b/text.h @@ -2,4 +2,3 @@ #include void show_histogram(std::vector bins); -