diff --git a/lab03.depend b/lab03.depend index 2945666..fb3bf0a 100644 --- a/lab03.depend +++ b/lab03.depend @@ -1,5 +1,5 @@ # depslib dependency file v1.0 -1716798554 source:c:\users\lenov\desktop\lab03\histogram.cpp +1716799438 source:c:\users\lenov\desktop\lab03\histogram.cpp "histogram.h" @@ -7,12 +7,27 @@ 1716797551 c:\users\lenov\desktop\lab03\histogram.h -1716799012 source:c:\users\lenov\desktop\lab03\main.cpp +1716817439 source:c:\users\lenov\desktop\lab03\main.cpp "histogram.h" "text.h" + "svg.h" 1716798974 c:\users\lenov\desktop\lab03\text.h +1716799035 source:c:\users\lenov\desktop\lab03\text.cpp + + "text.h" + + +1716817390 c:\users\lenov\desktop\lab03\svg.h + + +1716817410 source:c:\users\lenov\desktop\lab03\svg.cpp + + + + "svg.h" + diff --git a/main.cpp b/main.cpp index 20966ee..6041a34 100644 --- a/main.cpp +++ b/main.cpp @@ -2,12 +2,13 @@ #include #include "histogram.h" #include "text.h" - +#include "svg.h" using namespace std; struct Input { vector numbers; size_t bin_count{}; + double COLUMN_HIGHT; }; Input input_data() { @@ -22,6 +23,8 @@ input_data() { } cerr << "Enter bin count: "; cin>> in.bin_count; + cerr << "Enter column hight: "; + cin>> in.COLUMN_HIGHT; return in; } @@ -111,7 +114,8 @@ int main() { auto in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); - show_histogram_text(bins,in.bin_count ); + // show_histogram_svg(bins); + COLUMN_hight_function(in.COLUMN_HIGHT,in.bin_count,bins); /*int main() { diff --git a/marks.txt b/marks.txt new file mode 100644 index 0000000..2cdb9aa --- /dev/null +++ b/marks.txt @@ -0,0 +1,3 @@ +10 +3 3 4 4 4 4 4 5 5 5 +3 diff --git a/svg.cpp b/svg.cpp new file mode 100644 index 0000000..d4c6315 --- /dev/null +++ b/svg.cpp @@ -0,0 +1,104 @@ +#include +#include +#include +#include "svg.h" +using namespace std; + +const auto IMAGE_WIDTH = 400; +const auto IMAGE_HEIGHT = 700; +const auto TEXT_LEFT = 20; +const auto TEXT_BASELINE = 20; +const auto TEXT_WIDTH = 50; +const auto BIN_HEIGHT = 30; +const auto BLOCK_WIDTH = 10; +const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH; + +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 = "aaaaaa") { + cout << "\n"; +} + +void +svg_begin(double width, double height) { + cout << "\n"; + cout << "\n"; +} + +void +svg_end() { + cout << "\n"; +} + +void +show_histogram_svg(const vector& bins) { + const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH; + size_t max_count = 0; + for (size_t x : bins) { + if (x > max_count) { + max_count = x; + } + } + if (max_count == 0) { + max_count = 1; + } + auto scale_factor = static_cast(MAX_WIDTH) / (max_count * BLOCK_WIDTH); + if (scale_factor > 1) { + scale_factor = 1; + } + svg_begin(400, 300); + double top = 0; + for (size_t bin : bins) { + double bin_width = BLOCK_WIDTH * bin * scale_factor; + svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT); + top += BIN_HEIGHT; + } + svg_end(); +} + +void +svg_rect_hight(double x, double y, double width, double height, string stroke = "black", string fill = "hfhfh") { + cout << "\n"; +} + +void +COLUMN_hight_function( double &COLUMN_height ,size_t bin_count,const vector& bins){ + const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH; + size_t max_count = 0; + for (size_t x : bins) { + if (x > max_count) { + max_count = x; + } + } + if (max_count == 0) { + max_count = 1; + } + auto scale_factor = static_cast(MAX_WIDTH) / (max_count * BLOCK_WIDTH); + if (scale_factor > 1) { + scale_factor = 1; + } + svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT ); + if ((bin_count*COLUMN_height) > IMAGE_HEIGHT ){ + + COLUMN_height= static_cast(IMAGE_HEIGHT) / bin_count; + + } + double top = 0; + for (size_t bin : bins) { + double bin_width = BLOCK_WIDTH * bin * scale_factor; + svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); + svg_rect(TEXT_WIDTH, top, bin_width, COLUMN_height); + top += COLUMN_height; + } + svg_end(); + +} diff --git a/svg.h b/svg.h new file mode 100644 index 0000000..2a9c94f --- /dev/null +++ b/svg.h @@ -0,0 +1,10 @@ +#ifndef SVG_H_INCLUDED +#define SVG_H_INCLUDED + +#include + +void +show_histogram_svg(const std::vector& bins); +void +COLUMN_hight_function( double &COLUMN_height ,size_t bin_count,const std::vector& bins); +#endif // SVG_H_INCLUDED diff --git a/unittest.cpp b/unittest.cpp index e69de29..06013cb 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -0,0 +1,46 @@ +#define DOCTEST_CONFIG_NO_MULTITHREADING +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "doctest.h" +#include "histogram_internal.h" +#include "svg.h" + +TEST_CASE("distinct positive numbers") { + double min = 0; + double max = 0; + find_minmax({1, 2}, min, max); + CHECK(min == 1); + CHECK(max == 2); +} +TEST_CASE("distinct negative numbers") { + double min = -1; + double max = -1; + find_minmax({-1, -2}, min, max); + CHECK(min == -2); + CHECK(max == -1); +} +TEST_CASE("one number") { + double min = 0; + double max = 0; + find_minmax({1}, min, max); + CHECK(min == 1); + CHECK(max == 1); +} +TEST_CASE("same numbers") { + double min = 0; + double max = 0; + find_minmax({2, 2}, min, max); + CHECK(min == 2); + CHECK(max == 2); +} + + + + + +TEST_CASE("distinct positive numbers") { + double COLUMN_height = 400; + COLUMN_hight_function(COLUMN_height ,5,{23,5,54,6,7}); + CHECK(COLUMN_height == 150 ); + +} + diff --git a/unittest.depend b/unittest.depend new file mode 100644 index 0000000..7b0e9ea --- /dev/null +++ b/unittest.depend @@ -0,0 +1,71 @@ +# depslib dependency file v1.0 +1716799438 source:c:\users\lenov\desktop\lab03\histogram.cpp + "histogram.h" + + + +1716797551 c:\users\lenov\desktop\lab03\histogram.h + + +1716819750 source:c:\users\lenov\desktop\lab03\unittest.cpp + "doctest.h" + "histogram_internal.h" + "svg.h" + +1716800315 c:\users\lenov\desktop\lab03\doctest.h + + + + + + + "doctest_fwd.h" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1716799551 c:\users\lenov\desktop\lab03\histogram_internal.h + + +1716819750 source:c:\users\lenov\desktop\lab03\svg.cpp + + + + "svg.h" + +1716819918 c:\users\lenov\desktop\lab03\svg.h + +