Сommit
2e6d5b708d
@ -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,7 @@
|
|||||||
|
#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,40 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||||
|
<CodeBlocks_project_file>
|
||||||
|
<FileVersion major="1" minor="6" />
|
||||||
|
<Project>
|
||||||
|
<Option title="lab3344" />
|
||||||
|
<Option pch_mode="2" />
|
||||||
|
<Option compiler="gcc" />
|
||||||
|
<Build>
|
||||||
|
<Target title="Debug">
|
||||||
|
<Option output="bin/Debug/lab3344" 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/lab3344" 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="main.cpp" />
|
||||||
|
<Extensions>
|
||||||
|
<lib_finder disable_auto="1" />
|
||||||
|
</Extensions>
|
||||||
|
</Project>
|
||||||
|
</CodeBlocks_project_file>
|
@ -0,0 +1,46 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <math.h>
|
||||||
|
#include "histogram.h"
|
||||||
|
#include "text.h"
|
||||||
|
#include "svg.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
struct Input
|
||||||
|
{
|
||||||
|
vector<double>numbers;
|
||||||
|
size_t kol_kor{};
|
||||||
|
size_t number_count;
|
||||||
|
};
|
||||||
|
|
||||||
|
Input
|
||||||
|
input_data()
|
||||||
|
{
|
||||||
|
Input in;
|
||||||
|
|
||||||
|
cerr<<"Marks: ";
|
||||||
|
cin>>in.number_count;
|
||||||
|
|
||||||
|
|
||||||
|
in.numbers.resize(in.number_count);
|
||||||
|
for (size_t i=0; i<in.number_count; i++)
|
||||||
|
{
|
||||||
|
cerr<<"numbers["<<i<<"]=";
|
||||||
|
cin>>in.numbers[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
cerr<<"Kol_kor: ";
|
||||||
|
cin>>in.kol_kor;
|
||||||
|
return in;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
size_t number_count;
|
||||||
|
auto in = input_data();
|
||||||
|
auto B = make_histogram(in.numbers, in.kol_kor);
|
||||||
|
show_histogram_svg(B, in.number_count);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -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
|
||||||
|
|
||||||
|
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,6 @@
|
|||||||
|
#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
|
Загрузка…
Ссылка в новой задаче