Вы не можете выбрать более 25 тем
Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
104 строки
2.4 KiB
C++
104 строки
2.4 KiB
C++
#include <iostream>
|
|
#include <vector>
|
|
|
|
using namespace std;
|
|
|
|
const size_t SCREEN_WIDTH = 80;
|
|
const size_t MAX_LENGTH = SCREEN_WIDTH - 3 - 1;
|
|
|
|
struct Input {
|
|
vector<double> numbers;
|
|
size_t bin_count{};
|
|
};
|
|
|
|
Input
|
|
input_data(){
|
|
size_t number_count;
|
|
cerr << "Enter number count:" << endl;
|
|
cin >> number_count;
|
|
Input in;
|
|
in.numbers.resize(number_count);
|
|
vector<double> numbers(number_count);
|
|
cerr << "Enter numbers:" << endl;
|
|
for (size_t i = 0; i < number_count; i++) {
|
|
cin >> in.numbers[i];
|
|
}
|
|
cerr << "Enter bin count:" << endl;
|
|
cin >> in.bin_count;
|
|
return in;
|
|
}
|
|
|
|
void
|
|
find_minmax(const std::vector<double>& numbers, double& min, double& max, bool& res){
|
|
if (numbers.size() == 0){
|
|
res = false;
|
|
return;
|
|
}
|
|
min = numbers[0];
|
|
max = numbers[0];
|
|
for (auto x : numbers) {
|
|
if (x < min) {
|
|
min = x;
|
|
}
|
|
else if (x > max) {
|
|
max = x;
|
|
}
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
auto in = input_data();
|
|
auto min = in.numbers[0];
|
|
auto max = in.numbers[0];
|
|
bool res = true;
|
|
find_minmax(in.numbers, min, max, res);
|
|
if (res == false){
|
|
cerr << "Number of elements cannot be equal to zero";
|
|
exit(1);
|
|
}
|
|
double bin_size = (max - min) / in.bin_count;
|
|
vector<size_t> bins(in.bin_count);
|
|
for (auto x : in.numbers) {
|
|
bool found = false;
|
|
for (auto j = 0; (j < in.bin_count - 1) && !found ; j++) {
|
|
if ((min + j * bin_size <= x) && (x < min + (j + 1) * bin_size)) {
|
|
bins[j] += 1;
|
|
found = true;
|
|
}
|
|
}
|
|
if (!found) bins[in.bin_count - 1]++;
|
|
}
|
|
auto max_count = bins[0];
|
|
for (auto x : bins) {
|
|
if (x > max_count) {
|
|
max_count = x;
|
|
}
|
|
}
|
|
for (auto i = 0; i < in.bin_count; i++) {
|
|
auto j = 100;
|
|
while (bins[i] < j) {
|
|
cout << " ";
|
|
j /= 10;
|
|
}
|
|
cout << bins[i] << "|";
|
|
if (max_count > MAX_LENGTH) {
|
|
auto count = bins[i];
|
|
size_t height = MAX_LENGTH * (static_cast<double>(count) / max_count);
|
|
j = 0;
|
|
while (j < height) {
|
|
cout << "*";
|
|
j++;
|
|
}
|
|
}
|
|
else {
|
|
j = 0;
|
|
while (j < bins[i]) {
|
|
cout << "*";
|
|
j++;
|
|
}
|
|
}
|
|
cout << endl;
|
|
}
|
|
return 0;
|
|
}
|