From 7c6d2f19daca12a2949f077ec724ed32a17880a3 Mon Sep 17 00:00:00 2001 From: KhodiukMR <KhodiukMR@mpei.ru> Date: Mon, 29 Apr 2024 15:38:29 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=98=D1=82=D0=BE=D0=B3=D0=BE=D0=B2?= =?UTF-8?q?=D1=8B=D0=B9=20=D0=B2=D0=B0=D1=80=D0=B8=D0=B0=D0=BD=D1=82=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CommonVar.cpp | 90 ++++++++++++++------------------------------ histogram.cpp | 43 +++++++++++++++++++++ histogram.h | 4 ++ histogram_internal.h | 3 ++ svg.cpp | 64 +++++++++++++++++++++++++++++++ svg.h | 6 +++ text.cpp | 39 +++++++++++++++++++ text.h | 6 +++ 8 files changed, 194 insertions(+), 61 deletions(-) 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 diff --git a/CommonVar.cpp b/CommonVar.cpp index 9bcb988..2e44fe9 100644 --- a/CommonVar.cpp +++ b/CommonVar.cpp @@ -1,75 +1,43 @@ #include <vector> #include <iostream> #include <string> +#include "histogram.h" +#include "text.h" +#include "svg.h" using namespace std; -int main() { - const size_t SCREEN_WIDTH = 80; - const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; - double maxx, minn, MaxCount = 0; - size_t NumberCount, BinCount; +struct Input { + vector<double> Numbers; + size_t BinCount{}; +}; + +Input +InputData() { + Input in; + size_t NumberCount; cerr << "Enter number count: "; cin >> NumberCount; cerr << "Enter bin count: "; - cin >> BinCount; - vector <double> Numbers(NumberCount); + cin >> in.BinCount; + in.Numbers.resize(NumberCount); for (int i = 0; i < NumberCount; i++) { - cin >> Numbers[i]; - } - vector <size_t> Bins(BinCount); - maxx = Numbers[0]; - minn = Numbers[0]; - for (auto x : Numbers) { - if (x > maxx) { - maxx = x; - } - if (x < minn) { - minn = x; - } + cin >> in.Numbers[i]; } - double BinSize = (maxx - minn) / BinCount; + return in; +} - for (size_t i = 0; i < NumberCount; i++) { - bool found = false; - for (size_t j = 0; (j < BinCount - 1) && !found; j++) { - auto lo = minn + j * BinSize; - auto hi = minn + (j + 1) * BinSize; - if ((lo <= Numbers[i]) && (Numbers[i] < hi)) { - Bins[j]++; - found = true; - if (Bins[j] > MaxCount) { - MaxCount = Bins[j]; - } - } - } - if (!found) { - Bins[BinCount - 1]++; - if (Bins[BinCount - 1] > MaxCount) { - MaxCount = Bins[BinCount - 1]; - } - } - } - for (auto bin : Bins) { - if (bin < 100) { - cout << " "; - } - if (bin < 10) { - cout << " "; - } - cout << bin << "|"; - size_t height = MAX_ASTERISK * (static_cast<double>(bin) / MaxCount); - if (MaxCount <= 76) { - for (int i = 0; i < bin; i++) { - cout << '*'; - } - } - else { - for (int i = 0; i < height; i++) { - cout << '*'; - } - } - cout << endl; - } +void FindMinMax(const vector<double>& Numbers, double& minn, double& maxx); + +vector<size_t> MakeHistogram(vector<double> Numbers, size_t BinCount, double& MaxCount); + +void ShowHistogrammText(vector<size_t> Bins); + +int main() { + double MaxCount = 0; + Input in = InputData(); + vector <size_t> Bins(in.BinCount); + Bins = MakeHistogram(in.Numbers, in.BinCount, MaxCount); + ShowHistogramSvg(Bins); return 0; } diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..7109f5d --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,43 @@ +#include "histogram.h" +#include <vector> +using namespace std; +void FindMinMax(const vector<double>& Numbers, double& minn, double& maxx) { + maxx = Numbers[0]; + minn = Numbers[0]; + for (auto x : Numbers) { + if (x > maxx) { + maxx = x; + } + if (x < minn) { + minn = x; + } + } +} + +vector<size_t> MakeHistogram(vector<double> Numbers, size_t BinCount, double& MaxCount) { + double maxx, minn; + FindMinMax(Numbers, minn, maxx); + double BinSize = (maxx - minn) / BinCount; + vector <size_t> Bins(BinCount); + for (size_t i = 0; i < Numbers.size(); i++) { + bool found = false; + for (size_t j = 0; (j < BinCount - 1) && !found; j++) { + auto lo = minn + j * BinSize; + auto hi = minn + (j + 1) * BinSize; + if ((lo <= Numbers[i]) && (Numbers[i] < hi)) { + Bins[j]++; + found = true; + if (Bins[j] > MaxCount) { + MaxCount = Bins[j]; + } + } + } + if (!found) { + Bins[BinCount - 1]++; + if (Bins[BinCount - 1] > MaxCount) { + MaxCount = Bins[BinCount - 1]; + } + } + } + return Bins; +} \ No newline at end of file diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..a6ab3ae --- /dev/null +++ b/histogram.h @@ -0,0 +1,4 @@ +#pragma once +#include <vector> +std::vector<size_t> +MakeHistogram(const std::vector<double> Numbers, size_t BinCount, double& MaxCount); \ No newline at end of file diff --git a/histogram_internal.h b/histogram_internal.h new file mode 100644 index 0000000..81c293d --- /dev/null +++ b/histogram_internal.h @@ -0,0 +1,3 @@ +#pragma once +#include <vector> +void FindMinMax(const std::vector<double>& Numbers, double& minn, double& maxx); \ No newline at end of file diff --git a/svg.cpp b/svg.cpp new file mode 100644 index 0000000..7fba8f8 --- /dev/null +++ b/svg.cpp @@ -0,0 +1,64 @@ +#include <iostream> +#include "svg.h" +#include <vector> +#include <string> +using namespace std; + +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; + +void +SvgBegin(double width, double height) { + cout << "<?xml version='1.0' encoding='UTF-8'?>\n"; + cout << "<svg "; + cout << "width='" << width << "' "; + cout << "height='" << height << "' "; + cout << "viewBox='0 0 " << width << " " << height << "' "; + cout << "xmlns='http://www.w3.org/2000/svg'>\n"; +} + +void +SvgEnd() { + cout << "</svg>\n"; +} + +void +SvgText(double left, double baseline, string text) { + cout << "<text x='" << left << "' y='" << baseline << "' >" << text << "</text>\n"; +} + +void SvgRect(double x, double y, double width, double height, string stroke = "black", string fill = "black") { + cout << "<rect x ='" << x << "' y ='" << y << "' width ='" << width << "' height ='" << height << "' stroke ='" << stroke << "' fill ='" << fill << "' />\n"; +} + +void +ShowHistogramSvg(const vector<size_t>& bins) { + double top = 0; + SvgBegin(400, 300); + + size_t MaxCount = 0; + for (auto bin : bins) { + if (MaxCount < bin) { + MaxCount = bin; + } + } + int k = (IMAGE_WIDTH - TEXT_WIDTH) / MaxCount * BLOCK_WIDTH; + + if (k > 1) k = 1; + + for (size_t bin : bins) { + bin = bin * k; + const double bin_width = BLOCK_WIDTH * bin; + SvgText(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); + SvgRect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "red", "#FF69B4"); + top += BIN_HEIGHT; + } + SvgEnd(); +} + + diff --git a/svg.h b/svg.h new file mode 100644 index 0000000..7bda522 --- /dev/null +++ b/svg.h @@ -0,0 +1,6 @@ +#pragma once +#include <vector> + +void +ShowHistogramSvg(const std::vector<size_t>& bins); + diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..e28158e --- /dev/null +++ b/text.cpp @@ -0,0 +1,39 @@ +#include "text.h" +#include <iostream> +using namespace std; +const size_t SCREEN_WIDTH = 80; +const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + +void ShowHistogrammText(vector<size_t> Bins) { + size_t MaxCount = 0; + for (auto bin : Bins) { + if (MaxCount < bin) { + MaxCount = bin; + } + } + for (auto bin : Bins) { + if (bin < 100) { + cout << " "; + } + if (bin < 10) { + cout << " "; + } + cout << bin << "|"; + size_t height = MAX_ASTERISK * (static_cast<double>(bin) / MaxCount); + if (MaxCount <= 76) { + for (int i = 0; i < bin; i++) { + cout << '*'; + } + } + else { + for (int i = 0; i < height; i++) { + cout << '*'; + } + } + cout << endl; + } +} + + + + \ No newline at end of file diff --git a/text.h b/text.h new file mode 100644 index 0000000..36a7f7e --- /dev/null +++ b/text.h @@ -0,0 +1,6 @@ +#pragma once +#include <vector> + + +void +ShowHistogrammText(std::vector<size_t> Bins); \ No newline at end of file