From 3fe4023a613dba07987af706400b629e7d456517 Mon Sep 17 00:00:00 2001 From: TekotovaVA Date: Sun, 14 May 2023 20:38:10 +0300 Subject: [PATCH] =?UTF-8?q?=D1=84=D0=B8=D0=BD=D0=B0=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cs-lab34.cpp | 82 ++++++++++++------------------------------ histogram.cpp | 43 +++++++++++++++++++++++ histogram.h | 7 ++++ histogram_internal.h | 7 ++++ svg.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++ svg.h | 8 +++++ text.cpp | 26 ++++++++++++++ text.h | 7 ++++ 8 files changed, 205 insertions(+), 59 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/cs-lab34.cpp b/cs-lab34.cpp index 2afad2d..361eea4 100644 --- a/cs-lab34.cpp +++ b/cs-lab34.cpp @@ -1,65 +1,29 @@ +#include "text.h" +#include "histogram.h" +#include "svg.h" +#include "histogram_internal.h" #include #include using namespace std; - -int main() +struct Input { + vector numbers; + size_t bin_count{}; +}; +Input input_data() { - size_t num, po, i; - double max, min, m = 0, le, ri; - cout << "num="; - cin >> num; - cout << "po="; - cin >> po; - vector mas(num); - vector ch(po); - for (i = 0; i < num; i++) - { - cout << "mas [" << i << "]="; - cin >> mas[i]; - } - cout << endl; - max = mas[0]; - min = mas[0]; - for (i = 1; i < num; i++) - { - if (max < mas[i]) - max = mas[i]; - if (min > mas[i]) - min = mas[i]; - } - le = min; - ri = le + (max - min) / po; - int kol = 0; - while (kol < po - 1) - { - for (i = 0; i < num; i++) - { - if (le <= mas[i] && ri > mas[i]) - ch[kol]++; - } - if (ch[kol] > m) - m = ch[kol]; - kol++; - le = ri; - ri = le + (max - min) / po; - } - for (i = 0; i < num; i++) - { - if ((ri <= mas[i] && le < mas[i]) || (le <= mas[i] && ri > mas[i])) - ch[kol] ++; - if (ch[kol] > m) - m = ch[kol]; + size_t number_count; + cin >> number_count; + Input in; + in.numbers.resize(number_count); + for (size_t i = 0; i < number_count; i++) { + cin >> in.numbers[i]; } - for (i = 0; i < po; i++) - { - int d = m - ch[i]; - if (d != 0) - for (int j = 0; j < d; j++) - cout << " "; - for (size_t j = 0; j < ch[i]; j++) - cout << "*"; - cout << "|" << ch[i]; - cout << endl; - } - return 0; + cin >> in.bin_count; + return in; +} +int main() +{ + Input in = input_data(); + auto ch = make_histogram(in.numbers, in.bin_count); + show_histogram_svg(ch); } \ No newline at end of file diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..d945fe9 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,43 @@ +#include "histogram.h" +#include "histogram_internal.h" +#include +#include +using namespace std; + +void find_minmax(const vector & mas, double& min, double& max) +{ + for (size_t i = 0; i < mas.size(); i++) + { + if (max < mas[i]) + max = mas[i]; + if (min > mas[i]) + min = mas[i]; + } +} +vector make_histogram(const vector& mas, size_t bin_count) +{ + size_t i; + vector ch(bin_count); + double max = mas[0], min = mas[0], le, ri; + find_minmax(mas, min, max); + le = min; + ri = le + (max - min) / bin_count; + int kol = 0; + while (kol < bin_count - 1) + { + for (i = 0; i < mas.size(); i++) + { + if (le <= mas[i] && ri > mas[i]) + ch[kol]++; + } + kol++; + le = ri; + ri = le + (max - min) / bin_count; + } + for (i = 0; i < mas.size(); i++) + { + if ((ri <= mas[i] && le < mas[i]) || (le <= mas[i] && ri > mas[i])) + ch[kol] ++; + } + return ch; +} \ No newline at end of file diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..69c7c0f --- /dev/null +++ b/histogram.h @@ -0,0 +1,7 @@ +#ifndef histogram +#define histogram +#include + +std::vector make_histogram(const std::vector& mas, size_t bin_count); + +#endif //histogram diff --git a/histogram_internal.h b/histogram_internal.h new file mode 100644 index 0000000..feb6a9b --- /dev/null +++ b/histogram_internal.h @@ -0,0 +1,7 @@ +#ifndef histogram_internal +#define histogram_internal +#include + +void find_minmax(const std::vector & mas, double& min, double& max); + +#endif //histogram_internal \ No newline at end of file diff --git a/svg.cpp b/svg.cpp new file mode 100644 index 0000000..9e9b589 --- /dev/null +++ b/svg.cpp @@ -0,0 +1,84 @@ +#include "svg.h" +#include +#include +#include +#include +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 fill) { + cout << ""; +} + +void pers(int bin, int sum, int& x) { + x = round(bin * 100 / sum); +} + +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); + double top = 0; + const size_t MAX_ASTERISK = IMAGE_WIDTH - TEXT_WIDTH; + int maxb = bins[0], sum= bins[0]; + for (size_t j = 1; j < bins.size(); j++) + { + if (bins[j] > maxb) maxb = bins[j]; + sum += bins[j]; + } + + int maxb1 = maxb * BLOCK_WIDTH; + + for (size_t bin : bins) { + + double bin_width; + + + if (maxb1 > MAX_ASTERISK) + { + bin_width = BLOCK_WIDTH * MAX_ASTERISK * (bin / maxb1); + } + else bin_width = BLOCK_WIDTH * bin; + + svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "red", "#ffeeee"); + int x = 0; + pers(bin, sum, x); + svg_text((TEXT_LEFT+ TEXT_WIDTH+ bin_width), top + TEXT_BASELINE, (to_string(x)+ " % ")); + top += BIN_HEIGHT; + } + svg_end(); + + +} + + + diff --git a/svg.h b/svg.h new file mode 100644 index 0000000..e25c081 --- /dev/null +++ b/svg.h @@ -0,0 +1,8 @@ +#ifndef svg +#define svg +#include + +void pers(int bin, int sum, int& x); +void show_histogram_svg(const std::vector & bins); + +#endif //svg \ No newline at end of file diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..924c78e --- /dev/null +++ b/text.cpp @@ -0,0 +1,26 @@ +#include "text.h" +#include +#include +using namespace std; + +void show_histogram_text(vector& ch, size_t bin_count) +{ + int m = 0, i; + for (i = 0; i < bin_count; i++) + if (ch[i] > m) + m = ch[i]; + for (i = 0; i < bin_count; i++) + { + if (ch[i] < 10) + cout << " "; + else if (ch[i] < 100) + cout << " "; + cout << ch[i] << "|"; + size_t k = ch[i]; + if (m > 76) + k = 76 * (static_cast (ch[i]) / m); + for (size_t j = 0; j < k; j++) + cout << "*"; + cout << endl; + } +} \ No newline at end of file diff --git a/text.h b/text.h new file mode 100644 index 0000000..928718f --- /dev/null +++ b/text.h @@ -0,0 +1,7 @@ +#ifndef text +#define text +#include + +void show_histogram_text(std::vector& ch); + +#endif //text \ No newline at end of file