diff --git a/Lab_1.cbp b/Lab_1.cbp index d3c8471..cfd844f 100644 --- a/Lab_1.cbp +++ b/Lab_1.cbp @@ -32,7 +32,14 @@ + + + + + + + diff --git a/Lab_1.depend b/Lab_1.depend new file mode 100644 index 0000000..8e104f5 --- /dev/null +++ b/Lab_1.depend @@ -0,0 +1,46 @@ +# depslib dependency file v1.0 +1680527920 source:l:\i курс\А-3-22\Комков\lab_1\main.cpp + + + +1680527920 source:e:\лаба 1\lab_1\main.cpp + + + +1685970224 source:c:\users\жесткий п\desktop\лаба 1\lab_1\histogram.cpp + + + "histogram.h" + +1685968632 c:\users\жесткий п\desktop\лаба 1\lab_1\histogram.h + + +1685971982 source:c:\users\жесткий п\desktop\лаба 1\lab_1\main.cpp + + + + "histogram.h" + "text.h" + "svg.h" + +1685882625 c:\users\жесткий п\desktop\лаба 1\lab_1\text.h + + +1685882527 source:c:\users\жесткий п\desktop\лаба 1\lab_1\text.cpp + + + "text.h" + +1685972025 source:c:\users\жесткий п\desktop\лаба 1\lab_1\svg.cpp + + + + + + "svg.h" + +1685972059 c:\users\жесткий п\desktop\лаба 1\lab_1\svg.h + + + + diff --git a/Lab_1.layout b/Lab_1.layout new file mode 100644 index 0000000..ea80b81 --- /dev/null +++ b/Lab_1.layout @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/histogram.cpp b/histogram.cpp index 0eacf96..861dc46 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -4,8 +4,9 @@ #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; + else{ max = numbers[0]; min = numbers[1]; for (double x: numbers) @@ -19,10 +20,9 @@ void find_minmax(const vector &numbers, double &min, double &max) max = x; } } - return; - + return true ; +} } - vector make_histogram (vector numbers, size_t bin_count) { @@ -52,4 +52,14 @@ vector make_histogram (vector numbers, size_t bin_count) return bins; } +vector make_borders(const vector& numbers, size_t bin_count){ + double min, max; + find_minmax(numbers, min, max); + double diff = (max - min) / bin_count; + std::vector borders(bin_count - 1); + for (size_t i = 0; i <= bin_count - 2; i++){ + borders[i] = min + (i + 1) * diff; + } + return borders; +} diff --git a/histogram.h b/histogram.h index ed9e7b4..90bbcb7 100644 --- a/histogram.h +++ b/histogram.h @@ -5,4 +5,7 @@ std::vector make_histogram(const std::vector numbers, size_t bin_count); +std::vector +make_borders(const std::vector& numbers, size_t bin_count); + #endif // HISTOGRAM_H_INCLUDED diff --git a/main.cpp b/main.cpp index 0095eec..77ae553 100644 --- a/main.cpp +++ b/main.cpp @@ -4,6 +4,7 @@ #include #include "histogram.h" #include "text.h" +#include "svg.h" using namespace std; struct Input { @@ -32,7 +33,8 @@ int main() { auto in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); - show_histogram_text(bins, in.bin_count); + auto borders = make_borders(in.numbers, in.bin_count); + show_histogram_svg(bins, borders, in.bin_count); getch(); return 0; } diff --git a/main.exe b/main.exe new file mode 100644 index 0000000..e84de91 Binary files /dev/null and b/main.exe differ diff --git a/main.o b/main.o new file mode 100644 index 0000000..b593bcd Binary files /dev/null and b/main.o differ diff --git a/svg.cpp b/svg.cpp index fc28ea5..348e9e4 100644 --- a/svg.cpp +++ b/svg.cpp @@ -19,6 +19,7 @@ svg_begin(double width, double height) cout << "xmlns='http://www.w3.org/2000/svg'>\n"; } + void svg_end() { @@ -41,7 +42,7 @@ svg_rect(double x, double y, double width, double height, string stroke = "black void -show_histogram_svg(const vector& bins) +show_histogram_svg(const vector& bins, const vector& borders, size_t bin_count) { const auto IMAGE_WIDTH = 400; const auto IMAGE_HEIGHT = 300; @@ -50,12 +51,14 @@ show_histogram_svg(const vector& bins) const auto TEXT_WIDTH = 50; const auto BIN_HEIGHT = 30; const auto BLOCK_WIDTH = 10; - const auto BLACK = "black"; - const auto RED = "red"; + const auto COLOUR1 = "black"; + const auto COLOUR2 = "green"; const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH; - + size_t border = 0; + size_t number_of_blocks; svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT); + size_t max_bin = *max_element(bins.begin(), bins.end()); double top = 0; double max_count = bins[0]; @@ -69,10 +72,20 @@ show_histogram_svg(const vector& bins) for (size_t bin : bins) { - double bin_width = (MAX_WIDTH)*(bin/max_count); + number_of_blocks = bin; + if ((max_bin * BLOCK_WIDTH) > (IMAGE_WIDTH - TEXT_WIDTH)){ + number_of_blocks = ((IMAGE_WIDTH - TEXT_WIDTH)/10) * (static_cast(bin) / max_bin); + } + const double bin_width = BLOCK_WIDTH * number_of_blocks; 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, COLOUR1, COLOUR2); top += BIN_HEIGHT; + if (border < bin_count - 1){ + top += BIN_HEIGHT; + svg_text(TEXT_LEFT / 2, top + TEXT_BASELINE , to_string(borders[border])); + border += 1; + top += BIN_HEIGHT; + } } svg_end(); diff --git a/svg.h b/svg.h index 91d53ba..38ceecf 100644 --- a/svg.h +++ b/svg.h @@ -1,9 +1,11 @@ #ifndef SVG_H_INCLUDED #define SVG_H_INCLUDED #include +#include +#include void -show_histogram_svg(const std::vector& bins); +show_histogram_svg(const std::vector& bins, const std::vector& borders, size_t bin_count); #endif // SVG_H_INCLUDED diff --git a/unittest.cbp b/unittest.cbp new file mode 100644 index 0000000..5a9c8c0 --- /dev/null +++ b/unittest.cbp @@ -0,0 +1,42 @@ + + + + + + diff --git a/unittest.cpp b/unittest.cpp index 7fb242b..7ce6d54 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -25,5 +25,6 @@ TEST_CASE("vector of the same elements"){ find_minmax({3,3,3}, min, max); CHECK(min == 3); CHECK(max == 3); + } diff --git a/unittest.depend b/unittest.depend new file mode 100644 index 0000000..ab1f114 --- /dev/null +++ b/unittest.depend @@ -0,0 +1,61 @@ +# depslib dependency file v1.0 +1685890713 source:c:\users\жесткий п\desktop\лаба 1\lab_1\unittest.cpp + "doctest.h" + "histogram_internal.h" + +1685890386 c:\users\жесткий п\desktop\лаба 1\lab_1\doctest.h + + + + + + + "doctest_fwd.h" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1685888788 c:\users\жесткий п\desktop\лаба 1\lab_1\histogram_internal.h + + +1685970224 source:c:\users\жесткий п\desktop\лаба 1\lab_1\histogram.cpp + + + "histogram.h" + +1685968632 c:\users\жесткий п\desktop\лаба 1\lab_1\histogram.h + + diff --git a/unittest.layout b/unittest.layout new file mode 100644 index 0000000..3f14a73 --- /dev/null +++ b/unittest.layout @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + +