laba34 (BuntovaMS) 19 часов назад
Родитель 64f8e58dd4
Сommit 7662aadb54

@ -1,30 +1,28 @@
#include "histogram.h" #include "histogram.h"
bool find_minmax(vector<double> vec, double& min, double& max) { bool find_minmax(vector<double> vec, double& min, double& max) {
if (vec.size() == 0) {
if (vec.size()== 0){ cerr << "Empty vec";
cerr<<"Empty vec";
return false; return false;
} }
min = vec[0]; min = vec[0];
max = vec[0]; max = vec[0];
for (double x : vec) { for (double x : vec) {
if (x < min) min = x; if (x < min) {
if (x > max) max = x; min = x;
}
else if (x > max)
{
max = x;
}
} }
return true; return true;
} }
vector<size_t> make_histogram(size_t number, vector<double> vec) { vector<size_t> make_histogram(size_t number, vector<double> vec) {
vector<size_t> bins(number); vector<size_t> bins(number);
if (vec.empty()) return bins;
double mn, mx; double mn, mx;
find_minmax(vec, mn, mx); find_minmax(vec, mn, mx);
float bin_size = (mx - mn) / number; float bin_size = (mx - mn) / number;
for (size_t i = 0; i < vec.size(); i++) { for (size_t i = 0; i < vec.size(); i++) {
bool fl = false; bool fl = false;
for (size_t j = 0; (j < number - 1) && !fl; j++) { for (size_t j = 0; (j < number - 1) && !fl; j++) {
@ -37,7 +35,9 @@ vector<size_t> make_histogram(size_t number, vector<double> vec) {
} }
if (!fl) { if (!fl) {
bins[number - 1]++; bins[number - 1]++;
} }
} }
return bins; return bins;
} }

@ -1,10 +1,5 @@
#ifndef HISTOGRAM_H_INCLUDED
#define HISTOGRAM_H_INCLUDED
#include <vector> #include <vector>
#include <iostream> #include <iostream>
using namespace std; using namespace std;
vector<size_t> make_histogram(size_t number, vector<double> vec); vector<size_t> make_histogram(size_t number, vector<double> vec);
#endif // HISTOGRAM_H_INCLUDED

@ -20,58 +20,61 @@ svg_end()
} }
void void
svg_text(double left, double baseline, string text) svg_text(double left, double baseline, const string& text, const string& text_decoration)
{ {
cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>"; cout << "<text x='" << left << "' y='" << baseline << "' text-decoration='" << text_decoration << "'>" << text << "</text>";
} }
void void
svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black") svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black")
{ {
cout << "<rect x='"<<x<<"' y='"<<y<<"' width='"<<width<<"' height='"<<height<<"' stroke='"<<stroke<<"' fill='"<<fill<<"' />"; cout << "<rect x='"<<x<<"' y='"<<y<<"' width='"<<width<<"' height='"<<height<<"' stroke='"<<stroke<<"' fill='"<<fill<<"' />";
} }
void
void show_histogram_svg(const vector<size_t>& bins) { show_histogram_svg(const vector<size_t>& bins)
const double IMAGE_WIDTH = 400.0; {
const double IMAGE_HEIGHT = 300.0; string decoration;
const double LEFT_MARGIN = 50.0; cerr<<"Enter text decoration (none, underline, overline, line-through): ";
const double LABEL_MARGIN = 30.0; cin>>decoration;
const double BIN_HEIGHT = 30.0; while(!(decoration=="none"||decoration=="underline"||decoration=="overline"||decoration=="line-through"))
const double LABEL_OFFSET = 5.0; {
cerr<<"Invalid value. Please enter one of: none, underline, overline, line-through: ";
const double axis = IMAGE_WIDTH - LABEL_MARGIN; cin>>decoration;
const double MAX_WIDTH = axis - LEFT_MARGIN;
double mx = 0.0;
for (size_t v : bins) {
if (static_cast<double>(v) > mx) {
mx = static_cast<double>(v);
}
} }
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); const auto IMAGE_WIDTH = 400;
const auto IMAGE_HEIGHT = 300;
double top = 0.0; const auto TEXT_LEFT = 20;
for (size_t v : bins) { const auto TEXT_BASELINE = 20;
double bar_w; const auto TEXT_WIDTH = 50;
if (mx > 0) { const auto BIN_HEIGHT = 30;
bar_w = MAX_WIDTH * static_cast<double>(v) / mx; const auto BLACK = "black";
} else { const auto RED = "red";
bar_w = 0.0; const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH;
svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT);
double top = 0;
double max_count = bins[0];
for (size_t i = 0; i < bins.size(); i++)
{
if (max_count<bins[i])
{
max_count=bins[i];
} }
}
double x0 = axis - bar_w; for (size_t bin : bins)
svg_rect(x0, top, bar_w, BIN_HEIGHT, "green", "yellow"); {
double bin_width = (MAX_WIDTH)*(bin/max_count);
double label_x = x0 + bar_w + LABEL_OFFSET; svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin), decoration);
double label_y = top + BIN_HEIGHT / 2 + 5; svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, BLACK, RED);
svg_text(label_x, label_y, to_string(v));
top += BIN_HEIGHT; top += BIN_HEIGHT;
} }
svg_end(); svg_end();
} }

