diff --git a/Programm/03-scaling.input.txt b/Programm/03-scaling.input.txt new file mode 100644 index 0000000..2539400 --- /dev/null +++ b/Programm/03-scaling.input.txt @@ -0,0 +1,5 @@ +142 +1 1 1 1 1 1 1 1 1 +2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 +3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 +3 diff --git a/Programm/histogram.cpp b/Programm/histogram.cpp index 86d4bf2..7f377d4 100644 --- a/Programm/histogram.cpp +++ b/Programm/histogram.cpp @@ -1,4 +1,5 @@ #include "histogram.h" +// #include "histogram_internal.h" void find_minmax(const std::vector &numbers, double &min, double &max) { diff --git a/Programm/histogram_internal.h b/Programm/histogram_internal.h deleted file mode 100644 index 3b0cdb0..0000000 --- a/Programm/histogram_internal.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef HISTOGRAM_INTERNAL_H -#define HISTOGRAM_INTERNAL_H -#include - -void find_minmax(const std::vector &numbers, double &min, double &max); -#endif \ No newline at end of file diff --git a/Programm/marks.svg b/Programm/marks.svg new file mode 100644 index 0000000..4d9af4f --- /dev/null +++ b/Programm/marks.svg @@ -0,0 +1,3 @@ + + +96%3323%10071% diff --git a/Programm/marks.txt b/Programm/marks.txt index 2cdb9aa..dbe28a3 100644 --- a/Programm/marks.txt +++ b/Programm/marks.txt @@ -1,3 +1,3 @@ -10 -3 3 4 4 4 4 4 5 5 5 +12 +1 1 1 1 2 2 2 2 2 3 3 3 3 diff --git a/Programm/marks11.txt b/Programm/marks11.txt new file mode 100644 index 0000000..e7adb81 --- /dev/null +++ b/Programm/marks11.txt @@ -0,0 +1,3 @@ + 2|** 18% + 5|***** 45% + 4|**** 36% diff --git a/Programm/svg.cpp b/Programm/svg.cpp index d89966f..162b317 100644 --- a/Programm/svg.cpp +++ b/Programm/svg.cpp @@ -1,4 +1,5 @@ #include "svg.h" + void svg_text(double left, double baseline, std::string text); void svg_begin(double width, double height) @@ -27,7 +28,7 @@ void svg_rect(double x, double y, double width, double height, std::string strok << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << " '/>"; } -void show_histogram_svg(const std::vector &bins) +double show_histogram_svg(const std::vector &bins) { const auto IMAGE_WIDTH = 400; const auto IMAGE_HEIGHT = 300; @@ -45,9 +46,9 @@ void show_histogram_svg(const std::vector &bins) if (bin > max_count) max_count = bin; } - const auto BLOCK_WIDTH = (IMAGE_WIDTH - TEXT_WIDTH * 2 - TEXT_LEFT) / max_count; + const auto BLOCK_WIDTH = (IMAGE_WIDTH - TEXT_WIDTH * 2 - TEXT_LEFT) / max_count; const auto TEXT_RATIO_SHIFT = IMAGE_WIDTH - TEXT_WIDTH; - auto summ = 0; + double summ = 0, ratio_summ = 0; for (size_t bin : bins) { summ += bin; @@ -58,10 +59,12 @@ void show_histogram_svg(const std::vector &bins) const double bin_width = BLOCK_WIDTH * bin; svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin)); svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "blue", "#aaffaa"); - auto ratio = (bin * 100) / summ; - if (bin == bins.back() && summ % 2 != 0) + int ratio = (bin * 100) / summ; + ratio_summ += ratio; + if (bin == bins.back() && ratio_summ != 100) { ratio ++; + ratio_summ ++; } svg_text(TEXT_RATIO_SHIFT, top + TEXT_BASELINE, (std::to_string(ratio) + "%")); top += BIN_HEIGHT; @@ -69,4 +72,5 @@ void show_histogram_svg(const std::vector &bins) // svg_text(20, 20, std::to_string(bins[0])); // svg_rect(50, 0, bins[0] * 10, 30); svg_end(); + return ratio_summ; } \ No newline at end of file diff --git a/Programm/svg.dSYM/Contents/Resources/DWARF/svg b/Programm/svg.dSYM/Contents/Resources/DWARF/svg index eb365e7..80a6494 100644 Binary files a/Programm/svg.dSYM/Contents/Resources/DWARF/svg and b/Programm/svg.dSYM/Contents/Resources/DWARF/svg differ diff --git a/Programm/svg.h b/Programm/svg.h index 30a1db8..ec1b339 100644 --- a/Programm/svg.h +++ b/Programm/svg.h @@ -3,5 +3,5 @@ #include #include -void show_histogram_svg(const std::vector &bins); +double show_histogram_svg(const std::vector &bins); #endif \ No newline at end of file diff --git a/Unittests/histogram.cpp b/Unittests/histogram.cpp new file mode 100644 index 0000000..b527a07 --- /dev/null +++ b/Unittests/histogram.cpp @@ -0,0 +1,51 @@ +#include "histogram_internal.h" + +bool find_minmax(const std::vector &numbers, double &min, double &max) +{ + if(numbers.empty()){ + return false; + } + min = numbers[0]; + max = numbers[0]; + for (double x : numbers) + { + if (x < min) + { + min = x; + } + else if (x > max) + { + max = x; + } + } + return true; +} + +std::vector make_histogram(const std::vector &numbers, const size_t &bin_count) +{ + double min, max; + find_minmax(numbers, min, max); + + std::vector bins(bin_count); + double bin_size = (max - min) / bin_count; + + for (size_t i = 0; i < numbers.size(); i++) + { + bool found = false; + for (size_t j = 0; (j < bin_count - 1) && !found; j++) + { + auto lo = min + j * bin_size; + auto hi = min + (j + 1) * bin_size; + if ((lo <= numbers[i]) && (numbers[i] <= hi)) + { + bins[j]++; + found = true; + } + } + if (!found) + { + bins[bin_count - 1]++; + } + } + return bins; +} \ No newline at end of file diff --git a/Unittests/histogram_internal.h b/Unittests/histogram_internal.h index 3b0cdb0..a00b0cf 100644 --- a/Unittests/histogram_internal.h +++ b/Unittests/histogram_internal.h @@ -1,6 +1,7 @@ -#ifndef HISTOGRAM_INTERNAL_H -#define HISTOGRAM_INTERNAL_H +#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); +double show_histogram_svg(const std::vector &bins); #endif \ No newline at end of file diff --git a/Unittests/svg.cpp b/Unittests/svg.cpp new file mode 100644 index 0000000..2e0a381 --- /dev/null +++ b/Unittests/svg.cpp @@ -0,0 +1,78 @@ +#include "histogram_internal.h" +#include +#include + +void svg_text(double left, double baseline, std::string text); + +void svg_begin(double width, double height) +{ + std::cout << "\n"; + std::cout << "\n"; +} + +void svg_end() +{ + std::cout << "\n"; +} + +void svg_text(double left, double baseline, std::string text) +{ + std::cout << "" << text << ""; +} + +void svg_rect(double x, double y, double width, double height, std::string stroke = "black", std::string fill = "black") +{ + std::cout << ""; +} + +double show_histogram_svg(const std::vector &bins) +{ + const auto IMAGE_WIDTH = 400; + const auto IMAGE_HEIGHT = 300; + const auto TEXT_LEFT = 20; + const auto TEXT_BASELINE = 20; + const auto TEXT_WIDTH = 50; + const auto BIN_HEIGHT = 30; + + svg_begin(400, 300); + double top = 0; + + size_t max_count = 0; + for (auto bin : bins) + { + if (bin > max_count) + max_count = bin; + } + const auto BLOCK_WIDTH = (IMAGE_WIDTH - TEXT_WIDTH * 2 - TEXT_LEFT) / max_count; + const auto TEXT_RATIO_SHIFT = IMAGE_WIDTH - TEXT_WIDTH; + double summ = 0, ratio_summ = 0; + for (size_t bin : bins) + { + summ += bin; + } + + for (size_t bin : bins) + { + const double bin_width = BLOCK_WIDTH * bin; + svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin)); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "blue", "#aaffaa"); + int ratio = (bin * 100) / summ; + ratio_summ += ratio; + if (bin == bins.back() && ratio_summ != 100) + { + ratio++; + ratio_summ++; + } + svg_text(TEXT_RATIO_SHIFT, top + TEXT_BASELINE, (std::to_string(ratio) + "%")); + top += BIN_HEIGHT; + } + // svg_text(20, 20, std::to_string(bins[0])); + // svg_rect(50, 0, bins[0] * 10, 30); + svg_end(); + return ratio_summ; +} diff --git a/Unittests/unittest b/Unittests/unittest new file mode 100755 index 0000000..b2307a7 Binary files /dev/null and b/Unittests/unittest differ diff --git a/Unittests/unittest.cpp b/Unittests/unittest.cpp index 042e42a..1a1e3ac 100644 --- a/Unittests/unittest.cpp +++ b/Unittests/unittest.cpp @@ -1,12 +1,58 @@ #define DOCTEST_CONFIG_NO_MULTITHREADING #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #include "doctest.h" +#include #include "histogram_internal.h" -TEST_CASE("distinct positive numbers") { +TEST_CASE("distinct positive numbers") +{ double min = 0; double max = 0; find_minmax({1, 2}, min, max); CHECK(min == 1); CHECK(max == 2); -} \ No newline at end of file +} + +TEST_CASE("only one vector in numbers") +{ + double min = 0; + double max = 0; + find_minmax({1}, min, max); + CHECK(min == 1); + CHECK(max == 1); +} + +TEST_CASE("distinct negative numbers") +{ + double min = 0; + double max = 0; + find_minmax({-1, -100, -30, -40}, min, max); + CHECK(min == -100); + CHECK(max == -1); +} + +TEST_CASE("vector of indentical elements") +{ + double min = 0; + double max = 0; + find_minmax({5, 5, 5, 5}, min, max); + CHECK(min == 5); + CHECK(max == 5); +} + +TEST_CASE("vector of indentical elements") +{ + double min = 0; + double max = 0; + CHECK(find_minmax({}, min, max) == false); +} + + +TEST_CASE("vector of indentical elements") +{ + auto summ = 0; + summ = show_histogram_svg({1, 2, 4}); + CHECK(summ == 100); +} + + diff --git a/Unittests/unittest.dSYM/Contents/Info.plist b/Unittests/unittest.dSYM/Contents/Info.plist new file mode 100644 index 0000000..268569e --- /dev/null +++ b/Unittests/unittest.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.unittest + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Unittests/unittest.dSYM/Contents/Resources/DWARF/unittest b/Unittests/unittest.dSYM/Contents/Resources/DWARF/unittest new file mode 100644 index 0000000..2451954 Binary files /dev/null and b/Unittests/unittest.dSYM/Contents/Resources/DWARF/unittest differ