Родитель
079f9fd1b5
Сommit
998ed9798f
@ -0,0 +1,68 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <math.h>
|
||||
#include "histogram.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool find_minmax(const vector<double>& A, double& min, double& max)
|
||||
{
|
||||
bool empt;
|
||||
|
||||
if (A.size() != 0)
|
||||
{
|
||||
|
||||
min = A[0];
|
||||
for (auto i = 0; i < A.size(); i++)
|
||||
{
|
||||
if (A[i] < min)
|
||||
{
|
||||
min = A[i];
|
||||
}
|
||||
}
|
||||
|
||||
max = A[0];
|
||||
for (auto i = 0; i < A.size(); i++)
|
||||
{
|
||||
if (A[i] > max)
|
||||
{
|
||||
max = A[i];
|
||||
}
|
||||
}
|
||||
empt =true;
|
||||
}
|
||||
else
|
||||
empt = false;
|
||||
return empt;
|
||||
}
|
||||
|
||||
|
||||
vector <size_t> make_histogram(const vector<double>& A, size_t bin)
|
||||
{
|
||||
vector<size_t>B(bin);
|
||||
size_t max_count;
|
||||
double max, min;
|
||||
bool empt;
|
||||
find_minmax(A, min, max);
|
||||
double step = (max - min) / (bin);
|
||||
|
||||
for (size_t i = 0; i < A.size(); i++)
|
||||
{
|
||||
for (size_t j = 0; j < bin; j++)
|
||||
{
|
||||
if ((A[i] >= (min + j * step)) && (A[i] < (min + (j + 1)*step)))
|
||||
{
|
||||
B[j]++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < A.size(); i++)
|
||||
{
|
||||
if (A[i] == max)
|
||||
B[bin - 1]++;
|
||||
}
|
||||
return B;
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
#ifndef HISTOGRAM_H_INCLUDED
|
||||
#define HISTOGRAM_H_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
|
||||
std::vector<size_t>
|
||||
make_histogram(const std::vector<double>& A, size_t bin);
|
||||
|
||||
#endif // HISTOGRAM_H_INCLUDED
|
@ -0,0 +1,7 @@
|
||||
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
|
||||
#define HISTOGRAM_INTERNAL_H_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
|
||||
bool find_minmax(const std::vector<double>& A, double& min, double& max);
|
||||
#endif // HISTOGRAM_INTERNAL_H_INCLUDED
|
@ -0,0 +1,64 @@
|
||||
#include <math.h>
|
||||
#include <iostream>
|
||||
#include <conio.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "svg.h"
|
||||
using namespace std;
|
||||
|
||||
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>";
|
||||
}
|
||||
|
||||
void
|
||||
svg_rect(double x, double y, double width, double height, string opacity , string stroke, string fill) {
|
||||
cout << "<rect x='"<<x<<"' y='"<<y<<"' width='"<<width<<"' height='"<<height<<"' fill-opacity= '"<< opacity<< "' stroke='"<<stroke<<"' fill='"<<fill<<"' />";
|
||||
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
show_histogram_svg(const vector<size_t>& B) {
|
||||
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 = 10;
|
||||
const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH;
|
||||
|
||||
|
||||
svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT);
|
||||
|
||||
double top = 0;
|
||||
double max_count = B[0];
|
||||
for (size_t i = 0; i < B.size(); i++) {
|
||||
if (B[i] > max_count)
|
||||
max_count = B[i];
|
||||
}
|
||||
for (size_t bin : B) {
|
||||
double opacity = (bin / max_count);
|
||||
const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH)*(bin/max_count);
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, to_string(opacity), "blue", "#9BB0D2");
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
|
||||
svg_end();
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
#ifndef SVG_H_INCLUDED
|
||||
#define SVG_H_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
void
|
||||
show_histogram_svg(const std::vector<size_t>& B);
|
||||
|
||||
#endif // SVG_H_INCLUDED
|
@ -0,0 +1,29 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <math.h>
|
||||
#include "text.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
void show_histogram(vector<size_t>B, size_t bin)
|
||||
{
|
||||
for (size_t i = 0; i < bin; i++)
|
||||
{
|
||||
if (B[i] < 10)
|
||||
{
|
||||
cout << " ";
|
||||
}
|
||||
else if (B[i] < 100)
|
||||
{
|
||||
cout << " ";
|
||||
}
|
||||
cout << B[i] << "|";
|
||||
for (size_t j = 0; j < B[i]; j++)
|
||||
{
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
void show_histogram(std::vector<size_t>B, size_t bin);
|
||||
|
@ -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,56 @@
|
||||
#define DOCTEST_CONFIG_NO_MULTITHREADING
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
#include "doctest.h"
|
||||
#include "histogram_internal.h"
|
||||
|
||||
TEST_CASE("emptyA") {
|
||||
std::vector<double>A{1,2,3,4};
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax(A, min, max);
|
||||
CHECK(A.size() !=0 );
|
||||
}
|
||||
|
||||
TEST_CASE("min")
|
||||
{
|
||||
std::vector<double>A{1,2,3,4};
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax(A, min, max);
|
||||
CHECK(min == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("max")
|
||||
{
|
||||
std::vector<double>A{1,2,3,4};
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax(A, min, max);
|
||||
CHECK(max == 4);
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("1A") {
|
||||
std::vector<double>A{1,2,3,4};
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax(A, min, max);
|
||||
CHECK(A.size() !=1 );
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("same"){
|
||||
std::vector<double>A{1,1,1,1};
|
||||
std::vector<double>B{1,1,1,1};
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax(A, min, max);
|
||||
CHECK(A == B);
|
||||
}
|
||||
|
||||
TEST_CASE("emt"){
|
||||
std::vector<double>A{};
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
CHECK(find_minmax(A,min,max)==false);
|
||||
}
|
Загрузка…
Ссылка в новой задаче