From 998ed9798f08cfad2b2b7596da9a549455288324 Mon Sep 17 00:00:00 2001 From: MachulinaDV Date: Sat, 3 Jun 2023 17:59:13 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20Input?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++ histogram.h | 9 ++++++ histogram_internal.h | 7 +++++ svg.cpp | 64 +++++++++++++++++++++++++++++++++++++++++ svg.h | 8 ++++++ text.cpp | 29 +++++++++++++++++++ text.h | 6 ++++ unittest.cbp | 40 ++++++++++++++++++++++++++ unittest.cpp | 56 ++++++++++++++++++++++++++++++++++++ 9 files changed, 287 insertions(+) create mode 100644 histogram.cpp create mode 100644 histogram.h create mode 100644 histogram_internal.h 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..4198d98 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include "histogram.h" + + +using namespace std; + +bool find_minmax(const vector& A, double& min, double& max) +{ + bool empt; + + if (A.size() != 0) + { + + min = A[0]; + for (auto i = 0; i < A.size(); i++) + { + if (A[i] < min) + { + min = A[i]; + } + } + + max = A[0]; + for (auto i = 0; i < A.size(); i++) + { + if (A[i] > max) + { + max = A[i]; + } + } + empt =true; + } + else + empt = false; + return empt; +} + + +vector make_histogram(const vector& A, size_t bin) +{ + vectorB(bin); + size_t max_count; + double max, min; + bool empt; + find_minmax(A, min, max); + double step = (max - min) / (bin); + + for (size_t i = 0; i < A.size(); i++) + { + for (size_t j = 0; j < bin; j++) + { + if ((A[i] >= (min + j * step)) && (A[i] < (min + (j + 1)*step))) + { + B[j]++; + break; + } + } + } + + for (size_t i = 0; i < A.size(); i++) + { + if (A[i] == max) + B[bin - 1]++; + } + return B; +} diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..f06c41f --- /dev/null +++ b/histogram.h @@ -0,0 +1,9 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED + +#include + +std::vector +make_histogram(const std::vector& A, size_t bin); + +#endif // HISTOGRAM_H_INCLUDED diff --git a/histogram_internal.h b/histogram_internal.h new file mode 100644 index 0000000..6e1e667 --- /dev/null +++ b/histogram_internal.h @@ -0,0 +1,7 @@ +#ifndef HISTOGRAM_INTERNAL_H_INCLUDED +#define HISTOGRAM_INTERNAL_H_INCLUDED + +#include + +bool find_minmax(const std::vector& A, double& min, double& max); +#endif // HISTOGRAM_INTERNAL_H_INCLUDED diff --git a/svg.cpp b/svg.cpp new file mode 100644 index 0000000..ecfae83 --- /dev/null +++ b/svg.cpp @@ -0,0 +1,64 @@ +#include +#include +#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 opacity , string stroke, string fill) { +cout << ""; + +} + + +void +show_histogram_svg(const vector& B) { + 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 MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH; + + + svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT); + + double top = 0; + double max_count = B[0]; + for (size_t i = 0; i < B.size(); i++) { + if (B[i] > max_count) + max_count = B[i]; + } + for (size_t bin : B) { + double opacity = (bin / max_count); + const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH)*(bin/max_count); + svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, to_string(opacity), "blue", "#9BB0D2"); + top += BIN_HEIGHT; + } + + svg_end(); +} diff --git a/svg.h b/svg.h new file mode 100644 index 0000000..d269374 --- /dev/null +++ b/svg.h @@ -0,0 +1,8 @@ +#ifndef SVG_H_INCLUDED +#define SVG_H_INCLUDED + +#include +void +show_histogram_svg(const std::vector& B); + +#endif // SVG_H_INCLUDED diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..d4f1174 --- /dev/null +++ b/text.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include "text.h" + +using namespace std; + +void show_histogram(vectorB, size_t bin) +{ + for (size_t i = 0; i < bin; i++) + { + if (B[i] < 10) + { + cout << " "; + } + else if (B[i] < 100) + { + cout << " "; + } + cout << B[i] << "|"; + for (size_t j = 0; j < B[i]; j++) + { + cout << "*"; + } + cout << endl; + } + return; +} + diff --git a/text.h b/text.h new file mode 100644 index 0000000..5875d32 --- /dev/null +++ b/text.h @@ -0,0 +1,6 @@ +#pragma once + +#include + +void show_histogram(std::vectorB, size_t bin); + diff --git a/unittest.cbp b/unittest.cbp new file mode 100644 index 0000000..379d0a2 --- /dev/null +++ b/unittest.cbp @@ -0,0 +1,40 @@ + + + + + + diff --git a/unittest.cpp b/unittest.cpp new file mode 100644 index 0000000..c8f4db1 --- /dev/null +++ b/unittest.cpp @@ -0,0 +1,56 @@ +#define DOCTEST_CONFIG_NO_MULTITHREADING +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "doctest.h" +#include "histogram_internal.h" + +TEST_CASE("emptyA") { + std::vectorA{1,2,3,4}; + double min = 0; + double max = 0; + find_minmax(A, min, max); + CHECK(A.size() !=0 ); +} + +TEST_CASE("min") +{ + std::vectorA{1,2,3,4}; + double min = 0; + double max = 0; + find_minmax(A, min, max); + CHECK(min == 1); +} + +TEST_CASE("max") +{ + std::vectorA{1,2,3,4}; + double min = 0; + double max = 0; + find_minmax(A, min, max); + CHECK(max == 4); +} + + +TEST_CASE("1A") { + std::vectorA{1,2,3,4}; + double min = 0; + double max = 0; + find_minmax(A, min, max); + CHECK(A.size() !=1 ); +} + + +TEST_CASE("same"){ + std::vectorA{1,1,1,1}; + std::vectorB{1,1,1,1}; + double min = 0; + double max = 0; + find_minmax(A, min, max); + CHECK(A == B); +} + +TEST_CASE("emt"){ + std::vectorA{}; + double min = 0; + double max = 0; + CHECK(find_minmax(A,min,max)==false); +}