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

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

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

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

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

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