master
TiutinMO 5 месяцев назад
Родитель d83bbfd7f3
Сommit 501ab86ef1

@ -1,6 +1,7 @@
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include "svg.h"
using namespace std;
@ -39,22 +40,34 @@ void show_histogram_svg(const vector<size_t>& bins) {
const auto TEXT_BASELINE = 20;
const auto TEXT_WIDTH = 50;
const auto BIN_HEIGHT = 30;
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH - 50;
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
double top = 0;
size_t max_count = bins[0];
size_t total_count = 0;
for (size_t bin : bins) {
if (max_count < bin) {
max_count = bin;
}
total_count += bin;
}
for (size_t bin : bins) {
const double bin_width = MAX_WIDTH * (static_cast<double>(bin) / max_count);
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", "red");
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", "#3399ff");
if (total_count > 0) {
int percentage = static_cast<int>((static_cast<double>(bin) / total_count) * 100 + 0.5);
string percentage_text = to_string(percentage) + "%";
svg_text(TEXT_WIDTH + bin_width + 10, top + TEXT_BASELINE, percentage_text);
}
top += BIN_HEIGHT;
}

@ -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;
}
}

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