KuybedaAY 4 дней назад
Родитель 8ada9f3fdd
Сommit 49d11fa294

@ -7,91 +7,82 @@ using namespace std;
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
int main() {
// Ñòðóêòóðà äëÿ âõîäíûõ äàííûõ
struct Input {
vector<double> numbers;
size_t bin_count{};
};
// Ôóíêöèÿ ââîäà äàííûõ
Input
input_data() {
Input in;
size_t number_count;
cerr << "Enter number count: ";
cin >> number_count;
vector<double> numbers(number_count);
in.numbers.resize(number_count);
cerr << "Enter " << number_count << " numbers: ";
for (size_t i = 0; i < number_count; i++) {
cin >> numbers[i];
cin >> in.numbers[i];
}
size_t bin_count;
cerr << "Enter bin count: ";
cin >> bin_count;
cin >> in.bin_count;
return in;
}
double min = numeric_limits<double>::max();
double max = numeric_limits<double>::lowest();
// Ôóíêöèÿ ïîèñêà ìèíèìóìà è ìàêñèìóìà
void
find_minmax(const vector<double>& numbers, double& min, double& max) {
min = numeric_limits<double>::max();
max = numeric_limits<double>::lowest();
for (double x : numbers) {
if (x < min) {
min = x;
}
if (x > max) {
max = x;
}
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;
if (max == min) {
return vector<size_t>(bin_count, numbers.size());
}
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;
}
for (double x : numbers) {
size_t bin_index = static_cast<size_t>((x - min) / bin_size);
if (bin_index >= bin_count) bin_index = bin_count - 1;
bins[bin_index]++;
}
if (!found) {
bins[bin_count - 1]++;
}
}
return bins;
}
// Ôóíêöèÿ âûâîäà ãèñòîãðàììû
void
show_histogram_text(const vector<size_t>& bins) {
size_t max_count = 0;
for (size_t count : bins) {
if (count > max_count) {
max_count = count;
if (count > max_count) max_count = count;
}
}
cerr << "Bin counts:" << endl;
for (size_t j = 0; j < bins.size(); j++) {
size_t height = (max_count > MAX_ASTERISK)
? static_cast<size_t>(MAX_ASTERISK * (static_cast<double>(bins[j]) / max_count))
: bins[j];
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) {
@ -100,14 +91,16 @@ int main() {
cout << bins[j] << "|";
}
// Âûâîäèì ñèìâîëû '*' â ñîîòâåòñòâèè ñ âûñîòîé
for (size_t k = 0; k < height; k++) {
cout << "*";
}
for (size_t k = 0; k < height; k++) cout << "*";
cout << endl;
}
return 0;
}
int
main() {
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins);
return 0;
}

Загрузка…
Отмена
Сохранить