code: lab4
Этот коммит содержится в:
14
main.cpp
14
main.cpp
@@ -11,10 +11,10 @@ struct Input {
|
|||||||
size_t bin_count{};
|
size_t bin_count{};
|
||||||
};
|
};
|
||||||
|
|
||||||
Input input_data() {
|
Input input_data(istream& in1) {
|
||||||
size_t number_count;
|
size_t number_count;
|
||||||
cerr << "Enter number count: ";
|
cerr << "Enter number count: ";
|
||||||
cin >> number_count;
|
in1 >> number_count;
|
||||||
|
|
||||||
Input in;
|
Input in;
|
||||||
|
|
||||||
@@ -22,11 +22,11 @@ Input input_data() {
|
|||||||
|
|
||||||
for (size_t i = 0; i < number_count; i++)
|
for (size_t i = 0; i < number_count; i++)
|
||||||
{
|
{
|
||||||
cin >> in.numbers[i];
|
in1 >> in.numbers[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
cerr << "Enter number bin: ";
|
cerr << "Enter number bin: ";
|
||||||
cin >> in.bin_count;
|
in1 >> in.bin_count;
|
||||||
|
|
||||||
return in;
|
return in;
|
||||||
}
|
}
|
||||||
@@ -35,9 +35,9 @@ Input input_data() {
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
auto in = input_data();
|
auto in = input_data(cin);
|
||||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||||
//show_histogram_text(bins, in.bin_count);
|
show_histogram_text(bins, in.bin_count);
|
||||||
show_histogram_svg(bins);
|
//show_histogram_svg(bins);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
20
svg.cpp
20
svg.cpp
@@ -37,8 +37,8 @@ void show_histogram_svg(const vector<size_t>& bins)
|
|||||||
const double IMAGE_HEIGHT = 300;
|
const double IMAGE_HEIGHT = 300;
|
||||||
const double TEXT_LEFT = 20;
|
const double TEXT_LEFT = 20;
|
||||||
const double TEXT_BASELINE = 20;
|
const double TEXT_BASELINE = 20;
|
||||||
const double TEXT_HEIGHT = 20;
|
const double TEXT_WIDTH = 50;
|
||||||
const double BIN_WIDTH = 30;
|
const double BIN_HEIGHT = 30;
|
||||||
const double BLOCK_WIDTH = 10;
|
const double BLOCK_WIDTH = 10;
|
||||||
|
|
||||||
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
||||||
@@ -52,25 +52,23 @@ void show_histogram_svg(const vector<size_t>& bins)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
double max_height = IMAGE_HEIGHT - TEXT_HEIGHT - BLOCK_WIDTH;
|
double max_width = IMAGE_WIDTH - TEXT_WIDTH;
|
||||||
|
double top = 0;
|
||||||
double left = TEXT_LEFT;
|
|
||||||
|
|
||||||
for (size_t count : bins)
|
for (size_t count : bins)
|
||||||
{
|
{
|
||||||
double bin_width;
|
double bin_width;
|
||||||
if (max_count > 0)
|
if (max_count > 0)
|
||||||
{
|
{
|
||||||
bin_height = static_cast<double>(count) / max_count * max_height;
|
bin_width = static_cast<double>(count) / max_count * max_width;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
bin_height = 0.0;
|
bin_width = 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
svg_text(left + BIN_WIDTH/2.0, IMAGE_HEIGHT - 5 ,to_string(count));
|
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(count));
|
||||||
svg_rect(left, IMAGE_HEIGHT - bin_height, BIN_WIDTH, bin_height, "blue", "#aaffaa");
|
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "blue", "#aaffaa");
|
||||||
left += BIN_WIDTH + BLOCK_WIDTH ;
|
top += BIN_HEIGHT;
|
||||||
}
|
}
|
||||||
svg_end();
|
svg_end();
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user