Сравнить коммиты
14 Коммитов
cf465317f2
...
538508ef98
Автор | SHA1 | Дата |
---|---|---|
![]() |
538508ef98 | 2 месяцев назад |
![]() |
ec765e16b4 | 2 месяцев назад |
![]() |
54fc874282 | 2 месяцев назад |
![]() |
37124aec8f | 2 месяцев назад |
![]() |
85ba83fd45 | 2 месяцев назад |
![]() |
a5c6f91238 | 2 месяцев назад |
![]() |
1c78ed9cd8 | 2 месяцев назад |
![]() |
b8b8b22817 | 2 месяцев назад |
![]() |
4ee24ceecb | 2 месяцев назад |
![]() |
b0563042c3 | 2 месяцев назад |
![]() |
08470acf11 | 2 месяцев назад |
![]() |
29f51bb555 | 2 месяцев назад |
![]() |
934f4788fb | 2 месяцев назад |
![]() |
d056da7c77 | 2 месяцев назад |
@ -1,4 +1,9 @@
|
||||
ProgUit Lab1.vcxproj
|
||||
ProgUit Lab1.vcxproj.filters
|
||||
ProgUit Lab1.vcxproj.user
|
||||
ProgUit Lab1/ARM64
|
||||
/ARM64
|
||||
/unittest.vcxproj
|
||||
/unittest.vcxproj.filters
|
||||
/unittest.vcxproj.user
|
||||
|
||||
|
После Ширина: | Высота: | Размер: 448 B |
@ -0,0 +1,3 @@
|
||||
10
|
||||
3 3 4 4 4 4 4 5 5 5
|
||||
3
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
@ -0,0 +1,32 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include "histogram.h"
|
||||
#include "histogram_internal.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
|
||||
vector <size_t> make_histogram(const vector<double>& numbers, const size_t bin_count) {
|
||||
|
||||
vector <size_t> bins(bin_count);
|
||||
|
||||
double min_in_numbers, max_in_numbers;
|
||||
find_minmax(numbers, min_in_numbers, max_in_numbers);
|
||||
double bin_size = (max_in_numbers - min_in_numbers) / bin_count;
|
||||
|
||||
for (size_t i = 0; i < numbers.size(); i++) {
|
||||
int to_bin = int((numbers[i]) - min_in_numbers) / bin_size;
|
||||
if (to_bin >= bin_count) {
|
||||
bins[to_bin - 1]++;
|
||||
}
|
||||
else {
|
||||
bins[to_bin]++;
|
||||
}
|
||||
}
|
||||
|
||||
return bins;
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
#ifndef HISTOGRAM_H_INCLUDED
|
||||
#define HISTOGRAM_H_INCLUDED
|
||||
|
||||
|
||||
|
||||
#include <vector>
|
||||
|
||||
std::vector<size_t>
|
||||
make_histogram(const std::vector<double>& numbers, size_t bin_count);
|
||||
|
||||
|
||||
#endif // HISTOGRAM_H_INCLUDED
|
@ -0,0 +1,13 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include "histogram_internal.h"
|
||||
using namespace std;
|
||||
|
||||
|
||||
void find_minmax(const vector<double>& numbers, double& min_in_numbers, double& max_in_numbers) {
|
||||
min_in_numbers = numbers[0];
|
||||
max_in_numbers = *(max_element(begin(numbers), end(numbers)));
|
||||
min_in_numbers = *(min_element(begin(numbers), end(numbers)));
|
||||
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
#ifndef INTERNAL_H_INCLUDED
|
||||
#define INTERNAL_H_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
|
||||
void find_minmax(const std::vector<double>& numbers, double& min_in_numbers, double& max_in_numbers);
|
||||
|
||||
#endif //INTERNAL_H_INCLUDED
|
@ -0,0 +1,96 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
#include "text.h"
|
||||
#include "histogram.h"
|
||||
#include "histogram_internal.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
std::string getRandomHexColor() {
|
||||
random_device rd; // Èñòî÷íèê ñëó÷àéíûõ ÷èñåë
|
||||
mt19937 gen(rd()); // Ãåíåðàòîð Mersenne Twister
|
||||
std::uniform_int_distribution<> dis(0, 255); // Ðàâíîìåðíîå ðàñïðåäåëåíèå [0, 255]
|
||||
|
||||
int r = dis(gen); // Êðàñíûé
|
||||
int g = dis(gen); // Çåëåíûé
|
||||
int b = dis(gen); // Ñèíèé
|
||||
|
||||
// Ôîðìèðóåì HEX-ñòðîêó
|
||||
std::stringstream hexStream;
|
||||
hexStream << "#"
|
||||
<< std::hex << std::setw(2) << std::setfill('0') << r
|
||||
<< std::hex << std::setw(2) << std::setfill('0') << g
|
||||
<< std::hex << std::setw(2) << std::setfill('0') << b;
|
||||
|
||||
return hexStream.str();
|
||||
}
|
||||
|
||||
void
|
||||
svg_begin(double width, double height) {
|
||||
cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
|
||||
cout << "<svg ";
|
||||
cout << "width='" << width << "' ";
|
||||
cout << "height='" << height << "' ";
|
||||
cout << "viewBox='0 0 " << width << " " << height << "' ";
|
||||
cout << "xmlns='http://www.w3.org/2000/svg'>\n";
|
||||
}
|
||||
|
||||
void
|
||||
svg_end() {
|
||||
cout << "</svg>\n";
|
||||
}
|
||||
|
||||
void
|
||||
svg_text(double left, double baseline, string text) {
|
||||
cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>" << endl;
|
||||
}
|
||||
|
||||
void svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black") {
|
||||
cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << "' />" << endl;
|
||||
}
|
||||
|
||||
void
|
||||
show_histogram_svg(const vector<size_t> bins) {
|
||||
|
||||
auto mm = *(max_element(bins.begin(), bins.end()));
|
||||
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;
|
||||
auto BLOCK_WIDTH = 10;
|
||||
|
||||
BLOCK_WIDTH = (IMAGE_WIDTH - TEXT_WIDTH) / mm;
|
||||
|
||||
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, "grey", getRandomHexColor());
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//svg_begin(400, 300);
|
||||
//svg_text(20, 35, to_string(bins[0]));
|
||||
//svg_rect(50, 0, bins[0] * 10, 30, "purple", "red");
|
||||
svg_end();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -0,0 +1,12 @@
|
||||
#ifndef TEXT_H_INCLUDED
|
||||
#define TEXT_H_INCLUDED
|
||||
|
||||
|
||||
|
||||
#include <vector>
|
||||
|
||||
void show_histogram_text(std::vector <size_t> bins, size_t bin_count);
|
||||
void show_histogram_svg(const std::vector<size_t> bins);
|
||||
|
||||
|
||||
#endif // TEXT_H_INCLUDED
|
@ -0,0 +1,42 @@
|
||||
#define DOCTEST_CONFIG_NO_MULTITHREADING
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
#include "doctest.h"
|
||||
#include "histogram_internal.h"
|
||||
#include "histogram_internal.cpp"
|
||||
|
||||
|
||||
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("empty vector") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({}, min, max);
|
||||
CHECK(min == 1);
|
||||
CHECK(max == 2);
|
||||
}
|
||||
TEST_CASE("you fill so lonly") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({ 1 }, min, max);
|
||||
CHECK(min == 1);
|
||||
CHECK(max == 2);
|
||||
}
|
||||
TEST_CASE("negative numbers") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({ -1, -2 }, min, max);
|
||||
CHECK(min == 1);
|
||||
CHECK(max == 2);
|
||||
}
|
||||
TEST_CASE("twins") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({ 1, 1 }, min, max);
|
||||
CHECK(min == 1);
|
||||
CHECK(max == 2);
|
||||
}
|
Загрузка…
Ссылка в новой задаче