From 6e1d00fae3b71f49a34ffaa6ab0bdf8990a691af Mon Sep 17 00:00:00 2001 From: "Vera(SaraikinaVK)" Date: Mon, 29 Apr 2024 13:14:50 +0300 Subject: [PATCH] var4 --- histogram.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++ histogram.h | 14 ++++++++++++ histogram_internal.h | 8 +++++++ show_svg.cpp | 43 +++++++++++++++++++++++++++++++++++++ svg.cpp | 35 ++++++++++++++++++++++++++++++ svg.h | 15 +++++++++++++ text.cpp | 37 ++++++++++++++++++++++++++++++++ text.h | 11 ++++++++++ unittest.cbp | 41 +++++++++++++++++++++++++++++++++++ unittest.cpp | 40 ++++++++++++++++++++++++++++++++++ 10 files changed, 295 insertions(+) create mode 100644 histogram.cpp create mode 100644 histogram.h create mode 100644 histogram_internal.h create mode 100644 show_svg.cpp create mode 100644 svg.cpp create mode 100644 svg.h create mode 100644 text.cpp create mode 100644 text.h create mode 100644 unittest.cbp create mode 100644 unittest.cpp diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..1df7c99 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,51 @@ +#include "histogram.h" + +vector +make_histogram(const vector& numbers, size_t & bin_count, size_t & number_count, size_t & max_count) { + double min, max; + find_minmax(numbers, min, max); + double bin_size = (max - min) / bin_count; + vector bins(bin_count); + max_count = bins[0]; + for (size_t i = 0; i < number_count; 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 (bins[j] > max_count){ + max_count = bins[j]; + } + } + if (!found){ + bins[bin_count - 1]++; + } + if (bins[bin_count - 1] > max_count){ + max_count = bins[bin_count - 1]; + } + } + return bins; +} + +void +find_minmax(const vector& numbers, double& min, double& max) { + if (!numbers.empty()) { + min = numbers[0]; + max = numbers[0]; + } else { + min = 0; + max = 0; + } + for (double x : numbers) + { + if (x < min){ + min = x; + } + else if (x > max){ + max = x; + } + } +} diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..7e3aefa --- /dev/null +++ b/histogram.h @@ -0,0 +1,14 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED + +#include + +using namespace std; + +vector +make_histogram(const vector& numbers, size_t & bin_count, size_t & number_count, size_t & max_count); + +void +find_minmax(const vector& numbers, double& min, double& max); + +#endif // HISTOGRAM_H_INCLUDED diff --git a/histogram_internal.h b/histogram_internal.h new file mode 100644 index 0000000..59c1874 --- /dev/null +++ b/histogram_internal.h @@ -0,0 +1,8 @@ +#ifndef HISTOGRAM_INTERNAL_H_INCLUDED +#define HISTOGRAM_INTERNAL_H_INCLUDED +#include + +void +find_minmax(const std::vector& numbers, double& min, double& max); + +#endif // HISTOGRAM_INTERNAL_H_INCLUDED diff --git a/show_svg.cpp b/show_svg.cpp new file mode 100644 index 0000000..f9d9b25 --- /dev/null +++ b/show_svg.cpp @@ -0,0 +1,43 @@ +#include "svg.h" + +void +show_histogram_svg(const vector& bins, size_t number_count) { + 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; + const auto BLOCK_WIDTH = 10; + const auto PERCENT_WIDTH = 30; + const auto PERCENT_LEFT = 20; + svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); + double top = 0; + size_t longest; + if (!bins.empty()) { + longest = bins[0]; + } else { + longest = 0; + } + for (size_t bin : bins) { + if (bin > longest) { + longest = bin; + } + } + for (size_t bin : bins) { + const double bin_width = BLOCK_WIDTH * bin; + size_t percent = (static_cast(bin) / static_cast (number_count) * 100); + if (bin == longest){ + svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); + svg_rect(TEXT_WIDTH, top, IMAGE_WIDTH - TEXT_WIDTH - PERCENT_WIDTH - PERCENT_LEFT, BIN_HEIGHT, "#000000", "#00ffff"); + svg_perc(IMAGE_WIDTH - PERCENT_WIDTH, top + TEXT_BASELINE, to_string(percent)); + top += BIN_HEIGHT; + } else{ + svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "#000000", "#00ffff"); + svg_perc(IMAGE_WIDTH - PERCENT_WIDTH, top + TEXT_BASELINE, to_string(percent)); + top += BIN_HEIGHT; + } + } + svg_end(); +} diff --git a/svg.cpp b/svg.cpp new file mode 100644 index 0000000..a68215f --- /dev/null +++ b/svg.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include "svg.h" +using namespace std; + +void +svg_begin(double width, double height) { + cout << "\n"; + cout << "\n"; +} + +void +svg_end() { + cout << "\n"; +} + +void +svg_text(double left, double baseline, string text) { + cout << ""<< text <<""; +} + +void +svg_rect(double x, double y, double width, double height, string stroke, string fill){ + cout << ""; +} + +void +svg_perc(double left, double baseline, string text) { + cout << ""<< text <<"%"; +} diff --git a/svg.h b/svg.h new file mode 100644 index 0000000..dab2584 --- /dev/null +++ b/svg.h @@ -0,0 +1,15 @@ +#ifndef SVG_H_INCLUDED +#define SVG_H_INCLUDED +#include +#include +using namespace std; +void svg_begin(double width, double height); + +void svg_end(); + +void svg_text(double left, double baseline, string text); + +void svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black"); + +void svg_perc(double left, double baseline, string text); +#endif // SVG_H_INCLUDED diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..b72f564 --- /dev/null +++ b/text.cpp @@ -0,0 +1,37 @@ +#include "text.h" +#include +#include +using namespace std; +void +show_histogram_text(vector& bins, size_t & max_count,size_t & bin_count){ +const size_t SCREEN_WIDTH = 80; +const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; +if (max_count > MAX_ASTERISK){ + vector hights(bin_count); + for (size_t i = 0; i < bin_count; i++){ + size_t height = MAX_ASTERISK * (static_cast(bins[i]) / max_count); + hights[i] = height; + } + + for (size_t i = 0; i < bin_count; i++){ + printf("%3d|", bins[i]); + for (size_t j = 0; j < hights[i]; j++){ + cout<<"*"; + + } + + cout << endl; + } + } + else{ + for (size_t i = 0; i < bin_count; i++) + { + printf("%3d|", bins[i]); + for (size_t j = 0; j < bins[i]; j++){ + cout<<"*"; + } + cout << endl; + } + } + +} diff --git a/text.h b/text.h new file mode 100644 index 0000000..821c343 --- /dev/null +++ b/text.h @@ -0,0 +1,11 @@ +#ifndef SHOW-SVG_H_INCLUDED +#define SHOW-SVG_H_INCLUDED + +#include + +using namespace std; + +void +show_histogram_svg(const vector& bins, size_t number_count); + +#endif // SHOW-SVG_H_INCLUDED diff --git a/unittest.cbp b/unittest.cbp new file mode 100644 index 0000000..534f497 --- /dev/null +++ b/unittest.cbp @@ -0,0 +1,41 @@ + + + + + + diff --git a/unittest.cpp b/unittest.cpp new file mode 100644 index 0000000..5c6b44c --- /dev/null +++ b/unittest.cpp @@ -0,0 +1,40 @@ +#define DOCTEST_CONFIG_NO_MULTITHREADING +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "doctest.h" +#include "histogram_internal.h" + +TEST_CASE("distinct positive numbers") { + double min = 0; + double max = 0; + find_minmax({}, min, max); + CHECK(min == 0); + CHECK(max == 0); +} + 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 positive numbers") { + double min = 0; + double max = 0; + find_minmax({1}, min, max); + CHECK(min == 1); + CHECK(max == 1); +} +TEST_CASE("distinct positive numbers") { + 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") { + double min = 0; + double max = 0; + find_minmax({-1.5,-2,-3,-4}, min, max); + CHECK(min == -4); + CHECK(max == -1.5); +}