From f49df8d998a1874295be2bacbadcd954fdc708e7 Mon Sep 17 00:00:00 2001 From: Danila Date: Sun, 23 Apr 2023 22:00:21 +0300 Subject: [PATCH] =?UTF-8?q?=D1=87=D0=B5=D1=82=D0=B2=D0=B5=D1=80=D1=82?= =?UTF-8?q?=D1=8B=D0=B9=20=D1=8D=D1=82=D0=B0=D0=BF=20=D1=81=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B0=D0=BD,=20=D0=B4=D0=BE=D1=88=D0=B5=D0=BB=20=D0=B4?= =?UTF-8?q?=D0=BE=205=D0=B3=D0=BE=20=D0=BF=D1=83=D0=BD=D0=BA=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- project/.gitignore | 2 ++ project/histogram.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++ project/histogram.h | 8 ++++++ project/main.cpp | 40 +++++++++++++++++++++++++++++ project/pr3.cbp | 45 ++++++++++++++++++++++++++++++++ project/text.cpp | 53 ++++++++++++++++++++++++++++++++++++++ project/text.h | 7 +++++ 7 files changed, 215 insertions(+) create mode 100644 project/.gitignore create mode 100644 project/histogram.cpp create mode 100644 project/histogram.h create mode 100644 project/main.cpp create mode 100644 project/pr3.cbp create mode 100644 project/text.cpp create mode 100644 project/text.h diff --git a/project/.gitignore b/project/.gitignore new file mode 100644 index 0000000..fba4e61 --- /dev/null +++ b/project/.gitignore @@ -0,0 +1,2 @@ +/obj +/bin diff --git a/project/histogram.cpp b/project/histogram.cpp new file mode 100644 index 0000000..8913643 --- /dev/null +++ b/project/histogram.cpp @@ -0,0 +1,60 @@ +#include +#include +#include "histogram.h" +using namespace std; + + +void find_minmax(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; + } + } + return; +} + +vector make_histogram (vector numbers, size_t bin_count) +{ + double min; + double max; + find_minmax (numbers, min, max); + double bin_size = (max - min) / bin_count; + vector bins(bin_count); + + for (size_t i = 0; i < numbers.size(); 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 (!found) + { + bins[bin_count - 1]++; + } + } + + return bins; +} + + + + + + diff --git a/project/histogram.h b/project/histogram.h new file mode 100644 index 0000000..ed9e7b4 --- /dev/null +++ b/project/histogram.h @@ -0,0 +1,8 @@ +#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/project/main.cpp b/project/main.cpp new file mode 100644 index 0000000..9515236 --- /dev/null +++ b/project/main.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include "histogram.h" +#include "text.h" +using namespace std; + +struct Input +{ + vector numbers; + size_t bin_count{}; +}; + +Input input_data() +{ + size_t number_count; + cerr << "Enter 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]; + } + cerr << "Enter bin count: "; + cin>> in.bin_count; + return in; +} + + + +int main() +{ + auto in = input_data(); + auto bins = make_histogram(in.numbers, in.bin_count); + show_histogram_text(bins, in.bin_count); + getch(); + return 0; +} diff --git a/project/pr3.cbp b/project/pr3.cbp new file mode 100644 index 0000000..925b16f --- /dev/null +++ b/project/pr3.cbp @@ -0,0 +1,45 @@ + + + + + + diff --git a/project/text.cpp b/project/text.cpp new file mode 100644 index 0000000..5ae4546 --- /dev/null +++ b/project/text.cpp @@ -0,0 +1,53 @@ +#include +#include +#include "text.h" +using namespace std; + +void show_histogram_text(vector bins, size_t bin_count) +{ + const size_t screen_width = 80; + const size_t max_asterisk = screen_width - 3 - 1; + size_t i,j; + double max_count; + max_count = bins[0]; + for (i=0; i< bin_count; i++) + { + if (max_countmax_asterisk) + { + flag=true; + } + for (j = 0; j < bin_count; j++) + { + if (bins[j] < 100) + { + cout << " "; + } + if (bins[j] < 10) + { + cout << " "; + } + cout << bins[j] << "|"; + + if (flag) + { + height = max_asterisk * (static_cast(bins[j]) / max_count); + } + else + { + height=bins[j]; + } + for (i = 0; i < height; i++) + { + cout << "*"; + } + cout << endl; + } +} diff --git a/project/text.h b/project/text.h new file mode 100644 index 0000000..bd0f200 --- /dev/null +++ b/project/text.h @@ -0,0 +1,7 @@ +#ifndef TEXT_H_INCLUDED +#define TEXT_H_INCLUDED + +void +show_histogram_text(std::vector bins, size_t bin_count); + +#endif // TEXT_H_INCLUDED