Родитель
d83bbfd7f3
Сommit
501ab86ef1
@ -1,37 +1,42 @@
|
||||
#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 - 3 - 1;
|
||||
double max_bin;
|
||||
size_t height;
|
||||
max_bin = bins[0];
|
||||
height = bins[0];
|
||||
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];
|
||||
}
|
||||
|
||||
bool flag = false;
|
||||
if (max_bin > 80) {
|
||||
flag = true;
|
||||
}
|
||||
for (size_t i = 0; i < bin_count; i++) {
|
||||
if (bins[i] < 100) cout << " ";
|
||||
if (bins[i] < 10) cout << " ";
|
||||
cout << bins[i] << "|";
|
||||
if (flag == true) {
|
||||
height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_bin);
|
||||
}
|
||||
else {
|
||||
height = bins[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 < height; j++) {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Загрузка…
Ссылка в новой задаче