diff --git a/project/percent.cpp b/project/percent.cpp new file mode 100644 index 0000000..c46129e --- /dev/null +++ b/project/percent.cpp @@ -0,0 +1,16 @@ +#include "percent.h" + +#include +using namespace std; + +void +percent(const vector& bins, vector&bins_percent){ + size_t number_count = 0; + size_t bin_count = bins.size(); + for (size_t bin : bins){ + number_count=number_count+bin; + } + for (int i=0; i +using namespace std; + +void +percent(const vector& bins, vector&bins_percent); + +#endif // PERCENT_H_INCLUDED diff --git a/project/show_histogram.cpp b/project/show_histogram.cpp index d22990a..9693632 100644 --- a/project/show_histogram.cpp +++ b/project/show_histogram.cpp @@ -1,4 +1,5 @@ #include "svg.h" +#include "percent.h" void show_histogram_svg(const vector& bins) { @@ -10,8 +11,13 @@ show_histogram_svg(const vector& bins) { const auto BIN_HEIGHT = 30; const auto BLOCK_WIDTH = 10; svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); + + double top = 0; size_t longest; + vectorbins_percent{}; + + if (!bins.empty()) { longest = bins[0]; } else { @@ -26,7 +32,7 @@ show_histogram_svg(const vector& bins) { const double bin_width = BLOCK_WIDTH * bin; if (bin == longest){ svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); - svg_rect(TEXT_WIDTH, top, IMAGE_WIDTH - TEXT_WIDTH, BIN_HEIGHT, "#000000", "#ff00a2"); + svg_rect(TEXT_WIDTH, top, IMAGE_WIDTH - 2*TEXT_WIDTH, BIN_HEIGHT, "#000000", "#ff00a2"); top += BIN_HEIGHT; } else{ svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); @@ -34,5 +40,11 @@ show_histogram_svg(const vector& bins) { top += BIN_HEIGHT; } } + top = 0; + percent(bins, bins_percent); + for (size_t bin_percent : bins_percent){ + svg_text(IMAGE_WIDTH - TEXT_WIDTH + TEXT_LEFT, top + TEXT_BASELINE, to_string(bin_percent)+"%"); + top += BIN_HEIGHT; + } svg_end(); } diff --git a/project/unittest.cpp b/project/unittest.cpp index 5c6b44c..35c8854 100644 --- a/project/unittest.cpp +++ b/project/unittest.cpp @@ -2,8 +2,9 @@ #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #include "doctest.h" #include "histogram_internal.h" +#include "percent.h" -TEST_CASE("distinct positive numbers") { +TEST_CASE("distinct empty") { double min = 0; double max = 0; find_minmax({}, min, max); @@ -17,24 +18,30 @@ TEST_CASE("distinct positive numbers") { CHECK(min == 1); CHECK(max == 2); } -TEST_CASE("distinct positive numbers") { +TEST_CASE("distinct single") { double min = 0; double max = 0; find_minmax({1}, min, max); CHECK(min == 1); CHECK(max == 1); } -TEST_CASE("distinct positive numbers") { +TEST_CASE("distinct same") { double min = 0; double max = 0; find_minmax({1,1,1,1,1,1,1,1,1}, min, max); CHECK(min == 1); CHECK(max == 1); } -TEST_CASE("distinct positive numbers") { +TEST_CASE("distinct negative numbers") { double min = 0; double max = 0; find_minmax({-1.5,-2,-3,-4}, min, max); CHECK(min == -4); CHECK(max == -1.5); } +TEST_CASE("percent 1") { + const vectorbins{5, 10, 10, 50, 25}; + vectorbins_percent{}; + percent(bins, bins_percent); + CHECK(bins_percent[0] == 5); +}