diff --git a/TWOPO.cpp b/TWOPO.cpp index f0650f1..9ea8754 100644 --- a/TWOPO.cpp +++ b/TWOPO.cpp @@ -1,5 +1,9 @@ #include <iostream> #include <vector> +#include "histogram.h" +#include "text.h" +#include "histogram_internal.h" + using namespace std; @@ -14,65 +18,18 @@ Input input_data() { cout << "������� ���������� ������: "; cin >> number_count; - Input in; // ��������� ��������� Input - in.numbers.resize(number_count); // �������� ������ ������� + Input in; + in.numbers.resize(number_count); cout << "������� ������:\n"; for (size_t i = 0; i < number_count; i++) { - cin >> in.numbers[i]; // ������ �������� ������� + cin >> in.numbers[i]; } cout << "������� ���������� ������: "; - cin >> in.bin_count; // ������ ���������� ������ - - return in; // ���������� ��������� -} - -//����� �������� � ��������� -void find_minmax(const vector<double>& numbers, double& min, double& max) { - min = numbers[0]; - max = numbers[0]; - for (double x : numbers) { - if (x < min) { - min = x; - } - if (x > max) { - max = x; - } - } -} - -//�������� ����������� -vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) { - double min, max; - find_minmax(numbers, min, max); + cin >> in.bin_count; - double bin_size = (max - min) / bin_count; - vector<size_t> bins(bin_count, 0); - - for (double number : numbers) { - size_t bin_index = static_cast<size_t>((number - min) / bin_size); - if (bin_index >= bin_count) { - bin_index = bin_count - 1; - } - bins[bin_index]++; - } - - return bins; -} - -//����� -void show_histogram_text(const vector<size_t>& bins, double min, double bin_size) { - for (size_t i = 0; i < bins.size(); i++) { - double lo = min + i * bin_size; - double up = min + (i + 1) * bin_size; - cout << bins[i] << " | "; - - for (size_t j = 0; j < bins[i]; j++) { - cout << "*"; - } - cout << endl; - } + return in; } int main() { @@ -86,7 +43,7 @@ int main() { double bin_size = (max - min) / in.bin_count; - show_histogram_text(bins, min, bin_size); + show_histogram_text(bins, bin_size); return 0; } diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..4920da2 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,36 @@ +#include <iostream> +#include <vector> +#include "histogram.h" +using namespace std; + +void find_minmax(const vector<double>& numbers, double& min, double& max) { + min = numbers[0]; + max = numbers[0]; + for (double x : numbers) { + if (x < min) { + min = x; + } + if (x > max) { + max = x; + } + } +} + +//�������� ����������� +vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) { + double min, max; + find_minmax(numbers, min, max); + + double bin_size = (max - min) / bin_count; + vector<size_t> bins(bin_count, 0); + + for (double number : numbers) { + size_t bin_index = static_cast<size_t>((number - min) / bin_size); + if (bin_index >= bin_count) { + bin_index = bin_count - 1; + } + bins[bin_index]++; + } + + return bins; +} diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..c66f9e7 --- /dev/null +++ b/histogram.h @@ -0,0 +1,10 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED + +#include <vector> + +std::vector<size_t> +make_histogram(const std::vector<double>& 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..ee7778f --- /dev/null +++ b/histogram_internal.h @@ -0,0 +1,10 @@ +#ifndef HISTOGRAM_INTERNAL_H_INCLUDED +#define HISTOGRAM_INTERNAL_H_INCLUDED + +#include <vector> + +std::vector<size_t> +find_minmax(const std::vector<double>& numbers, double& min, double& max); + + +#endif // HISTOGRAM_INTERNAL_H_INCLUDED diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..cde5b82 --- /dev/null +++ b/text.cpp @@ -0,0 +1,16 @@ +#include <iostream> +#include <vector> +#include "text.h" +using namespace std; + +//����� +void show_histogram_text(const vector<size_t>& bins, double bin_size) { + for (size_t i = 0; i < bins.size(); i++) { + cout << bins[i] << " | "; + + for (size_t j = 0; j < bins[i]; j++) { + cout << "*"; + } + cout << endl; + } +} diff --git a/text.h b/text.h new file mode 100644 index 0000000..2b075a1 --- /dev/null +++ b/text.h @@ -0,0 +1,8 @@ +#ifndef TEXT_H_INCLUDED +#define TEXT_H_INCLUDED + +#include <vector> + +void show_histogram_text(const std::vector<size_t>& bins, double bin_size); + +#endif // TEXT_H_INCLUDED