protect
Этот коммит содержится в:
@@ -2,21 +2,29 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
void find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
|
||||
for (double x : numbers)
|
||||
{
|
||||
if (x < min)
|
||||
|
||||
bool find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||
if (!(numbers.empty())) {
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (double x : numbers)
|
||||
{
|
||||
min = x;
|
||||
}
|
||||
else if (x > max)
|
||||
{
|
||||
max = x;
|
||||
if (x < min)
|
||||
{
|
||||
min = x;
|
||||
}
|
||||
else if (x > max)
|
||||
{
|
||||
max = x;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count)
|
||||
|
||||
@@ -4,6 +4,6 @@
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
void find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
||||
bool find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
||||
|
||||
#endif // HISTOGRAM_INTERNAL_H_INCLUDED
|
||||
|
||||
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 TEXT_LEFT = 20;
|
||||
const double TEXT_BASELINE = 20;
|
||||
const double TEXT_WIDTH = 50;
|
||||
const double BIN_HEIGHT = 30;
|
||||
const double TEXT_HEIGHT = 20;
|
||||
const double BIN_WIDTH = 30;
|
||||
const double BLOCK_WIDTH = 10;
|
||||
|
||||
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
||||
@@ -52,23 +52,25 @@ void show_histogram_svg(const vector<size_t>& bins)
|
||||
}
|
||||
}
|
||||
|
||||
double max_width = IMAGE_WIDTH - TEXT_WIDTH;
|
||||
double top = 0;
|
||||
double max_height = IMAGE_HEIGHT - TEXT_HEIGHT - BLOCK_WIDTH;
|
||||
|
||||
double left = TEXT_LEFT;
|
||||
|
||||
for (size_t count : bins)
|
||||
{
|
||||
double bin_width;
|
||||
if (max_count > 0)
|
||||
{
|
||||
bin_width = static_cast<double>(count) / max_count * max_width;
|
||||
bin_height = static_cast<double>(count) / max_count * max_height;
|
||||
}
|
||||
else
|
||||
{
|
||||
bin_width = 0.0;
|
||||
bin_height = 0.0;
|
||||
}
|
||||
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(count));
|
||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "blue", "#aaffaa");
|
||||
top += BIN_HEIGHT;
|
||||
svg_text(left + BIN_WIDTH/2.0, IMAGE_HEIGHT - 5 ,to_string(count));
|
||||
svg_rect(left, IMAGE_HEIGHT - bin_height, BIN_WIDTH, bin_height, "blue", "#aaffaa");
|
||||
left += BIN_WIDTH + BLOCK_WIDTH ;
|
||||
}
|
||||
svg_end();
|
||||
}
|
||||
|
||||
@@ -42,3 +42,10 @@ TEST_CASE("positive and negative numbers")
|
||||
CHECK(min == -3);
|
||||
CHECK(max == 5);
|
||||
}
|
||||
TEST_CASE("none elements")
|
||||
{
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
CHECK(!(find_minmax({}, min, max)));
|
||||
}
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user