code: Вывод границ корзин
Этот коммит содержится в:
@@ -23,6 +23,26 @@ void find_minmax(const vector<double>& numbers, double& min, double& max) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void find_max_capacity(const vector<size_t>& bins, size_t& max_bin_capacity)
|
||||||
|
{
|
||||||
|
max_bin_capacity = bins[0];
|
||||||
|
for (size_t x : bins)
|
||||||
|
{
|
||||||
|
if (x > max_bin_capacity)
|
||||||
|
{
|
||||||
|
max_bin_capacity = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void find_range_border(const vector<double>& numbers, size_t bin_count, size_t bin_number, double& range_border)
|
||||||
|
{
|
||||||
|
double min_number, max_number;
|
||||||
|
find_minmax(numbers, min_number, max_number);
|
||||||
|
double bin_size = abs((max_number - min_number) / bin_count);
|
||||||
|
range_border = min_number + bin_number * bin_size;
|
||||||
|
}
|
||||||
|
|
||||||
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count)
|
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count)
|
||||||
{
|
{
|
||||||
double min_number, max_number;
|
double min_number, max_number;
|
||||||
@@ -32,7 +52,7 @@ vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count)
|
|||||||
{
|
{
|
||||||
bins[i] = 0;
|
bins[i] = 0;
|
||||||
}
|
}
|
||||||
double bin_size = (max_number - min_number) / bin_count;
|
double bin_size = abs((max_number - min_number) / bin_count);
|
||||||
for (size_t i = 0; i < numbers.size(); i++)
|
for (size_t i = 0; i < numbers.size(); i++)
|
||||||
{
|
{
|
||||||
bool found = false;
|
bool found = false;
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count);
|
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count);
|
||||||
|
void find_range_border(const std::vector<double>& numbers, size_t bin_count, size_t bin_number, double& range_border);
|
||||||
|
void find_max_capacity(const std::vector<size_t>& bins, size_t& max_bin_capacity);
|
||||||
@@ -29,6 +29,7 @@
|
|||||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
|
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="histogram_internal.h" />
|
<ClInclude Include="histogram_internal.h" />
|
||||||
|
<ClInclude Include="range_border.h" />
|
||||||
<ClInclude Include="svg.h" />
|
<ClInclude Include="svg.h" />
|
||||||
<ClInclude Include="text.h" />
|
<ClInclude Include="text.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
3
lab1.cpp
3
lab1.cpp
@@ -27,7 +27,8 @@ int main()
|
|||||||
const size_t SCREEN_WIDTH = 80;
|
const size_t SCREEN_WIDTH = 80;
|
||||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||||
auto in = input_data();
|
auto in = input_data();
|
||||||
|
vector<double> numbers_data = in.numbers;
|
||||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||||
show_histogram_svg(bins);
|
show_histogram_svg(bins, in.numbers, in.bin_count);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
4
range_border.h
Обычный файл
4
range_border.h
Обычный файл
@@ -0,0 +1,4 @@
|
|||||||
|
#pragma once
|
||||||
|
#include<vector>
|
||||||
|
|
||||||
|
void find_range_border(const std::vector<double>& numbers, size_t bin_count, size_t bin_number, double& range_border);
|
||||||
17
svg.cpp
17
svg.cpp
@@ -3,7 +3,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "svg.h"
|
#include "svg.h"
|
||||||
#include "text.h"
|
#include "histogram.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void svg_begin(double width, double height) {
|
void svg_begin(double width, double height) {
|
||||||
@@ -27,7 +27,7 @@ void svg_rect(double x, double y, double width, double height, string stroke = "
|
|||||||
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 << "' />";
|
||||||
}
|
}
|
||||||
|
|
||||||
void show_histogram_svg(const vector<size_t>& bins) {
|
void show_histogram_svg(const vector<size_t>& bins, const vector<double>& numbers, size_t& bin_count) {
|
||||||
const auto IMAGE_WIDTH = 400;
|
const auto IMAGE_WIDTH = 400;
|
||||||
const auto IMAGE_HEIGHT = 300;
|
const auto IMAGE_HEIGHT = 300;
|
||||||
const auto TEXT_LEFT = 20;
|
const auto TEXT_LEFT = 20;
|
||||||
@@ -39,9 +39,16 @@ void show_histogram_svg(const vector<size_t>& bins) {
|
|||||||
double top = 0;
|
double top = 0;
|
||||||
size_t max_bin_capacity{};
|
size_t max_bin_capacity{};
|
||||||
find_max_capacity(bins, max_bin_capacity);
|
find_max_capacity(bins, max_bin_capacity);
|
||||||
for (size_t bin : bins) {
|
double range_border = 0;
|
||||||
const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH) * (static_cast<double>(bin) / max_bin_capacity);
|
for (size_t i = 0; i < bins.size(); i++) {
|
||||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH) * (static_cast<double>(bins[i]) / max_bin_capacity);
|
||||||
|
if (i != 0)
|
||||||
|
{
|
||||||
|
find_range_border(numbers, bin_count, i, range_border);
|
||||||
|
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(range_border));
|
||||||
|
top += BIN_HEIGHT;
|
||||||
|
}
|
||||||
|
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bins[i]));
|
||||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "purple", "#ffcc00");
|
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "purple", "#ffcc00");
|
||||||
top += BIN_HEIGHT;
|
top += BIN_HEIGHT;
|
||||||
}
|
}
|
||||||
|
|||||||
2
svg.h
2
svg.h
@@ -1,4 +1,4 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include<vector>
|
#include<vector>
|
||||||
|
|
||||||
void show_histogram_svg(const std::vector<size_t>& bins);
|
void show_histogram_svg(const std::vector<size_t>& bins, const std::vector<double>& numbers, size_t& bin_count);
|
||||||
12
text.cpp
12
text.cpp
@@ -4,18 +4,6 @@
|
|||||||
#include "text.h"
|
#include "text.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void find_max_capacity(const vector<size_t>& bins, size_t& max_bin_capacity)
|
|
||||||
{
|
|
||||||
max_bin_capacity = bins[0];
|
|
||||||
for (size_t x : bins)
|
|
||||||
{
|
|
||||||
if (x > max_bin_capacity)
|
|
||||||
{
|
|
||||||
max_bin_capacity = x;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void show_histogram_text(const vector<size_t>& bins, size_t max_asterisk)
|
void show_histogram_text(const vector<size_t>& bins, size_t max_asterisk)
|
||||||
{
|
{
|
||||||
size_t max_bin_capacity{};
|
size_t max_bin_capacity{};
|
||||||
|
|||||||
14
unittest.cpp
14
unittest.cpp
@@ -2,6 +2,7 @@
|
|||||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||||
#include "doctest.h"
|
#include "doctest.h"
|
||||||
#include "histogram_internal.h"
|
#include "histogram_internal.h"
|
||||||
|
#include "range_border.h"
|
||||||
|
|
||||||
TEST_CASE("distinct positive numbers") {
|
TEST_CASE("distinct positive numbers") {
|
||||||
double min = 0;
|
double min = 0;
|
||||||
@@ -18,10 +19,23 @@ TEST_CASE("empty vector") {
|
|||||||
CHECK(min == 0);
|
CHECK(min == 0);
|
||||||
CHECK(max == 0);
|
CHECK(max == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("negative values") {
|
TEST_CASE("negative values") {
|
||||||
double min = 0;
|
double min = 0;
|
||||||
double max = 0;
|
double max = 0;
|
||||||
find_minmax({-2, -4}, min, max);
|
find_minmax({-2, -4}, min, max);
|
||||||
CHECK(min == -4);
|
CHECK(min == -4);
|
||||||
CHECK(max == -2);
|
CHECK(max == -2);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("negative borders") {
|
||||||
|
double range_border = 0;
|
||||||
|
find_range_border({-2, -4}, 2, 1, range_border);
|
||||||
|
CHECK(range_border == -3);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("more bins than numbers") {
|
||||||
|
double range_border = 0;
|
||||||
|
find_range_border({3}, 2, 1, range_border);
|
||||||
|
CHECK(range_border == 3);
|
||||||
}
|
}
|
||||||
@@ -133,6 +133,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="doctest.h" />
|
<ClInclude Include="doctest.h" />
|
||||||
<ClInclude Include="histogram_internal.h" />
|
<ClInclude Include="histogram_internal.h" />
|
||||||
|
<ClInclude Include="range_border.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user