Родитель
63b5e7bb17
Сommit
554cdd5d11
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
@ -0,0 +1,49 @@
|
|||||||
|
#include "histogram.h"
|
||||||
|
|
||||||
|
void
|
||||||
|
find_minmax(const std::vector<double>& numbers, double& min, double& max) {
|
||||||
|
min = numbers[0];
|
||||||
|
for(double element : numbers)
|
||||||
|
{
|
||||||
|
if(element < min) {min = element;}
|
||||||
|
}
|
||||||
|
|
||||||
|
max = numbers[0];
|
||||||
|
for(double element : numbers)
|
||||||
|
{
|
||||||
|
if(element > max) {max = element;}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
@ -0,0 +1,44 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include "text.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
|
||||||
|
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 > 76) {modifier = static_cast<double>(74) / (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,9 @@
|
|||||||
|
#ifndef HISTOGRAM_H_INCLUDED
|
||||||
|
#define HISTOGRAM_H_INCLUDED
|
||||||
|
|
||||||
|
#endif // HISTOGRAM_H_INCLUDED
|
||||||
|
|
||||||
|
#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);
|
||||||
|
}
|
Загрузка…
Ссылка в новой задаче