code: индивидуальный вариант

main
TabolinIA 11 месяцев назад
Родитель 2de0e2d5f9
Сommit 47c9936890

@ -10,6 +10,7 @@ using namespace std;
struct Input {
vector<double> numbers;
size_t bin_count{};
size_t user_block_width;
};
Input
@ -25,6 +26,16 @@ input_data() {
}
cerr << "Enter bin count: ";
cin >> in.bin_count;
in.user_block_width = 15;
do {
cerr << "Enter block width: ";
cin >> in.user_block_width;
if ((in.user_block_width < 3) || (in.user_block_width > 30)){
cerr << "Error: block width must be included in the interval: [3; 30]" << endl;
}
}
while ((in.user_block_width < 3) || (in.user_block_width > 30));
return in;
}
@ -34,6 +45,6 @@ main()
{
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_svg(bins);
show_histogram_svg(bins, in.user_block_width);
return 0;
}

@ -30,7 +30,7 @@ void svg_rect(double x, double y, double width, double height, string stroke = "
void
show_histogram_svg(const vector<size_t>& bins) {
show_histogram_svg(const vector<size_t>& bins, size_t user_block_width) {
const auto IMAGE_WIDTH = 400;
const auto IMAGE_HEIGHT = 300;
@ -38,7 +38,7 @@ 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 BLOCK_WIDTH = 10;
const auto BLOCK_WIDTH = user_block_width;
const size_t MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH - 1;
@ -49,27 +49,24 @@ show_histogram_svg(const vector<size_t>& bins) {
}
}
if (maxbin > MAX_WIDTH) {
svg_begin(400, 300);
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
double top = 0;
for (size_t bin : bins) {
const double bin_width = MAX_WIDTH * (bin / maxbin);
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
top += BIN_HEIGHT;
}
svg_end();
}
svg_begin(400, 300);
double top = 0;
for (size_t bin : bins) {
const double bin_width = BLOCK_WIDTH * bin;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
top += BIN_HEIGHT;
if (maxbin<=76){
for (size_t bin : bins) {
double bin_width = BLOCK_WIDTH * bin;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
top += BIN_HEIGHT;
}
svg_end();
} else {
for (size_t bin : bins) {
double bin_width= MAX_WIDTH * (static_cast<double>(bin) / maxbin);
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
top += BIN_HEIGHT;
}
svg_end();
}
svg_end();
}

@ -1,6 +1,6 @@
#ifndef SVG_H_INCLUDED
#define SVG_H_INCLUDED
void
show_histogram_svg(const std::vector<size_t>& bins);
show_histogram_svg(const std::vector<size_t>& bins, size_t user_block_width);
#endif // SVG_H_INCLUDED

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