diff --git a/histogram.cpp b/histogram.cpp index 4d98aed..247d6d1 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -3,23 +3,17 @@ #include "histogram.h" using namespace std; -void find_minmax(const vector &numbers, double &min, double &max) -{ +bool find_minmax(const vector& numbers, double& min, double& max) { + if (numbers.empty()) { + return false; + } min = numbers[0]; max = numbers[0]; - for(double number : numbers) - { - if(number < min) - { - min = number; - } - if(number > max) - { - max = number; - } + for (double number : numbers) { + if (number < min) min = number; + if (number > max) max = number; } - return; - + return true; } std::vector make_histogram(std::vector& numbers, size_t bin_count) diff --git a/histogram_internal.h b/histogram_internal.h index 8e35594..d7cbb71 100644 --- a/histogram_internal.h +++ b/histogram_internal.h @@ -1,6 +1,6 @@ #ifndef HISTOGRAM_INTERNAL_H_INCLUDED #define HISTOGRAM_INTERNAL_H_INCLUDED #include -void find_minmax(const std::vector &numbers, double &min, double &max); +bool find_minmax(const std::vector &numbers, double &min, double &max); #endif // HISTOGRAM_INTERNAL_H_INCLUDED diff --git a/historgam.cpp b/historgam.cpp index 4d98aed..247d6d1 100644 --- a/historgam.cpp +++ b/historgam.cpp @@ -3,23 +3,17 @@ #include "histogram.h" using namespace std; -void find_minmax(const vector &numbers, double &min, double &max) -{ +bool find_minmax(const vector& numbers, double& min, double& max) { + if (numbers.empty()) { + return false; + } min = numbers[0]; max = numbers[0]; - for(double number : numbers) - { - if(number < min) - { - min = number; - } - if(number > max) - { - max = number; - } + for (double number : numbers) { + if (number < min) min = number; + if (number > max) max = number; } - return; - + return true; } std::vector make_histogram(std::vector& numbers, size_t bin_count) diff --git a/lab01.depend b/lab01.depend index 9be2205..a1c8304 100644 --- a/lab01.depend +++ b/lab01.depend @@ -74,7 +74,7 @@ 1745611606 c:\users\платон\desktop\project\lab01\histogram_internal.h -1746093073 source:c:\users\платон\desktop\project\lab01\histogram.cpp +1746450679 source:c:\users\платон\desktop\project\lab01\histogram.cpp "histogram.h" diff --git a/lab01.layout b/lab01.layout index 06c2d6a..7a7a092 100644 --- a/lab01.layout +++ b/lab01.layout @@ -2,24 +2,24 @@ - + - + - + - + - + - + - + diff --git a/main.cpp b/main.cpp index 57e57b6..57c9136 100644 --- a/main.cpp +++ b/main.cpp @@ -9,11 +9,12 @@ using namespace std; struct Input { vector numbers; size_t bin_count{}; + size_t bin_height{};//добавил переменную }; Input input_data() { - size_t number_count, bin_count; + size_t number_count, bin_count, bin_height; cerr << "Enter number count: "; cin >> number_count; Input in; @@ -25,13 +26,15 @@ input_data() { } cerr << "Enter bin count: "; cin >> in.bin_count; + cerr << "Enter bin height: ";//добавил ввод + cin >> in.bin_height; return in; } int main() { auto in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); - show_histogram_svg(bins); + show_histogram_svg(bins, in.bin_height);//добавил bin_height как параметр return 0; } diff --git a/svg.cpp b/svg.cpp index 2cc6d1d..0a5cd9c 100644 --- a/svg.cpp +++ b/svg.cpp @@ -1,14 +1,13 @@ #include #include -#include #include #include #include "svg.h" +#include 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 = "black", string fill = "black") { cout << ""; } - - -void -show_histogram_svg(const vector& bins) +void show_histogram_svg(const vector& bins, size_t bin_height) { const auto IMAGE_WIDTH = 400; - const auto IMAGE_HEIGHT = 300; + const auto IMAGE_HEIGHT = 700;//поменял const auto TEXT_LEFT = 20; const auto TEXT_BASELINE = 20; const auto TEXT_WIDTH = 50; @@ -53,29 +46,28 @@ show_histogram_svg(const vector& bins) const auto RED = "red"; const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH; + if (!bins.empty()){ + const size_t total_height = bins.size() * bin_height; + if (total_height > IMAGE_HEIGHT) { + bin_height = IMAGE_HEIGHT / bins.size(); + } + } 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(bin) / max_count); + svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); + svg_rect(TEXT_WIDTH, top, bin_width, bin_height, BLACK, RED); //добавил параметр + top += bin_height; //добавил параметр + } } svg_end(); } - diff --git a/svg.h b/svg.h index 91d53ba..6f76e4e 100644 --- a/svg.h +++ b/svg.h @@ -2,8 +2,6 @@ #define SVG_H_INCLUDED #include -void -show_histogram_svg(const std::vector& bins); - +void show_histogram_svg(const std::vector& bins, size_t bin_height);//отредачил заголовок #endif // SVG_H_INCLUDED diff --git a/unittest.cpp b/unittest.cpp index 26f2524..d938ec1 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -24,3 +24,19 @@ TEST_CASE("vector of the same elements"){ CHECK(min == 3); CHECK(max == 3); } +TEST_CASE("single element vector") { + double min = 0; + double max = 0; + bool success = find_minmax({1}, min, max); + CHECK(success == true); + CHECK(min == 1); + CHECK(max == 1); +} +TEST_CASE("empty vector") { + double min = 0; + double max = 0; + bool success = find_minmax({}, min, max); + CHECK(success == false); + CHECK(min == 0); + CHECK(max == 0); +} diff --git a/unittest.depend b/unittest.depend index e2d3ad4..618b379 100644 --- a/unittest.depend +++ b/unittest.depend @@ -7,7 +7,7 @@ 1745607609 c:\users\платон\desktop\project\lab01\histogram.h -1746092640 source:c:\users\платон\desktop\project\lab01\unittest.cpp +1746450558 source:c:\users\платон\desktop\project\lab01\unittest.cpp "doctest.h" "histogram_internal.h" @@ -56,10 +56,10 @@ -1746092862 c:\users\платон\desktop\project\lab01\histogram_internal.h +1746450731 c:\users\платон\desktop\project\lab01\histogram_internal.h -1746092126 source:c:\users\платон\desktop\project\lab01\histogram.cpp +1746450679 source:c:\users\платон\desktop\project\lab01\histogram.cpp "histogram.h"