Родитель
dd5f526371
Сommit
6630be86d1
@ -0,0 +1,5 @@
|
||||
142
|
||||
1 1 1 1 1 1 1 1 1
|
||||
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
|
||||
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
|
||||
3
|
@ -1,6 +0,0 @@
|
||||
#ifndef HISTOGRAM_INTERNAL_H
|
||||
#define HISTOGRAM_INTERNAL_H
|
||||
#include <vector>
|
||||
|
||||
void find_minmax(const std::vector<double> &numbers, double &min, double &max);
|
||||
#endif
|
После Ширина: | Высота: | Размер: 535 B |
@ -1,3 +1,3 @@
|
||||
10
|
||||
3 3 4 4 4 4 4 5 5 5
|
||||
12
|
||||
1 1 1 1 2 2 2 2 2 3 3 3
|
||||
3
|
||||
|
@ -0,0 +1,3 @@
|
||||
2|** 18%
|
||||
5|***** 45%
|
||||
4|**** 36%
|
Двоичный файл не отображается.
@ -0,0 +1,51 @@
|
||||
#include "histogram_internal.h"
|
||||
|
||||
bool find_minmax(const std::vector<double> &numbers, double &min, double &max)
|
||||
{
|
||||
if(numbers.empty()){
|
||||
return false;
|
||||
}
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (double x : numbers)
|
||||
{
|
||||
if (x < min)
|
||||
{
|
||||
min = x;
|
||||
}
|
||||
else if (x > max)
|
||||
{
|
||||
max = x;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<size_t> make_histogram(const std::vector<double> &numbers, const size_t &bin_count)
|
||||
{
|
||||
double min, max;
|
||||
find_minmax(numbers, min, max);
|
||||
|
||||
std::vector<size_t> bins(bin_count);
|
||||
double bin_size = (max - min) / bin_count;
|
||||
|
||||
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 = min + j * bin_size;
|
||||
auto hi = min + (j + 1) * bin_size;
|
||||
if ((lo <= numbers[i]) && (numbers[i] <= hi))
|
||||
{
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found)
|
||||
{
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
#ifndef HISTOGRAM_INTERNAL_H
|
||||
#define HISTOGRAM_INTERNAL_H
|
||||
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
|
||||
#define HISTOGRAM_INTERNAL_H_INCLUDED
|
||||
#include <vector>
|
||||
|
||||
void find_minmax(const std::vector<double> &numbers, double &min, double &max);
|
||||
bool find_minmax(const std::vector<double> &numbers, double &min, double &max);
|
||||
double show_histogram_svg(const std::vector<size_t> &bins);
|
||||
#endif
|
@ -0,0 +1,78 @@
|
||||
#include "histogram_internal.h"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
void svg_text(double left, double baseline, std::string text);
|
||||
|
||||
void svg_begin(double width, double height)
|
||||
{
|
||||
std::cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
|
||||
std::cout << "<svg ";
|
||||
std::cout << "width='" << width << "' ";
|
||||
std::cout << "height='" << height << "' ";
|
||||
std::cout << "viewBox='0 0 " << width << " " << height << "' ";
|
||||
std::cout << "xmlns='http://www.w3.org/2000/svg'>\n";
|
||||
}
|
||||
|
||||
void svg_end()
|
||||
{
|
||||
std::cout << "</svg>\n";
|
||||
}
|
||||
|
||||
void svg_text(double left, double baseline, std::string text)
|
||||
{
|
||||
std::cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>";
|
||||
}
|
||||
|
||||
void svg_rect(double x, double y, double width, double height, std::string stroke = "black", std::string fill = "black")
|
||||
{
|
||||
std::cout << "<rect x='" << x << "' y='" << y << "' width='" << width
|
||||
<< "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << " '/>";
|
||||
}
|
||||
|
||||
double show_histogram_svg(const std::vector<size_t> &bins)
|
||||
{
|
||||
const auto IMAGE_WIDTH = 400;
|
||||
const auto IMAGE_HEIGHT = 300;
|
||||
const auto TEXT_LEFT = 20;
|
||||
const auto TEXT_BASELINE = 20;
|
||||
const auto TEXT_WIDTH = 50;
|
||||
const auto BIN_HEIGHT = 30;
|
||||
|
||||
svg_begin(400, 300);
|
||||
double top = 0;
|
||||
|
||||
size_t max_count = 0;
|
||||
for (auto bin : bins)
|
||||
{
|
||||
if (bin > max_count)
|
||||
max_count = bin;
|
||||
}
|
||||
const auto BLOCK_WIDTH = (IMAGE_WIDTH - TEXT_WIDTH * 2 - TEXT_LEFT) / max_count;
|
||||
const auto TEXT_RATIO_SHIFT = IMAGE_WIDTH - TEXT_WIDTH;
|
||||
double summ = 0, ratio_summ = 0;
|
||||
for (size_t bin : bins)
|
||||
{
|
||||
summ += bin;
|
||||
}
|
||||
|
||||
for (size_t bin : bins)
|
||||
{
|
||||
const double bin_width = BLOCK_WIDTH * bin;
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin));
|
||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "blue", "#aaffaa");
|
||||
int ratio = (bin * 100) / summ;
|
||||
ratio_summ += ratio;
|
||||
if (bin == bins.back() && ratio_summ != 100)
|
||||
{
|
||||
ratio++;
|
||||
ratio_summ++;
|
||||
}
|
||||
svg_text(TEXT_RATIO_SHIFT, top + TEXT_BASELINE, (std::to_string(ratio) + "%"));
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
// svg_text(20, 20, std::to_string(bins[0]));
|
||||
// svg_rect(50, 0, bins[0] * 10, 30);
|
||||
svg_end();
|
||||
return ratio_summ;
|
||||
}
|
Двоичный файл не отображается.
@ -1,12 +1,58 @@
|
||||
#define DOCTEST_CONFIG_NO_MULTITHREADING
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
#include "doctest.h"
|
||||
#include <vector>
|
||||
#include "histogram_internal.h"
|
||||
|
||||
TEST_CASE("distinct positive numbers") {
|
||||
TEST_CASE("distinct positive numbers")
|
||||
{
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({1, 2}, min, max);
|
||||
CHECK(min == 1);
|
||||
CHECK(max == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("only one vector in numbers")
|
||||
{
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({1}, min, max);
|
||||
CHECK(min == 1);
|
||||
CHECK(max == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("distinct negative numbers")
|
||||
{
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({-1, -100, -30, -40}, min, max);
|
||||
CHECK(min == -100);
|
||||
CHECK(max == -1);
|
||||
}
|
||||
|
||||
TEST_CASE("vector of indentical elements")
|
||||
{
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({5, 5, 5, 5}, min, max);
|
||||
CHECK(min == 5);
|
||||
CHECK(max == 5);
|
||||
}
|
||||
|
||||
TEST_CASE("vector of indentical elements")
|
||||
{
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
CHECK(find_minmax({}, min, max) == false);
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("vector of indentical elements")
|
||||
{
|
||||
auto summ = 0;
|
||||
summ = show_histogram_svg({1, 2, 4});
|
||||
CHECK(summ == 100);
|
||||
}
|
||||
|
||||
|
||||
|
@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>English</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>com.apple.xcode.dsym.unittest</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>dSYM</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
</dict>
|
||||
</plist>
|
Двоичный файл не отображается.
Загрузка…
Ссылка в новой задаче