code: модульные тесты; улучшены функции с расчетом и печатью гистограммы для работы с пустым вектором
Этот коммит содержится в:
@@ -2,20 +2,27 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
static void
|
||||
find_minmax(const vector<double>& numbers, double& min, double& max)
|
||||
void
|
||||
find_minmax(const vector<double>& numbers, double& min, double& max, bool& empty_vector)
|
||||
{
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (double x : numbers)
|
||||
if (numbers.empty())
|
||||
{
|
||||
if (x > max )
|
||||
empty_vector = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (double x : numbers)
|
||||
{
|
||||
max = x;
|
||||
}
|
||||
if (x < min)
|
||||
{
|
||||
min = x;
|
||||
if (x > max )
|
||||
{
|
||||
max = x;
|
||||
}
|
||||
if (x < min)
|
||||
{
|
||||
min = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,25 +32,36 @@ make_histogram(const vector<double>& numbers, size_t& bin_count)
|
||||
{
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax(numbers, min, max);
|
||||
auto bin_size = (max - min)/bin_count;
|
||||
bool empty_vector = false;
|
||||
find_minmax(numbers, min, max, empty_vector);
|
||||
vector<size_t> bins(bin_count);
|
||||
for (double number : numbers)
|
||||
if (empty_vector)
|
||||
{
|
||||
bool found = false;
|
||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++)
|
||||
for (size_t y : bins)
|
||||
{
|
||||
auto lo = min + j * bin_size;
|
||||
auto hi = min + (j + 1) * bin_size;
|
||||
if ((lo <= number) && (number < hi))
|
||||
{
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
y = 0;
|
||||
}
|
||||
if (!found)
|
||||
}
|
||||
else
|
||||
{
|
||||
auto bin_size = (max - min)/bin_count;
|
||||
for (double number : numbers)
|
||||
{
|
||||
bins[bin_count - 1]++;
|
||||
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 <= number) && (number < hi))
|
||||
{
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found)
|
||||
{
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
|
||||
9
histogram_internal.h
Обычный файл
9
histogram_internal.h
Обычный файл
@@ -0,0 +1,9 @@
|
||||
#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& empty_vector);
|
||||
|
||||
#endif // HISTOGRAM_INTERNAL_H_INCLUDED
|
||||
@@ -36,7 +36,14 @@
|
||||
<Unit filename="histogram.h">
|
||||
<Option target="<{~None~}>" />
|
||||
</Unit>
|
||||
<Unit filename="histogram_internal.h">
|
||||
<Option target="<{~None~}>" />
|
||||
</Unit>
|
||||
<Unit filename="main.cpp" />
|
||||
<Unit filename="text.cpp" />
|
||||
<Unit filename="text.h">
|
||||
<Option target="<{~None~}>" />
|
||||
</Unit>
|
||||
<Extensions>
|
||||
<lib_finder disable_auto="1" />
|
||||
</Extensions>
|
||||
|
||||
7
text.cpp
7
text.cpp
@@ -4,7 +4,8 @@
|
||||
using namespace std;
|
||||
|
||||
void
|
||||
show_histogram_text(const vector<size_t>& bins, size_t& bin_count) {
|
||||
show_histogram_text(const vector<size_t>& bins, size_t& bin_count)
|
||||
{
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
size_t max_count = 0;
|
||||
@@ -15,6 +16,10 @@ show_histogram_text(const vector<size_t>& bins, size_t& bin_count) {
|
||||
max_count = bins[s];
|
||||
}
|
||||
}
|
||||
if (max_count == 0)
|
||||
{
|
||||
max_count = 1;
|
||||
}
|
||||
auto scale_factor = static_cast<double>(MAX_ASTERISK) / max_count;
|
||||
if (scale_factor > 1)
|
||||
{
|
||||
|
||||
54
unittest.cpp
Обычный файл
54
unittest.cpp
Обычный файл
@@ -0,0 +1,54 @@
|
||||
#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;
|
||||
bool empty_vector = false;
|
||||
find_minmax({1, 2}, min, max, empty_vector);
|
||||
CHECK(empty_vector == false);
|
||||
CHECK(min == 1);
|
||||
CHECK(max == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("the only number") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
bool empty_vector = false;
|
||||
find_minmax({1}, min, max, empty_vector);
|
||||
CHECK(empty_vector == false);
|
||||
CHECK(min == 1);
|
||||
CHECK(max == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("negative numbers") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
bool empty_vector = false;
|
||||
find_minmax({-1, -2}, min, max, empty_vector);
|
||||
CHECK(empty_vector == false);
|
||||
CHECK(min == -2);
|
||||
CHECK(max == -1);
|
||||
}
|
||||
|
||||
TEST_CASE("identical numbers") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
bool empty_vector = false;
|
||||
find_minmax({2, 2}, min, max, empty_vector);
|
||||
CHECK(empty_vector == false);
|
||||
CHECK(min == 2);
|
||||
CHECK(max == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("empty vector") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
bool empty_vector = false;
|
||||
find_minmax({}, min, max, empty_vector);
|
||||
CHECK(empty_vector == true);
|
||||
CHECK(min == 0);
|
||||
CHECK(max == 0);
|
||||
}
|
||||
Ссылка в новой задаче
Block a user