Вы не можете выбрать более 25 тем
Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
125 строки
2.3 KiB
C++
125 строки
2.3 KiB
C++
#include <iostream>
|
|
#include <vector>
|
|
#include <string>
|
|
|
|
#include "text.h"
|
|
#include "histogram.h"
|
|
#include "histogram_internal.h"
|
|
|
|
|
|
using namespace std;
|
|
|
|
void
|
|
svg_begin(double width, double height) {
|
|
cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
|
|
cout << "<svg ";
|
|
cout << "width='" << width << "' ";
|
|
cout << "height='" << height << "' ";
|
|
cout << "viewBox='0 0 " << width << " " << height << "' ";
|
|
cout << "xmlns='http://www.w3.org/2000/svg'>\n";
|
|
}
|
|
|
|
void
|
|
svg_end() {
|
|
cout << "</svg>\n";
|
|
}
|
|
|
|
void
|
|
svg_text(double left, double baseline, string text) {
|
|
cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>" << endl;
|
|
}
|
|
|
|
void svg_rect(double x, double y, double width, double height, string stroke, string fill) {
|
|
cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke = '" << stroke << "' fill='" << fill << "' />" << endl;
|
|
}
|
|
|
|
void
|
|
show_histogram_svg(const vector<size_t> bins) {
|
|
|
|
const auto IMAGE_WIDTH = 400;
|
|
const auto IMAGE_HEIGHT = 300;
|
|
const auto TEXT_LEFT = 20;
|
|
const auto TEXT_BASELINE = 20;
|
|
const auto TEXT_WIDTH = 50;
|
|
const auto BIN_HEIGHT = 30;
|
|
const auto BLOCK_WIDTH = 10;
|
|
|
|
|
|
|
|
|
|
svg_begin(400, 300);
|
|
svg_text(20, 35, to_string(bins[0]));
|
|
svg_rect(50, 0, bins[0] * 10, 30, "purple", "red");
|
|
svg_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void show_histogram_text(const vector <size_t> bins, const size_t bin_count) {
|
|
// Ìàêñèìàëüíîå â êîîðçèíàõ
|
|
|
|
|
|
const size_t SCREEN_WIDTH = 80;
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
|
|
int max_in_bins = 0;
|
|
|
|
|
|
for (int i = 0; i < bin_count; i++) {
|
|
if (bins[i] > max_in_bins) {
|
|
max_in_bins = bins[i];
|
|
}
|
|
}
|
|
|
|
|
|
if (max_in_bins > MAX_ASTERISK) {
|
|
|
|
for (int i = 0; i < bin_count; i++) {
|
|
if (bins[i] < 100) {
|
|
cout << " ";
|
|
}
|
|
if (bins[i] < 10) {
|
|
cout << " ";
|
|
}
|
|
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_in_bins);
|
|
|
|
cout << bins[i] << "|";
|
|
|
|
|
|
for (int j = 0; j < height; j++) {
|
|
cout << "*";
|
|
}
|
|
cout << endl;
|
|
}
|
|
|
|
|
|
}
|
|
else {
|
|
// Åñëè ìàêñèìàëüíîå ÷èñëî ñðåäè êîîðçèí ìåíüøå èëè ðàâíî 76
|
|
for (int i = 0; i < bin_count; i++) {
|
|
if (bins[i] < 100) {
|
|
cout << " ";
|
|
}
|
|
if (bins[i] < 10) {
|
|
cout << " ";
|
|
}
|
|
|
|
|
|
cout << bins[i] << "|";
|
|
for (int j = 0; j < bins[i]; j++) {
|
|
cout << "*";
|
|
}
|
|
cout << endl;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} |