Вы не можете выбрать более 25 тем
Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
43 строки
1.2 KiB
C++
43 строки
1.2 KiB
C++
#include "text.h"
|
|
#include <iostream>
|
|
#include <iomanip>
|
|
using namespace std;
|
|
|
|
void show_histogram_text(vector<size_t> bins, size_t bin_count) {
|
|
const size_t SCREEN_WIDTH = 80;
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 15;
|
|
size_t max_bin = bins[0];
|
|
size_t total_count = 0;
|
|
|
|
for (size_t i = 0; i < bin_count; i++) {
|
|
if (bins[i] > max_bin) {
|
|
max_bin = bins[i];
|
|
}
|
|
total_count += bins[i];
|
|
}
|
|
|
|
for (size_t i = 0; i < bin_count; i++) {
|
|
cout << setw(3) << bins[i] << " |";
|
|
|
|
size_t bar_length;
|
|
if (max_bin > MAX_ASTERISK) {
|
|
bar_length = static_cast<size_t>((static_cast<double>(bins[i]) / max_bin) * MAX_ASTERISK);
|
|
} else {
|
|
bar_length = bins[i];
|
|
}
|
|
|
|
for (size_t j = 0; j < bar_length; j++) {
|
|
cout << "*";
|
|
}
|
|
|
|
if (total_count > 0) {
|
|
int percentage = static_cast<int>((static_cast<double>(bins[i]) / total_count) * 100 + 0.5);
|
|
cout << setw(SCREEN_WIDTH - 7 - bar_length) << right << percentage << "%";
|
|
} else {
|
|
cout << setw(SCREEN_WIDTH - 7 - bar_length) << right << "0%";
|
|
}
|
|
|
|
cout << endl;
|
|
}
|
|
}
|