@ -1,46 +1,82 @@
#include "text.h" #include "text.h"
#include <iostream>
void show_histogram(std::vector<size_t> bins) { void show_histogram(std::vector<size_t> bins) {
bool gigant = false;
auto spaces = 0;
size_t mx_count = 0; size_t mx_count = 0;
bool need_scale = false; for (auto x : bins) {
for (size_t x : bins) { if (x > 76) {
gigant = true;
}
if (x > mx_count) { if (x > mx_count) {
mx_count = x; mx_count = x;
} }
if (x > 76) { auto len = 0;
need_scale = true; while (x > 0) {
x /= 10;
len++;
}
if (len > spaces) {
spaces = len;
} }
} }
size_t max_bar = need_scale ? 76 : mx_count; if (spaces == 1) {
size_t digits = 1; for (size_t i = 0; i < bins.size(); i++) {
for (size_t t = mx_count; t >= 10; t /= 10) { std::cout << " " << bins[i] << "|";
++digits; if (gigant) {
} if (bins[i] == mx_count) {
for (size_t j = 0; j < 76; j++) {
for (size_t count : bins) { std::cout << "*";
size_t bar_len = 0; }
if (need_scale) { }
if (count == mx_count) { else
bar_len = max_bar; {
} else { for (size_t j = 0; j < 76 * static_cast<double>(bins[i]) / mx_count; j++) {
bar_len = static_cast<size_t>(max_bar * static_cast<double>(count) / mx_count); std::cout << "*";
}
}
}
else
{
for (size_t j = 0; j < bins[i]; j++) {
std::cout << "*";
}
std::cout << std::endl;
} }
} else {
bar_len = count;
}
size_t spaces = max_bar - bar_len;
for (size_t i = 0; i < spaces; ++i) {
std::cout << ' ';
}
for (size_t i = 0; i < bar_len; ++i) {
std::cout << '*';
} }
std::cout << "| "; }
std::string s = std::to_string(count); else
for (size_t i = s.size(); i < digits; ++i) { {
std::cout << ' '; for (size_t i = 0; i < bins.size(); i++) {
int len = 1;
int k = bins[i];
for (; k /= 10; ++len);
while (len < spaces) {
std::cout << " ";
len++;
}
std::cout << bins[i];
std::cout << "|";
if (gigant) {
if (bins[i] == mx_count) {
for (size_t j = 0; j < 76; j++) {
std::cout << "*";
}
}
else
{
for (size_t j = 0; j < (76 * static_cast<double>(bins[i]) / mx_count - 1); j++) {
std::cout << "*";
}
}
}
else
{
for (size_t j = 0; j < bins[i]; j++) {
std::cout << "*";
}
}
std::cout << std::endl;
} }
std::cout << s << "\n";
} }
} }

@ -1,8 +1,4 @@
#ifndef TEXT_H_INCLUDED #include <iostream>
#define TEXT_H_INCLUDED
#include <vector> #include <vector>
using namespace std;
void show_histogram(const std::vector<size_t>& bins);
#endif // TEXT_H_INCLUDED void show_histogram(std::vector<size_t> bins);

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