From 5e0dfd755c11984bddaa489321a7e54b291be10f Mon Sep 17 00:00:00 2001 From: OgarkovIA Date: Sun, 29 Sep 2024 11:38:44 +0000 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D0=BB(=D0=B0)=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B2=20''?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.h | 9 +++++++ histogram_internal.h | 10 +++++++ main.cpp | 45 ++++++++++++++++++++++++++++++++ my_project.cbp | 58 +++++++++++++++++++++++++++++++++++++++++ svg.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 184 insertions(+) create mode 100644 histogram.h create mode 100644 histogram_internal.h create mode 100644 main.cpp create mode 100644 my_project.cbp create mode 100644 svg.cpp diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..0d8dc8c --- /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& numbers, size_t bin_count); + +#endif // HISTOGRAM_H_INCLUDED diff --git a/histogram_internal.h b/histogram_internal.h new file mode 100644 index 0000000..7d546c7 --- /dev/null +++ b/histogram_internal.h @@ -0,0 +1,10 @@ +#ifndef HISTOGRAM_INTERNAL_H_INCLUDED +#define HISTOGRAM_INTERNAL_H_INCLUDED +#include +#include + +bool find_minmax(const std::vector& numbers, double& min, double& max); + +std::string +bin_color(size_t bin, size_t max_count); +#endif // HISTOGRAM_INTERNAL_H_INCLUDED diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..cb09681 --- /dev/null +++ b/main.cpp @@ -0,0 +1,45 @@ +#include +#include +#include "histogram.h" +#include "svg.h" +#include + +using namespace std; + + struct Input { + vector numbers; + size_t bin_count{}; + }; + +Input +input_data(istream& in, bool prompt){ + + if (prompt == true) cerr << "Enter number_count: "; + + size_t number_count; + in >> number_count; + + Input data; + data.numbers.resize(number_count); + + if (prompt == true) cerr << "Enter numbers: "; + + for (size_t i = 0; i < number_count; i++){ + in >> data.numbers[i]; + } + + if (prompt == true) cerr << "Enter bin count: "; + + in >> data.bin_count; + return data; +} + +int +main() +{ + curl_global_init(CURL_GLOBAL_ALL); + auto in = input_data(cin, false); + auto bins = make_histogram(in.numbers, in.bin_count); + show_histogram_svg(bins); + return 0; +} diff --git a/my_project.cbp b/my_project.cbp new file mode 100644 index 0000000..fc76e36 --- /dev/null +++ b/my_project.cbp @@ -0,0 +1,58 @@ + + + + + + diff --git a/svg.cpp b/svg.cpp new file mode 100644 index 0000000..3897a52 --- /dev/null +++ b/svg.cpp @@ -0,0 +1,62 @@ +#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 fil){ + cout << ""; +} + +//string bin_color(size_t bin, size_t max_count){ +// return ("#" + to_string(10 - (bin * 9) / max_count) + to_string(10 - (bin * 9) / max_count) + to_string(10 - (bin * 9) / max_count)); +//} + +void +show_histogram_svg(const vector& bins) { + 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; + svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); + + size_t max_count = 0; + for (size_t x: bins) { + if (x > max_count) { + max_count = x; + } + } + + double top = 0; + for (size_t bin : bins) { + const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH) * bin / max_count; + //const auto color = bin_color(bin,max_count); + svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT,"red", "#ffeeee"); + top += BIN_HEIGHT; + } + svg_end(); +}