From 3ba173bddce564f103c4f8cc032a04be5812d63e Mon Sep 17 00:00:00 2001 From: SukhotinMD <SukhotinMD@mpei.ru> Date: Thu, 29 May 2025 10:47:33 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=9F=D0=B5=D1=80=D0=B2=D1=8B=D0=B5=20?= =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D1=81?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=BE=D0=B9=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ARM64/Debug/marks.svg | 11 +++++++---- ProgUit Lab1/ProgUit Lab1.cpp | 10 +++++----- ProgUit Lab1/histogram_internal.cpp | 9 +++++++-- ProgUit Lab1/histogram_internal.h | 2 +- ProgUit Lab1/text.cpp | 14 +++++++++++--- ProgUit Lab1/unittest.cpp | 14 +++++++------- 6 files changed, 38 insertions(+), 22 deletions(-) diff --git a/ARM64/Debug/marks.svg b/ARM64/Debug/marks.svg index bfe9665..14e47e9 100644 --- a/ARM64/Debug/marks.svg +++ b/ARM64/Debug/marks.svg @@ -1,9 +1,12 @@ <?xml version='1.0' encoding='UTF-8'?> -<svg width='400' height='300' viewBox='0 0 400 300' xmlns='http://www.w3.org/2000/svg'> +<svg width='600' height='400' viewBox='0 0 600 400' xmlns='http://www.w3.org/2000/svg'> <text x='20' y='20'>2</text> -<rect x='50' y='0' width='140' height='30' stroke='grey' fill='#e3c3c7' /> +<rect x='50' y='0' width='140' height='30' stroke='grey' fill='#6c6835' /> +<text x='450' y='20'>20%</text> <text x='20' y='50'>5</text> -<rect x='50' y='30' width='350' height='30' stroke='grey' fill='#9efffa' /> +<rect x='50' y='30' width='350' height='30' stroke='grey' fill='#dee670' /> +<text x='450' y='50'>50%</text> <text x='20' y='80'>3</text> -<rect x='50' y='60' width='210' height='30' stroke='grey' fill='#6f092f' /> +<rect x='50' y='60' width='210' height='30' stroke='grey' fill='#6aa11b' /> +<text x='450' y='80'>30%</text> </svg> diff --git a/ProgUit Lab1/ProgUit Lab1.cpp b/ProgUit Lab1/ProgUit Lab1.cpp index b632428..8d1a0bc 100644 --- a/ProgUit Lab1/ProgUit Lab1.cpp +++ b/ProgUit Lab1/ProgUit Lab1.cpp @@ -17,7 +17,7 @@ struct Input { Input -input_data() { +input_data(istream& hin) { // Функция ввода //Создание переменных @@ -27,19 +27,19 @@ input_data() { // Ввод переменных cerr << "Enter number count: " << endl; - cin >> number_count; + hin >> number_count; in.numbers.resize(number_count); cerr << "Enter numbers: " << endl; for (int i = 0; i < number_count; i++) { - cin >> in.numbers[i]; + hin >> in.numbers[i]; } cerr << "Enter bin count: " << endl; - cin >> in.bin_count; + hin >> in.bin_count; return in; } @@ -52,7 +52,7 @@ input_data() { int main() { // Функция main - auto in = input_data(); // Ввод структуры + auto in = input_data(cin); // Ввод структуры auto bins = make_histogram(in.numbers, in.bin_count); // Распределние по корзинам show_histogram_svg(bins); // Вывод графика diff --git a/ProgUit Lab1/histogram_internal.cpp b/ProgUit Lab1/histogram_internal.cpp index 6fdbaed..12c9d04 100644 --- a/ProgUit Lab1/histogram_internal.cpp +++ b/ProgUit Lab1/histogram_internal.cpp @@ -4,10 +4,15 @@ #include "histogram_internal.h" using namespace std; +bool find_minmax(const vector<double>& numbers, double& min_in_numbers, double& max_in_numbers) { + + if (numbers.empty()) { + return false; + } -void find_minmax(const vector<double>& numbers, double& min_in_numbers, double& max_in_numbers) { - min_in_numbers = numbers[0]; max_in_numbers = *(max_element(begin(numbers), end(numbers))); min_in_numbers = *(min_element(begin(numbers), end(numbers))); + + return true; } diff --git a/ProgUit Lab1/histogram_internal.h b/ProgUit Lab1/histogram_internal.h index a29a456..fa95272 100644 --- a/ProgUit Lab1/histogram_internal.h +++ b/ProgUit Lab1/histogram_internal.h @@ -3,6 +3,6 @@ #include <vector> -void find_minmax(const std::vector<double>& numbers, double& min_in_numbers, double& max_in_numbers); +bool find_minmax(const std::vector<double>& numbers, double& min_in_numbers, double& max_in_numbers); #endif //INTERNAL_H_INCLUDED diff --git a/ProgUit Lab1/text.cpp b/ProgUit Lab1/text.cpp index 3aeb1ec..c15d670 100644 --- a/ProgUit Lab1/text.cpp +++ b/ProgUit Lab1/text.cpp @@ -65,22 +65,30 @@ show_histogram_svg(const vector<size_t> bins) { const auto TEXT_BASELINE = 20; const auto TEXT_WIDTH = 50; const auto BIN_HEIGHT = 30; + const auto PROCENT_SPACE = 50; auto BLOCK_WIDTH = 10; + auto sum_bins = 0; BLOCK_WIDTH = (IMAGE_WIDTH - TEXT_WIDTH) / mm; - svg_begin(400, 300); + svg_begin(600, 400); - + for (size_t Elem : bins) { + sum_bins += Elem; + } double top = 0; for (size_t bin : bins) { const double bin_width = BLOCK_WIDTH * bin; - + double PROCENT = double(bin) / double(sum_bins); + PROCENT = round(PROCENT * 100) / 100 ; svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "grey", getRandomHexColor()); + + svg_text(IMAGE_WIDTH + PROCENT_SPACE, top + TEXT_BASELINE, to_string(int(PROCENT * 100)) + "%"); top += BIN_HEIGHT; + } diff --git a/ProgUit Lab1/unittest.cpp b/ProgUit Lab1/unittest.cpp index 079375d..ed21793 100644 --- a/ProgUit Lab1/unittest.cpp +++ b/ProgUit Lab1/unittest.cpp @@ -15,28 +15,28 @@ TEST_CASE("distinct positive numbers") { TEST_CASE("empty vector") { double min = 0; double max = 0; - find_minmax({}, min, max); - CHECK(min == 1); - CHECK(max == 2); + CHECK(!find_minmax({}, min, max)); } + + TEST_CASE("you fill so lonly") { double min = 0; double max = 0; find_minmax({ 1 }, min, max); CHECK(min == 1); - CHECK(max == 2); + CHECK(max == 1); } TEST_CASE("negative numbers") { double min = 0; double max = 0; find_minmax({ -1, -2 }, min, max); - CHECK(min == 1); - CHECK(max == 2); + CHECK(min == -2); + CHECK(max == -1); } TEST_CASE("twins") { double min = 0; double max = 0; find_minmax({ 1, 1 }, min, max); CHECK(min == 1); - CHECK(max == 2); + CHECK(max == 1); } \ No newline at end of file