Родитель
d83bbfd7f3
Сommit
501ab86ef1
@ -1,37 +1,42 @@
|
|||||||
#include "text.h"
|
#include "text.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <iomanip>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void show_histogram_text(vector<size_t> bins, size_t bin_count) {
|
void show_histogram_text(vector<size_t> bins, size_t bin_count) {
|
||||||
const size_t SCREEN_WIDTH = 80;
|
const size_t SCREEN_WIDTH = 80;
|
||||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 15;
|
||||||
double max_bin;
|
size_t max_bin = bins[0];
|
||||||
size_t height;
|
size_t total_count = 0;
|
||||||
max_bin = bins[0];
|
|
||||||
height = bins[0];
|
|
||||||
for (size_t i = 0; i < bin_count; i++) {
|
for (size_t i = 0; i < bin_count; i++) {
|
||||||
if (bins[i] > max_bin) {
|
if (bins[i] > max_bin) {
|
||||||
max_bin = bins[i];
|
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++) {
|
for (size_t i = 0; i < bin_count; i++) {
|
||||||
if (bins[i] < 100) cout << " ";
|
cout << setw(3) << bins[i] << " |";
|
||||||
if (bins[i] < 10) cout << " ";
|
|
||||||
cout << bins[i] << "|";
|
size_t bar_length;
|
||||||
if (flag == true) {
|
if (max_bin > MAX_ASTERISK) {
|
||||||
height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_bin);
|
bar_length = static_cast<size_t>((static_cast<double>(bins[i]) / max_bin) * MAX_ASTERISK);
|
||||||
}
|
} else {
|
||||||
else {
|
bar_length = bins[i];
|
||||||
height = bins[i];
|
|
||||||
}
|
}
|
||||||
for (size_t j = 0; j < height; j++) {
|
|
||||||
|
for (size_t j = 0; j < bar_length; j++) {
|
||||||
cout << "*";
|
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;
|
cout << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Загрузка…
Ссылка в новой задаче