Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

114 строки
2.2 KiB
C++

#include <iostream>
#include <vector>
#include <limits>
using namespace std;
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
int main() {
size_t number_count;
cerr << "Enter number count: ";
cin >> number_count;
vector<double> numbers(number_count);
cerr << "Enter " << number_count << " numbers: ";
for (size_t i = 0; i < number_count; i++) {
cin >> numbers[i];
}
size_t bin_count;
cerr << "Enter bin count: ";
cin >> bin_count;
double min = numeric_limits<double>::max();
double max = numeric_limits<double>::lowest();
for (double x : numbers) {
if (x < min) {
min = x;
}
if (x > max) {
max = x;
}
}
double bin_size = (max - min) / bin_count;
vector<size_t> bins(bin_count, 0);
for (size_t i = 0; i < numbers.size(); i++) {
bool found = false;
for (size_t j = 0; (j < bin_count - 1) && !found; j++) {
double lo = min + j * bin_size;
double hi = min + (j + 1) * bin_size;
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
bins[j]++;
found = true;
}
}
if (!found) {
bins[bin_count - 1]++;
}
}
size_t max_count = 0;
for (size_t count : bins) {
if (count > max_count) {
max_count = count;
}
}
cerr << "Bin counts:" << endl;
size_t height;
for (size_t j = 0; j < bin_count; j++) {
// Âû÷èñëÿåì âûñîòó ãèñòîãðàììû
if(max_count > 76){
height = (max_count > 0) ? static_cast<size_t>(76 * (static_cast<double>(bins[j]) / max_count)) : 0;
}
else{
height = bins[j];
}
// Âûâîäèì êîëè÷åñòâî â áèíå ñ ó÷åòîì ôîðìàòèðîâàíèÿ
if (bins[j] < 10) {
cout << " " << bins[j] << "|";
} else if (bins[j] < 100) {
cout << " " << bins[j] << "|";
} else {
cout << bins[j] << "|";
}
// Âûâîäèì ñèìâîëû '*' â ñîîòâåòñòâèè ñ âûñîòîé
for (size_t k = 0; k < height; k++) {
cout << "*";
}
cout << endl;
}
return 0;
}