From 30cb7497eb7281aa4fb16c5b88f00bc6d9f1aa39 Mon Sep 17 00:00:00 2001 From: Dana Date: Mon, 22 Apr 2024 01:21:02 +0300 Subject: [PATCH] ... --- .gitignore | 6 +++++ histogram.cpp | 45 ++++++++++++++++++++++++++++++++ histogram.h | 6 +++++ histogram_internal.h | 6 +++++ lab03.cbp | 2 ++ lab03.depend | 29 +++++++++++++++++++++ svg.cpp | 48 ++++++++++++++++++++++++++++++++++ svg.h | 13 ++++++++++ text.cpp | 33 ++++++++++++++++++++++++ text.h | 6 +++++ unittest.cbp | 41 +++++++++++++++++++++++++++++ unittest.cpp | 12 +++++++++ unittest.depend | 61 ++++++++++++++++++++++++++++++++++++++++++++ unittest1.cbp | 41 +++++++++++++++++++++++++++++ unittest1.cpp | 12 +++++++++ unittest2.cbp | 41 +++++++++++++++++++++++++++++ unittest2.cpp | 12 +++++++++ 17 files changed, 414 insertions(+) create mode 100644 .gitignore create mode 100644 histogram.cpp create mode 100644 histogram.h create mode 100644 histogram_internal.h create mode 100644 lab03.depend 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 create mode 100644 unittest.depend create mode 100644 unittest1.cbp create mode 100644 unittest1.cpp create mode 100644 unittest2.cbp create mode 100644 unittest2.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..de954fc --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +/bin +/obj +/lab03.layout +/unittest2.layout +/unittest1.layout +/unittest.layout diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..0cf9b1d --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,45 @@ +#include "histogram.h" +void +find_minmax(const std::vector& numbers, double& min, double& max) { + min = numbers[0]; + max = numbers[0]; + for (double x : numbers) + { + if (x < min){ + min = x; + } + else if (x > max){ + max = x; + } + } +} + +std::vector +make_histogram(const std::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; + std::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; +} diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..0189f69 --- /dev/null +++ b/histogram.h @@ -0,0 +1,6 @@ +#pragma once +#include +#include + +std::vector +make_histogram(const std::vector& numbers, size_t & bin_count, size_t & number_count, size_t & max_count); diff --git a/histogram_internal.h b/histogram_internal.h new file mode 100644 index 0000000..94799e9 --- /dev/null +++ b/histogram_internal.h @@ -0,0 +1,6 @@ +#pragma once +#include +#include + +void +find_minmax(const std::vector& numbers, double& min, double& max); diff --git a/lab03.cbp b/lab03.cbp index 235e2c9..d9fa0eb 100644 --- a/lab03.cbp +++ b/lab03.cbp @@ -32,10 +32,12 @@ + + diff --git a/lab03.depend b/lab03.depend new file mode 100644 index 0000000..b8e0551 --- /dev/null +++ b/lab03.depend @@ -0,0 +1,29 @@ +# depslib dependency file v1.0 +1713734525 source:c:\users\Äàíà\desktop\lab03\main.cpp + + + "histogram.h" + "text.h" + "svg.h" + +1712947093 c:\users\Äàíà\desktop\lab03\histogram.h + + + +1713650480 source:c:\users\Äàíà\desktop\lab03\histogram.cpp + "histogram.h" + +1713561842 source:c:\users\Äàíà\desktop\lab03\text.cpp + "histogram.h" + +1712947492 c:\users\Äàíà\desktop\lab03\text.h + + + +1713734318 c:\users\Äàíà\desktop\lab03\svg.h + + + +1713714876 source:c:\users\Äàíà\desktop\lab03\svg.cpp + "svg.h" + diff --git a/svg.cpp b/svg.cpp new file mode 100644 index 0000000..155a7be --- /dev/null +++ b/svg.cpp @@ -0,0 +1,48 @@ +#include "svg.h" + +void +svg_begin(double width, double height) { + std::cout << "\n"; + 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 fill_opacity, std::string stroke = "black", std::string fil = "black"){ + std::cout<< ""; +} + +void +show_histogram_svg(std::vector& bins, size_t & max_count,size_t & bin_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 = (IMAGE_WIDTH - TEXT_WIDTH)/max_count; + + svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); + + double top = 0; + for (size_t bin : bins) { + const double bin_width = BLOCK_WIDTH * bin; + svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin)); + double fill_opacity=double(bin) / max_count; + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, std::to_string(fill_opacity)); + top += BIN_HEIGHT; + } + svg_end(); +} + +void +svg_end() { + std::cout << "\n"; +} diff --git a/svg.h b/svg.h new file mode 100644 index 0000000..edd9a22 --- /dev/null +++ b/svg.h @@ -0,0 +1,13 @@ +#pragma once +#include +#include +void +show_histogram_svg(std::vector& bins, size_t & max_count,size_t & bin_count); +void +svg_begin(double width, double height); +void +svg_text(double left, double baseline, std::string text); +void +svg_rect(double x, double y, double width, double height, double fill_opacity, std::string stroke, std::string fil); +void +svg_end(); diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..0d003ba --- /dev/null +++ b/text.cpp @@ -0,0 +1,33 @@ +#include "histogram.h" + +void +show_histogram_text(std::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){ + std::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++){ + std::cout<<"*"; + } + std::cout << std::endl; + } + } + else{ + for (size_t i = 0; i < bin_count; i++) + { + printf("%3d|", bins[i]); + for (size_t j = 0; j < bins[i]; j++){ + std::cout<<"*"; + } + std::cout << std::endl; + } + } + +} diff --git a/text.h b/text.h new file mode 100644 index 0000000..ac2152d --- /dev/null +++ b/text.h @@ -0,0 +1,6 @@ +#pragma once +#include +#include + +void +show_histogram_text(std::vector& bins, size_t & max_count,size_t & bin_count); 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..5ca2d51 --- /dev/null +++ b/unittest.cpp @@ -0,0 +1,12 @@ +#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({1}, min, max); + CHECK(min == 1); + CHECK(max == 1); +} diff --git a/unittest.depend b/unittest.depend new file mode 100644 index 0000000..014970a --- /dev/null +++ b/unittest.depend @@ -0,0 +1,61 @@ +# depslib dependency file v1.0 +1713650480 source:c:\users\Äàíà\desktop\lab03\histogram.cpp + "histogram.h" + +1712947093 c:\users\Äàíà\desktop\lab03\histogram.h + + + +1713715686 source:c:\users\Äàíà\desktop\lab03\unittest.cpp + "doctest.h" + "histogram_internal.h" + +1713636110 c:\users\Äàíà\desktop\lab03\doctest.h + + + + + + + "doctest_fwd.h" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1712947904 c:\users\Äàíà\desktop\lab03\histogram_internal.h + + + diff --git a/unittest1.cbp b/unittest1.cbp new file mode 100644 index 0000000..ff80144 --- /dev/null +++ b/unittest1.cbp @@ -0,0 +1,41 @@ + + + + + + diff --git a/unittest1.cpp b/unittest1.cpp new file mode 100644 index 0000000..87eaec9 --- /dev/null +++ b/unittest1.cpp @@ -0,0 +1,12 @@ +#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({-1, -2}, min, max); + CHECK(min == -2); + CHECK(max == -1); +} diff --git a/unittest2.cbp b/unittest2.cbp new file mode 100644 index 0000000..863ab5e --- /dev/null +++ b/unittest2.cbp @@ -0,0 +1,41 @@ + + + + + + diff --git a/unittest2.cpp b/unittest2.cpp new file mode 100644 index 0000000..c08c417 --- /dev/null +++ b/unittest2.cpp @@ -0,0 +1,12 @@ +#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({1,1}, min, max); + CHECK(min == 1); + CHECK(max == 1); +}