BiriukovaAlS 2 лет назад
Сommit ac9a9db006

@ -0,0 +1,57 @@
#include "histogram.h"
#include <vector>
#include "histogram_internal.h"
using namespace std;
bool find_minmax(const vector<double>& numbers, double& minp, double& maxp)
{
if (numbers.size() == 0)
return false;
minp = numbers[0];
for (auto i = 0; i<numbers.size(); i++)
{
if (numbers[i]<minp)
{
minp = numbers[i];
}
}
maxp = numbers[0];
for (auto i = 0; i<numbers.size(); i++)
{
if (numbers[i]>maxp)
{
maxp = numbers[i];
}
}
return true;
}
vector <size_t> make_histogram (const vector<double>& numbers, size_t kol_kor)
{
vector<size_t>B(kol_kor);
size_t max_count;
double maxp, minp;
find_minmax(numbers, minp, maxp);
double step = (maxp-minp)/(kol_kor);
for (size_t i=0; i<numbers.size(); i++)
{
for (size_t j=0; j<kol_kor; j++)
{
if ((numbers[i]>=(minp+j*step))&&(numbers[i]<(minp+(j+1)*step)))
{
B[j]++;
break;
}
}
}
for (size_t i=0; i<numbers.size(); i++)
{
if (numbers[i]== maxp)
B[kol_kor-1]++;
}
return B;
}

@ -0,0 +1,8 @@
#ifndef HISTOGRAM_H_INCLUDED
#define HISTOGRAM_H_INCLUDED
#include <vector>
std::vector<size_t>
make_histogram(const std::vector<double>& numbers, size_t kol_kor);
#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>& numbers, double& minp, double& maxp);
#endif // HISTOGRAM_INTERNAL_H_INCLUDED

@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="lab344" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/lab344" 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/lab344" 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" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename=".gitignore" />
<Unit filename="histogram.cpp" />
<Unit filename="histogram.h" />
<Unit filename="histogram_internal.h" />
<Unit filename="main.cpp" />
<Unit filename="svg.cpp" />
<Unit filename="svg.h" />
<Unit filename="text.cpp" />
<Unit filename="text.h" />
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>

@ -0,0 +1,86 @@
#define DOCTEST_CONFIG_NO_MULTITHREADING
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include "histogram_internal.h"
/*
TEST_CASE("distinct positive numbers")
{
double minp = 0;
double maxp = 0;
std::vector<double>numbers{1,2,3,4,5};
CHECK(numbers.size() !=0 );
CHECK(numbers.size() !=1 );
find_minmax(numbers, minp, maxp);
CHECK(minp == 1);
CHECK(maxp == 5);
CHECK(minp!=maxp);
}
TEST_CASE("distinct positive numbers")
{
double minp = 0;
double maxp = 0;
std::vector<double>numbers{3,4,4,4,4};
find_minmax(numbers, minp, maxp);
CHECK(minp == 1);
CHECK(maxp == 5);
}
TEST_CASE("emptyA")
{
std::vector<double>numbers{1,2,3,4};
double min = 0;
double max = 0;
find_minmax(numbers, min, max);
CHECK(numbers.size() !=0 );
}
TEST_CASE("min")
{
std::vector<double>numbers{1,2,3,4};
double min = 0;
double max = 0;
find_minmax(numbers, min, max);
CHECK(min == 1);
}
TEST_CASE("max")
{
std::vector<double>numbers{1,2,3,4};
double min = 0;
double max = 0;
find_minmax(numbers, min, max);
CHECK(max == 4);
}
*/
TEST_CASE("1A")
{
std::vector<double>numbers{1,2,3,4};
double min = 0;
double max = 0;
find_minmax(numbers, min, max);
CHECK(numbers.size() !=1 );
}
/*
TEST_CASE("same")
{
std::vector<double>numbers{1,1,1,1};
std::vector<double>B{1,1,1,1};
double min = 0;
double max = 0;
find_minmax(numbers, min, max);
CHECK(numbers == B);
}
TEST_CASE("emt")
{
std::vector<double>numbers{};
double minp = 0;
double maxp = 0;
CHECK(find_minmax(numbers,minp,maxp)==false);
}
*/

@ -0,0 +1,82 @@
#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 stroke, string fill)
{
cout << "<rect x='"<<x<<"' y='"<<y<<"' width='"<<width<<"' height='"<<height<<"' stroke='"<<stroke<<"' fill='"<<fill<<"' />";
}
void
show_histogram_svg(const vector<size_t>& B, size_t& number_count)
{
auto IMAGE_WIDTH=0;
const auto BLOCK_WIDTH = 10;
cerr << "Enter IMAGE_WIDTH:";
cin >> IMAGE_WIDTH;
while (IMAGE_WIDTH<70 || IMAGE_WIDTH>800 || IMAGE_WIDTH<(number_count/3.0*BLOCK_WIDTH))
{
cout<<"Error, incorrect IMAGE_WIDTH, please, re-enter\n";
cerr << "Enter IMAGE_WIDTH:";
cin >> IMAGE_WIDTH;
}
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 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 (max_count<B[i])
{
max_count=B[i];
}
}
for (size_t bin : B)
{
double bin_width = (MAX_WIDTH)*(bin/max_count);
svg_text(TEXT_LEFT, top+TEXT_BASELINE,to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", "green");
top += BIN_HEIGHT;
}
svg_end();
}

@ -0,0 +1,7 @@
#ifndef SVG_H_INCLUDED
#define SVG_H_INCLUDED
#include <vector>
void
show_histogram_svg(const std::vector<size_t>& B, size_t &number_count);
#endif // SVG_H_INCLUDED

@ -0,0 +1,41 @@
#include "text.h"
#include <vector>
#include <iostream>
using namespace std;
void show_histogram_text(vector<size_t>B, size_t kol_kor)
{
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
int max_count, count,j, height;
for (size_t i=0; i<kol_kor; i++)
{
if (B[i]<10)
{
cout<<" ";
}
else if (B[i]<100)
{
cout<<" ";
}
cout<<B[i]<<"|";
if (max_count > MAX_ASTERISK)
{
count = B[i];
height = MAX_ASTERISK * (static_cast<double>(count) / max_count);
}
else
{
height = B[i];
}
for (j = 0; j < height; j++)
{
cout << "*";
}
cout << endl;
}
return;
}

@ -0,0 +1,7 @@
#ifndef TEXT_H_INCLUDED
#define TEXT_H_INCLUDED
#include <vector>
void show_histogram_text(std::vector<size_t>B, size_t kol_kor, size_t& IMAGE_WIDTH);
#endif // TEXT_H_INCLUDED
Загрузка…
Отмена
Сохранить