From 196a901350faab93efa5f86ad88426b414c5c269 Mon Sep 17 00:00:00 2001 From: "laba34 (BuntovaMS)" Date: Wed, 18 Jun 2025 14:41:23 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B7=D0=B0=D1=89=D0=B8=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 +++ 18.cbp | 46 +++++++++++++++++++++++++++++ histogram.cpp | 43 +++++++++++++++++++++++++++ histogram.h | 5 ++++ main.cpp | 28 ++++++++++++++++++ svg.cpp | 80 +++++++++++++++++++++++++++++++++++++++++++++++++ svg.h | 9 ++++++ text.cpp | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++ text.h | 4 +++ 9 files changed, 301 insertions(+) create mode 100644 .gitignore create mode 100644 18.cbp create mode 100644 histogram.cpp create mode 100644 histogram.h create mode 100644 main.cpp create mode 100644 svg.cpp create mode 100644 svg.h create mode 100644 text.cpp create mode 100644 text.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..01fd935 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/bin +/obj +/*.layout +/*.depend diff --git a/18.cbp b/18.cbp new file mode 100644 index 0000000..e3e0fc4 --- /dev/null +++ b/18.cbp @@ -0,0 +1,46 @@ + + + + + + diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..de90e9a --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,43 @@ +#include "histogram.h" + +bool find_minmax(vector vec, double& min, double& max) { + if (vec.size() == 0) { + cerr << "Empty vec"; + return false; + } + min = vec[0]; + max = vec[0]; + for (double x : vec) { + if (x < min) { + min = x; + } + else if (x > max) + { + max = x; + } + } + return true; +} +vector make_histogram(size_t number, vector vec) { + vector bins(number); + double mn, mx; + find_minmax(vec, mn, mx); + float bin_size = (mx - mn) / number; + for (size_t i = 0; i < vec.size(); i++) { + bool fl = false; + for (size_t j = 0; (j < number - 1) && !fl; j++) { + auto lo = mn + j * bin_size; + auto hi = mn + (j + 1) * bin_size; + if ((lo <= vec[i]) && (vec[i] < hi)) { + bins[j]++; + fl = true; + } + } + if (!fl) { + bins[number - 1]++; + + } + } + return bins; +} + diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..2b63e9a --- /dev/null +++ b/histogram.h @@ -0,0 +1,5 @@ +#include +#include + +using namespace std; +vector make_histogram(size_t number, vector vec); diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..558fb56 --- /dev/null +++ b/main.cpp @@ -0,0 +1,28 @@ +#include "histogram.h" +#include "text.h" +#include "svg.h" + +struct Input { + vector vec; + size_t korz{}; +}; +Input input_data() { + Input in; + size_t n, korz; + + cerr << "Number of elements: "; + cin >> n; + in.vec.resize(n); + cerr << "Elements: "; + for (size_t i = 0; i < n; i++) + cin >> in.vec[i]; + cerr << "Enter bin count: "; + cin >> in.korz; + return in; +} + +int main() { + auto in = input_data(); + auto bins = make_histogram(in.korz, in.vec); + show_histogram_svg(bins); +} diff --git a/svg.cpp b/svg.cpp new file mode 100644 index 0000000..29c5a23 --- /dev/null +++ b/svg.cpp @@ -0,0 +1,80 @@ +#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, const string& text, const string& text_decoration) +{ + cout << "" << text << ""; +} + +void +svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black") +{ + cout << ""; +} + + +void +show_histogram_svg(const vector& bins) +{ + string decoration; + cerr<<"Enter text decoration (none, underline, overline, line-through): "; + cin>>decoration; + while(!(decoration=="none"||decoration=="underline"||decoration=="overline"||decoration=="line-through")) + { + cerr<<"Invalid value. Please enter one of: none, underline, overline, line-through: "; + cin>>decoration; + } + + 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 BLACK = "black"; + const auto RED = "red"; + const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH; + + + svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT); + + double top = 0; + double max_count = bins[0]; + for (size_t i = 0; i < bins.size(); i++) + { + if (max_count +#include +#include +#include + +using namespace std; +// Declaration for SVG histogram output +void show_histogram_svg(const vector& bins); diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..5eec270 --- /dev/null +++ b/text.cpp @@ -0,0 +1,82 @@ +#include "text.h" + +void show_histogram(std::vector bins) { + bool gigant = false; + auto spaces = 0; + size_t mx_count = 0; + for (auto x : bins) { + if (x > 76) { + gigant = true; + } + if (x > mx_count) { + mx_count = x; + } + auto len = 0; + while (x > 0) { + x /= 10; + len++; + } + if (len > spaces) { + spaces = len; + } + } + if (spaces == 1) { + for (size_t i = 0; i < bins.size(); i++) { + std::cout << " " << bins[i] << "|"; + if (gigant) { + if (bins[i] == mx_count) { + for (size_t j = 0; j < 76; j++) { + std::cout << "*"; + } + } + else + { + for (size_t j = 0; j < 76 * static_cast(bins[i]) / mx_count; j++) { + std::cout << "*"; + } + } + } + else + { + for (size_t j = 0; j < bins[i]; j++) { + std::cout << "*"; + } + std::cout << std::endl; + } + } + } + else + { + for (size_t i = 0; i < bins.size(); i++) { + int len = 1; + int k = bins[i]; + for (; k /= 10; ++len); + while (len < spaces) { + std::cout << " "; + len++; + } + std::cout << bins[i]; + std::cout << "|"; + if (gigant) { + if (bins[i] == mx_count) { + for (size_t j = 0; j < 76; j++) { + std::cout << "*"; + } + } + else + { + for (size_t j = 0; j < (76 * static_cast(bins[i]) / mx_count - 1); j++) { + std::cout << "*"; + } + } + } + else + { + 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..9db8b30 --- /dev/null +++ b/text.h @@ -0,0 +1,4 @@ +#include +#include + +void show_histogram(std::vector bins);