code: корректировка

main
Daniil (FilippovDY) 12 месяцев назад
Родитель ae9c5d2d73
Сommit 24646481c5

@ -39,6 +39,7 @@
<Unit filename="text.cpp" /> <Unit filename="text.cpp" />
<Unit filename="text.h" /> <Unit filename="text.h" />
<Unit filename="unittest.cbp" /> <Unit filename="unittest.cbp" />
<Unit filename="unittest.cpp" />
<Extensions /> <Extensions />
</Project> </Project>
</CodeBlocks_project_file> </CodeBlocks_project_file>

@ -7104,4 +7104,3 @@ DOCTEST_SUPPRESS_COMMON_WARNINGS_POP
#undef NOMINMAX #undef NOMINMAX
#undef DOCTEST_UNDEF_NOMINMAX #undef DOCTEST_UNDEF_NOMINMAX
#endif // DOCTEST_UNDEF_NOMINMAX #endif // DOCTEST_UNDEF_NOMINMAX

@ -1,7 +1,12 @@
#include "histogram.h" #include "histogram.h"
#include <vector> #include <vector>
void find_minmax(const std::vector <double> &numbers, double &min, double &max) void find_minmax(const std::vector <double> &numbers, double &min, double &max, bool &res)
{ {
if (numbers.size()==0)
{
res = true;
return;
}
min = numbers[0]; min = numbers[0];
max = numbers[0]; max = numbers[0];
for (double x : numbers) for (double x : numbers)
@ -19,7 +24,8 @@ void find_minmax(const std::vector <double> &numbers, double &min, double &max)
std::vector<std::size_t> make_histogram(const std::vector<double> &numbers, std::size_t bin_count) std::vector<std::size_t> make_histogram(const std::vector<double> &numbers, std::size_t bin_count)
{ {
double min, max; double min, max;
find_minmax(numbers, min, max); bool res = false;
find_minmax(numbers, min, max, res);
double bin_size = (max - min) / bin_count; double bin_size = (max - min) / bin_count;
std::vector <std::size_t> bins (bin_count); std::vector <std::size_t> bins (bin_count);

@ -29,6 +29,7 @@ input_data()
int int
main() main()
{ {
bool res = false;
Input in = input_data(); Input in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count); auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins); show_histogram_text(bins);

@ -8,6 +8,7 @@ const std::size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
void void
show_histogram_text(const std::vector <std::size_t> &bins) show_histogram_text(const std::vector <std::size_t> &bins)
{ {
std::cout<<std::endl;
std::size_t max_count = bins[0]; std::size_t max_count = bins[0];
for (std::size_t i = 0; i < bins.size(); i++) for (std::size_t i = 0; i < bins.size(); i++)
{ {

@ -3,10 +3,65 @@
#include "doctest.h" #include "doctest.h"
#include "histogram_internal.h" #include "histogram_internal.h"
TEST_CASE("distinct positive numbers") { TEST_CASE("distinct positive numbers") {
bool res = false;
double min = 0; double min = 0;
double max = 0; double max = 0;
find_minmax({1, 2}, min, max); find_minmax({1, 2}, min, max, res);
CHECK(min == 1); CHECK(min == 1);
CHECK(max == 2); CHECK(max == 2);
CHECK(res==0);
}
TEST_CASE("negative numbers") {
bool res = false;
double min = 0;
double max = 0;
find_minmax({-1, -2}, min, max, res);
CHECK(min == -2);
CHECK(max == -1);
CHECK(res==0);
}
TEST_CASE("empty array") {
bool res = false;
double min = 0;
double max = 0;
std::vector<double> numbers;
find_minmax(numbers, min, max, res);
CHECK(min == 0);
CHECK(max == 0);
CHECK(res==1);
}
TEST_CASE("1 number") {
bool res = false;
double min = 0;
double max = 0;
find_minmax({-1}, min, max, res);
CHECK(min == -1);
CHECK(max == -1);
CHECK(res==0);
}
TEST_CASE("same number") {
bool res = false;
double min = 0;
double max = 0;
find_minmax({1,1,1,1}, min, max, res);
CHECK(min == 1);
CHECK(max == 1);
CHECK(res==0);
}
TEST_CASE("same number but zero") {
bool res = false;
double min = 0;
double max = 0;
find_minmax({0,0,0,0}, min, max, res);
CHECK(min == 0);
CHECK(max == 0);
CHECK(res==0);
} }

Загрузка…
Отмена
Сохранить