Сравнить коммиты
3 Коммитов
3c5eb2a58c
...
b7fd7da9cb
Автор | SHA1 | Дата |
---|---|---|
![]() |
b7fd7da9cb | 2 лет назад |
![]() |
554cdd5d11 | 2 лет назад |
![]() |
63b5e7bb17 | 2 лет назад |
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
@ -0,0 +1,56 @@
|
||||
#include "histogram.h"
|
||||
#include "histogram_internal.h"
|
||||
|
||||
void
|
||||
find_minmax (const std::vector<double>& numbers, double& min, double& max) {
|
||||
if (numbers.size() == 0) { //empty array situation check
|
||||
return;
|
||||
} else {
|
||||
min = numbers[0];
|
||||
for (double element : numbers) {
|
||||
if (element < min) {
|
||||
min = element;
|
||||
}
|
||||
}
|
||||
|
||||
max = numbers[0];
|
||||
for (double element : numbers) {
|
||||
if (element > max) {
|
||||
max = element;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::vector<std::size_t>
|
||||
make_histogram (const std::vector<double>& numbers, std::size_t bin_count) {
|
||||
double max;
|
||||
double min;
|
||||
|
||||
find_minmax(numbers, min, max);
|
||||
|
||||
double bin_size = static_cast<double>(max - min) / (bin_count);
|
||||
|
||||
std::vector <std::size_t> bins(bin_count);
|
||||
|
||||
|
||||
for (std::size_t i = 0; i < numbers.size(); ++i) { //Checking if a number is in a bin.
|
||||
bool found = false;
|
||||
for (std::size_t j = 0; (j < bin_count - 1) && !found; ++j) {
|
||||
if ( (numbers[i] >= (min + j * bin_size)) && //Where (min + j * bin_size) is equal to the lower border
|
||||
(numbers[i] < (min + (j + 1) * bin_size)) ) { //and (min + (j + 1) * bin_size) is equal to the higher border.
|
||||
++bins[j];
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
++bins[bin_count - 1]; //A special case when current number is equal to the maximum.
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
||||
|
||||
|
||||
|
@ -0,0 +1,9 @@
|
||||
#ifndef HISTOGRAM_H_INCLUDED
|
||||
#define HISTOGRAM_H_INCLUDED
|
||||
|
||||
#endif // HISTOGRAM_H_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
|
||||
std::vector<std::size_t>
|
||||
make_histogram(const std::vector<double>& numbers, std::size_t bin_count);
|
@ -0,0 +1,4 @@
|
||||
#include <vector>
|
||||
|
||||
void
|
||||
find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
После Ширина: | Высота: | Размер: 933 B |
@ -0,0 +1,6 @@
|
||||
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
|
||||
6
|
@ -0,0 +1,100 @@
|
||||
#include <iostream>
|
||||
#include "svg.h"
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
static void
|
||||
find_max (const std::vector<std::size_t>& numbers, std::size_t& max) {
|
||||
max = numbers[0];
|
||||
for (double element : numbers) {
|
||||
if (element > max) {
|
||||
max = element;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 << "' />";
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
show_histogram_svg (const std::vector<size_t>& bins, std::size_t& interval_task) {
|
||||
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;
|
||||
//const auto BLOCK_WIDTH = 50;
|
||||
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
|
||||
//const auto SPACE_BETWEEN_INTERVALS = 10;
|
||||
const auto INTERVAL_SCALED = interval_task * 10;
|
||||
const auto SCALE_WIDTH = 8;
|
||||
//const auto HALF_SPACE_BETWEEN_INTERVALS = static_cast<double>(SPACE_BETWEEN_INTERVALS) / (2);
|
||||
const auto HALF_DEFAULT_FONT = 4;
|
||||
const auto MINIMAL_FONT_SPACE = 4 * 4;
|
||||
|
||||
std::size_t max_bin;
|
||||
find_max(bins, max_bin);
|
||||
double modifier;
|
||||
|
||||
modifier = static_cast<double>(MAX_WIDTH) / (max_bin);
|
||||
|
||||
size_t times;
|
||||
|
||||
for (times = 0; (times * INTERVAL_SCALED) < (MAX_WIDTH - MINIMAL_FONT_SPACE); ++times);
|
||||
|
||||
double interval_space = static_cast<double>(MAX_WIDTH - INTERVAL_SCALED * (times - 1)) / (times - 2);
|
||||
double half_interval_space = interval_space / 2;
|
||||
|
||||
svg_begin(410, 300);
|
||||
|
||||
double top = 0;
|
||||
for (size_t bin : bins) {
|
||||
const double bin_width = modifier * bin;
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin));
|
||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "#483D8B", "#9370DB");
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
|
||||
top += 10;
|
||||
|
||||
times -= 1;
|
||||
for (size_t i = 0; i < times; ++i) {
|
||||
svg_rect(TEXT_WIDTH + i * (INTERVAL_SCALED + interval_space), top, INTERVAL_SCALED, SCALE_WIDTH, "chocolate", "tan");
|
||||
}
|
||||
|
||||
top += 10;
|
||||
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(0));
|
||||
svg_text(TEXT_WIDTH + INTERVAL_SCALED + half_interval_space - HALF_DEFAULT_FONT, top + TEXT_BASELINE, std::to_string(interval_task));
|
||||
svg_text(TEXT_WIDTH + (INTERVAL_SCALED * times) + interval_space * (times - 1) - (HALF_DEFAULT_FONT * 2), top + TEXT_BASELINE, std::to_string(interval_task * times));
|
||||
|
||||
svg_end();
|
||||
}
|
||||
|
||||
|
||||
|
@ -0,0 +1,4 @@
|
||||
#include <vector>
|
||||
|
||||
void
|
||||
show_histogram_svg(const std::vector<std::size_t>& bins, std::size_t& interval_task);
|
@ -0,0 +1,55 @@
|
||||
#include <iostream>
|
||||
#include "text.h"
|
||||
|
||||
const size_t screen_width = 80;
|
||||
const size_t max_asterisk = screen_width - 3 - 1;
|
||||
|
||||
static void
|
||||
find_max (const std::vector<std::size_t>& numbers, std::size_t& max) {
|
||||
max = numbers[0];
|
||||
for (double element : numbers) {
|
||||
if (element > max) {
|
||||
max = element;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
show_histogram_text (const std::vector<std::size_t>& bins) {
|
||||
std::size_t max_bin;
|
||||
find_max(bins, max_bin);
|
||||
double modifier;
|
||||
|
||||
/*
|
||||
* In case when maximum bin > 76, histogram will be scaled according to
|
||||
* the formula "max_asterisk * (static_cast<double>(bins[i]) / max_bin)"
|
||||
* or "bins[i] * (static_cast<double>(max_asterisk) / max_bin)".
|
||||
* In other case, histogram won't be scaled, i.e, asterisk count depends on the current bin number.
|
||||
*/
|
||||
|
||||
if (max_bin > max_asterisk) {
|
||||
modifier = static_cast<double>(max_asterisk) / (max_bin);
|
||||
} else {
|
||||
modifier = 1;
|
||||
}
|
||||
|
||||
for (std::size_t i = 0; i < bins.size(); ++i) { //Histogram output with alignment, if necessary.
|
||||
if (bins[i] >= 10) {
|
||||
if (bins[i] >= 100) {
|
||||
std::cout << bins[i] << '|';
|
||||
} //Output a three-digit number.
|
||||
else {
|
||||
std::cout << ' ' << bins[i] << '|';
|
||||
} //Output a two-digit number with alignment.
|
||||
} else {
|
||||
std::cout << " " << bins[i] << '|';
|
||||
} //Output a single-digit number with alignment.
|
||||
|
||||
|
||||
size_t height = modifier * bins[i]; //Height stands for the number of output asterisks.
|
||||
for (size_t k = 0; k < height; ++k) {
|
||||
std::cout << '*';
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
#include <vector>
|
||||
|
||||
void
|
||||
show_histogram_text(const std::vector<std::size_t>& bins);
|
@ -0,0 +1,40 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_project_file>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="unittest" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Build>
|
||||
<Target title="Debug">
|
||||
<Option output="bin/Debug/unittest" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Debug/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-g" />
|
||||
</Compiler>
|
||||
</Target>
|
||||
<Target title="Release">
|
||||
<Option output="bin/Release/unittest" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Release/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-O2" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add option="-s" />
|
||||
</Linker>
|
||||
</Target>
|
||||
</Build>
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
</Compiler>
|
||||
<Unit filename="doctest.h" />
|
||||
<Unit filename="histogram.cpp" />
|
||||
<Unit filename="histogram_internal.h" />
|
||||
<Unit filename="unittest.cpp" />
|
||||
<Extensions />
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
@ -0,0 +1,46 @@
|
||||
#define DOCTEST_CONFIG_NO_MULTITHREADING
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
#include "doctest.h"
|
||||
#include "histogram_internal.h"
|
||||
|
||||
TEST_CASE("distinct positive numbers") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({1, 3}, min, max);
|
||||
CHECK(min == 1);
|
||||
CHECK(max == 3);
|
||||
}
|
||||
|
||||
TEST_CASE("empty vector") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({}, min, max);
|
||||
CHECK(min == 0);
|
||||
CHECK(max == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("vectors with one element") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({9}, min, max);
|
||||
CHECK(min == 9);
|
||||
CHECK(max == 9);
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("distinct negative numbers") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({-3, -5}, min, max);
|
||||
CHECK(min == -5);
|
||||
CHECK(max == -3);
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("vectors of identical elements") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({5, 5, 5, 5}, min, max);
|
||||
CHECK(min == 5);
|
||||
CHECK(max == 5);
|
||||
}
|
Загрузка…
Ссылка в новой задаче