Вы не можете выбрать более 25 тем
Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
56 строки
1.9 KiB
C++
56 строки
1.9 KiB
C++
#include <iostream>
|
|
#include "text.h"
|
|
|
|
const size_t screen_width = 80;
|
|
const size_t max_asterisk = screen_width - 3 - 1;
|
|
|
|
static void
|
|
find_max (const std::vector<std::size_t>& numbers, std::size_t& max) {
|
|
max = numbers[0];
|
|
for (double element : numbers) {
|
|
if (element > max) {
|
|
max = element;
|
|
}
|
|
}
|
|
}
|
|
|
|
void
|
|
show_histogram_text (const std::vector<std::size_t>& bins) {
|
|
std::size_t max_bin;
|
|
find_max(bins, max_bin);
|
|
double modifier;
|
|
|
|
/*
|
|
* In case when maximum bin > 76, histogram will be scaled according to
|
|
* the formula "max_asterisk * (static_cast<double>(bins[i]) / max_bin)"
|
|
* or "bins[i] * (static_cast<double>(max_asterisk) / max_bin)".
|
|
* In other case, histogram won't be scaled, i.e, asterisk count depends on the current bin number.
|
|
*/
|
|
|
|
if (max_bin > max_asterisk) {
|
|
modifier = static_cast<double>(max_asterisk) / (max_bin);
|
|
} else {
|
|
modifier = 1;
|
|
}
|
|
|
|
for (std::size_t i = 0; i < bins.size(); ++i) { //Histogram output with alignment, if necessary.
|
|
if (bins[i] >= 10) {
|
|
if (bins[i] >= 100) {
|
|
std::cout << bins[i] << '|';
|
|
} //Output a three-digit number.
|
|
else {
|
|
std::cout << ' ' << bins[i] << '|';
|
|
} //Output a two-digit number with alignment.
|
|
} else {
|
|
std::cout << " " << bins[i] << '|';
|
|
} //Output a single-digit number with alignment.
|
|
|
|
|
|
size_t height = modifier * bins[i]; //Height stands for the number of output asterisks.
|
|
for (size_t k = 0; k < height; ++k) {
|
|
std::cout << '*';
|
|
}
|
|
std::cout << "\n";
|
|
}
|
|
}
|