|
|
|
@ -1,7 +1,90 @@
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
struct Input{
|
|
|
|
|
std::vector<double> numbers;
|
|
|
|
|
size_t bin_count{};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Input input_data() {
|
|
|
|
|
size_t number_count;
|
|
|
|
|
std::cerr << "Enter number count: ";
|
|
|
|
|
std::cin >> number_count;
|
|
|
|
|
|
|
|
|
|
std::cerr << "Enter numbers: \n";
|
|
|
|
|
Input in;
|
|
|
|
|
in.numbers.resize(number_count);
|
|
|
|
|
for (size_t i = 0; i < number_count; ++i) {
|
|
|
|
|
std::cin >> in.numbers[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::cerr << "Enter bucket: ";
|
|
|
|
|
std::cin >> in.bin_count;
|
|
|
|
|
|
|
|
|
|
return in;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
size_t find_minmax(const std::vector<double>& numbers, double& min, double& max) {
|
|
|
|
|
if (numbers.empty()) {
|
|
|
|
|
min = max = 0;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
min = numbers[0];
|
|
|
|
|
max = numbers[0];
|
|
|
|
|
for (double number : numbers) {
|
|
|
|
|
if (number < min) {
|
|
|
|
|
min = number;
|
|
|
|
|
}
|
|
|
|
|
if (number > max) {
|
|
|
|
|
max = number;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count) {
|
|
|
|
|
std::vector<size_t> bins(bin_count, 0);
|
|
|
|
|
if (numbers.empty() || bin_count == 0) {
|
|
|
|
|
return bins;
|
|
|
|
|
}
|
|
|
|
|
double min, max;
|
|
|
|
|
find_minmax(numbers, min, max);
|
|
|
|
|
if (max == min) {
|
|
|
|
|
return bins; // Âîçâðàùàåì ïóñòûå êîðçèíû, òàê êàê ãèñòîãðàììà íå ìîæåò áûòü ïîñòðîåíà
|
|
|
|
|
}
|
|
|
|
|
double bin_width = (max - min) / bin_count;
|
|
|
|
|
for (double number : numbers) {
|
|
|
|
|
size_t bin_index = static_cast<size_t>((number - min) / bin_width);
|
|
|
|
|
if (bin_index >= bin_count) {
|
|
|
|
|
bin_index = bin_count - 1; // Îáðàáîòêà êðàéíåãî ñëó÷àÿ max == number
|
|
|
|
|
}
|
|
|
|
|
bins[bin_index]++;
|
|
|
|
|
}
|
|
|
|
|
return bins;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void show_histogram_text(const std::vector<size_t>& bins) {
|
|
|
|
|
const size_t SCREEN_WIDTH = 80;
|
|
|
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
|
|
|
size_t max_bin = 0;
|
|
|
|
|
for (size_t bin : bins) {
|
|
|
|
|
if (bin > max_bin) {
|
|
|
|
|
max_bin = bin;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (size_t bin_value : bins) {
|
|
|
|
|
if (bin_value < 100) std::cout << " ";
|
|
|
|
|
if (bin_value < 10) std::cout << " ";
|
|
|
|
|
std::cout << bin_value << " |";
|
|
|
|
|
size_t height = bin_value;
|
|
|
|
|
if (max_bin > MAX_ASTERISK) {
|
|
|
|
|
height = static_cast<size_t>(MAX_ASTERISK * (static_cast<double>(bin_value) / max_bin));
|
|
|
|
|
}
|
|
|
|
|
for (size_t i = 0; i < height; ++i) {
|
|
|
|
|
std::cout << "*";
|
|
|
|
|
}
|
|
|
|
|
std::cout << "\n";
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|