master
Varvara (ZeninaVA) 3 месяцев назад
Родитель d2fae250dc
Сommit 9333e93a79

@ -1,159 +1,26 @@
#include <iostream> #include "histogram.h"
#include <vector> #include "text.h"
#include "svg.h"
using namespace std;
struct Input { struct Input {
vector<double> numbers; vector<double> vec;
size_t bin_count{}; size_t korz{};
}; };
Input input_data() {
Input
input_data() {
size_t number_count, bin_count;
double minl, maxl, bin_size;
cerr << "Enter the number of elements: ";
cin >> number_count;
Input in; Input in;
in.numbers.resize(number_count); size_t n, korz;
vector<double> numbers(number_count); cerr << "Number of elements: ";
cin >> n;
cerr << "\nEnter " << number_count << " elements:" << endl; in.vec.resize(n);
for (size_t i = 0; i < number_count; i++) { cerr << "Elements: ";
cin >> in.numbers[i]; for (size_t i = 0; i < n; i++)
} cin >> in.vec[i];
cerr << "Enter bin count: ";
cin >> in.korz;
cerr << "Enter the number of bins: ";
cin >> in.bin_count;
return in; return in;
} }
void
find_minmax(const vector<double>& numbers, double& minl, double& maxl) {
minl = numbers[0];
maxl = numbers[0];
for (double x : numbers) {
if (x < minl) {
minl = x;
} else {
if (x > maxl) {
maxl = x;
}
}
}
}
vector<size_t>
make_histogram(const vector<double>& numbers, size_t& bin_count) {
double minn, maxn;
find_minmax(numbers, minn, maxn);
double bin_size = (maxn - minn) / bin_count;
vector<size_t> bins(bin_count, 0);
for (size_t i = 0; i < numbers.size(); i++) {
bool found = false;
for (size_t j = 0; (j < bin_count - 1) && !found; j++) {
auto lo = minn + j * bin_size;
auto hi = minn + (j + 1) * bin_size;
if ((numbers[i] >= lo) && (numbers[i] <= hi)) {
bins[j]++;
found = true;
}
}
if (!found) {
bins[bin_count - 1]++;
}
}
return bins;
}
void
show_histogram_text(const vector<size_t> bins, size_t bin_count) {
size_t max_count = bins[0];
for (size_t i = 1; i < bin_count; i++) {
if (bins[i] > max_count) {
max_count = bins[i];
}
}
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
short space_count;
cerr << "\nHistogram:" << endl;
if (max_count <= MAX_ASTERISK) {
for (size_t i = 0; i < bin_count; i++) {
if (bins[i] < 10) {
space_count = 2;
} else if (bins[i] >= 10 && bins[i] < 100) {
space_count = 1;
} else if (bins[i] >= 100 && bins[i] < 1000) {
space_count = 0;
}
for (size_t k = 0; k < space_count; k++) {
cout << " ";
}
cout << bins[i];
cout << "|";
for (size_t j = 0; j < bins[i]; j++) {
cout << "*";
}
cout << "\n";
}
} else {
for (size_t i = 0; i < bin_count; i++) {
if (bins[i] < 10) {
space_count = 2;
} else if (bins[i] >= 10 && bins[i] < 100) {
space_count = 1;
} else if (bins[i] >= 100 && bins[i] < 1000) {
space_count = 0;
}
for (size_t k = 0; k < space_count; k++) {
cout << " ";
}
cout << bins[i];
cout << "|";
size_t height = static_cast<size_t>(MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count));
for (size_t j = 0; j < height; j++) {
cout << "*";
}
cout << "\n";
}
}
}
int main() { int main() {
auto in = input_data(); auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count); auto bins = make_histogram(in.korz, in.vec);
show_histogram_text(bins, in.bin_count); show_histogram_svg(bins);
return 0;
} }

@ -28,7 +28,7 @@ svg_text(double left, double baseline, string 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 << "' />";
} }
@ -44,9 +44,9 @@ show_histogram_svg(const vector<size_t>& bins)
const auto TEXT_WIDTH = 50; const auto TEXT_WIDTH = 50;
const auto BIN_HEIGHT = 30; const auto BIN_HEIGHT = 30;
const auto BLOCK_WIDTH = 10; const auto BLOCK_WIDTH = 10;
const auto BLACK = "black"; const auto GREEN = "green"
const auto RED = "red"; const auto RED = "red";
const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH; const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT); svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT);
@ -55,17 +55,17 @@ show_histogram_svg(const vector<size_t>& bins)
double max_count = bins[0]; double max_count = bins[0];
for (size_t i = 0; i < bins.size(); i++) for (size_t i = 0; i < bins.size(); i++)
{ {
if (max_count<bins[i]) if (max_count < bins[i])
{ {
max_count=bins[i]; max_count = bins[i];
} }
} }
for (size_t bin : bins) for (size_t bin : bins)
{ {
double bin_width = (MAX_WIDTH)*(bin/max_count); double bin_width = (MAX_WIDTH) * (bin/max_count);
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); 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, GREEN, RED);
top += BIN_HEIGHT; top += BIN_HEIGHT;
} }

@ -5,6 +5,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <math.h> #include <math.h>
using namespace std; using namespace std;
void show_histogram_svg(const vector<size_t>& bins); void show_histogram_svg(const vector<size_t>& bins);

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