From 4c35f2c8f5ecd4f1e74999c88409f23790032bf1 Mon Sep 17 00:00:00 2001 From: MordashovSA Date: Sat, 24 May 2025 17:07:52 +0300 Subject: [PATCH] code: lab4 --- main.cpp | 14 +++++++------- svg.cpp | 20 +++++++++----------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/main.cpp b/main.cpp index 72dcc4b..33be677 100644 --- a/main.cpp +++ b/main.cpp @@ -11,10 +11,10 @@ struct Input { size_t bin_count{}; }; -Input input_data() { +Input input_data(istream& in1) { size_t number_count; cerr << "Enter number count: "; - cin >> number_count; + in1 >> number_count; Input in; @@ -22,11 +22,11 @@ Input input_data() { for (size_t i = 0; i < number_count; i++) { - cin >> in.numbers[i]; + in1 >> in.numbers[i]; } cerr << "Enter number bin: "; - cin >> in.bin_count; + in1 >> in.bin_count; return in; } @@ -35,9 +35,9 @@ Input input_data() { int main() { - auto in = input_data(); + auto in = input_data(cin); auto bins = make_histogram(in.numbers, in.bin_count); - //show_histogram_text(bins, in.bin_count); - show_histogram_svg(bins); + show_histogram_text(bins, in.bin_count); + //show_histogram_svg(bins); return 0; } diff --git a/svg.cpp b/svg.cpp index ed5c15a..16670af 100644 --- a/svg.cpp +++ b/svg.cpp @@ -37,8 +37,8 @@ void show_histogram_svg(const vector& bins) const double IMAGE_HEIGHT = 300; const double TEXT_LEFT = 20; const double TEXT_BASELINE = 20; - const double TEXT_HEIGHT = 20; - const double BIN_WIDTH = 30; + const double TEXT_WIDTH = 50; + const double BIN_HEIGHT = 30; const double BLOCK_WIDTH = 10; svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); @@ -52,25 +52,23 @@ void show_histogram_svg(const vector& bins) } } - double max_height = IMAGE_HEIGHT - TEXT_HEIGHT - BLOCK_WIDTH; - - double left = TEXT_LEFT; - + double max_width = IMAGE_WIDTH - TEXT_WIDTH; + double top = 0; for (size_t count : bins) { double bin_width; if (max_count > 0) { - bin_height = static_cast(count) / max_count * max_height; + bin_width = static_cast(count) / max_count * max_width; } else { - bin_height = 0.0; + bin_width = 0.0; } - svg_text(left + BIN_WIDTH/2.0, IMAGE_HEIGHT - 5 ,to_string(count)); - svg_rect(left, IMAGE_HEIGHT - bin_height, BIN_WIDTH, bin_height, "blue", "#aaffaa"); - left += BIN_WIDTH + BLOCK_WIDTH ; + svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(count)); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "blue", "#aaffaa"); + top += BIN_HEIGHT; } svg_end(); }