MaryK 2 лет назад
Родитель f6d71c673f
Сommit bcf36ab229

Двоичные данные
bin/Debug/devlab1.exe

Двоичный файл не отображается.

@ -1,3 +1,4 @@
10 10
3 3 3 4 4 4 4 4 5 5 3 3 3 4 4 4 4 4 5 5
3 3
12

@ -0,0 +1,6 @@
<?xml version='1.0' encoding='UTF-8'?>
<svg width='400' height='300' viewBox='0 0 400 300' xmlns='http://www.w3.org/2000/svg'>
<text x='20' y='20'>3</text><rect x='50' y='0' width='36' height='30' stroke='violet' fill='#101010' />
<text x='20' y='50'>5</text><rect x='50' y='30' width='60' height='30' stroke='violet' fill='#101010' />
<text x='20' y='80'>2</text><rect x='50' y='60' width='24' height='30' stroke='violet' fill='#101010' />
</svg>

После

Ширина:  |  Высота:  |  Размер: 448 B

@ -1,17 +1,18 @@
# depslib dependency file v1.0 # depslib dependency file v1.0
1681732107 source:c:\program files\codeblocks\devlab1\main.cpp 1681739567 source:c:\program files\codeblocks\devlab1\main.cpp
<iostream> <iostream>
<vector> <vector>
<cmath> <cmath>
"histogram.h" "histogram.h"
"svg.h" "svg.h"
1681733106 c:\program files\codeblocks\devlab1\histogram.h 1681733870 c:\program files\codeblocks\devlab1\histogram.h
<vector> <vector>
1681730387 source:c:\program files\codeblocks\devlab1\histogram.cpp 1681738471 source:c:\program files\codeblocks\devlab1\histogram.cpp
"histogram.h" "histogram.h"
<vector> <vector>
<iostream>
1680530385 c:\program files\codeblocks\devlab1\text.h 1680530385 c:\program files\codeblocks\devlab1\text.h
<vector> <vector>
@ -20,10 +21,10 @@
1680530410 source:c:\program files\codeblocks\devlab1\text.cpp 1680530410 source:c:\program files\codeblocks\devlab1\text.cpp
"text.h" "text.h"
1681733452 c:\program files\codeblocks\devlab1\svg.h 1681734640 c:\program files\codeblocks\devlab1\svg.h
<vector> <vector>
1681733443 source:c:\program files\codeblocks\devlab1\svg.cpp 1681739461 source:c:\program files\codeblocks\devlab1\svg.cpp
"svg.h" "svg.h"
<iostream> <iostream>
<vector> <vector>

@ -1,5 +1,6 @@
#include "histogram.h" #include "histogram.h"
#include <vector> #include <vector>
#include <iostream>
using namespace std; using namespace std;
void void
find_minmax(const vector<double>& numbers, double& min, double& max) find_minmax(const vector<double>& numbers, double& min, double& max)
@ -19,12 +20,13 @@ make_histogram(const vector<double>& numbers,size_t bin_count)
float lo,hi,dif; float lo,hi,dif;
double min, max; double min, max;
find_minmax(numbers, min, max); find_minmax(numbers, min, max);
//std::cout<<bin_count<<endl;
vector <size_t> bins(bin_count) ; vector <size_t> bins(bin_count) ;
dif=(max - min)/bin_count; dif=(max - min)/bin_count;
for(int i=0; i < numbers.size(); i++) for(int i=0; i < numbers.size(); i++)
{ {
bool found = false; bool found = false;
for (int j=0; (j < bin_count-1)&&!found; j++) for (size_t j=0; (j < bin_count-1)&&(!found); j++)
{ {
lo= min + j*dif; lo= min + j*dif;
hi= min + (j+1)*dif; hi= min + (j+1)*dif;
@ -33,11 +35,19 @@ make_histogram(const vector<double>& numbers,size_t bin_count)
bins[j]++; bins[j]++;
found = true; found = true;
} }
// std::cout<<bins[j]<<endl;
} }
if (!found) if (!found)
{ {
bins[bin_count - 1]++; bins[bin_count - 1]++;
} }
//if (i==0) break;
} }
/*for (size_t j=0; j < bin_count; j++){
std::cout<<bins[j]<<endl;
}*/
return bins; return bins;
} }

@ -4,6 +4,5 @@
std::vector<long long unsigned int> std::vector<long long unsigned int>
make_histogram(const std::vector<double>& numbers, size_t bin_count); make_histogram(const std::vector<double>& numbers, size_t bin_count);
//void void
//show_histogram_text(std::vector<double> bins, const std::vector<double>& numbers, size_t bin_count); show_histogram_text(std::vector<double> bins, const std::vector<double>& numbers, size_t bin_count);
//

@ -8,16 +8,16 @@ using namespace std;
int inbl() int inbl()
{ {
int BLOCK_WIDTH; int BLOCK_WIDTH;
cout << "block width is"; cerr << "block width is";
cin>>BLOCK_WIDTH; cin>>BLOCK_WIDTH;
if (BLOCK_WIDTH>30) if (BLOCK_WIDTH>30)
{ {
cout<< "too big, please make it smaller"; cerr<< "too big, please make it smaller";
inbl; inbl;
} }
else if (BLOCK_WIDTH<3) else if (BLOCK_WIDTH<3)
{ {
cout<< "too small, please make it bigger"; cerr<< "too small, please make it bigger";
inbl; inbl;
} }
else return BLOCK_WIDTH; else return BLOCK_WIDTH;
@ -26,32 +26,43 @@ int inbl()
struct Input struct Input
{ {
vector<double> numbers; vector<double> numbers;
size_t bin_count; int bin_count;
int BLOCK_WIDTH; int BLOCK_WIDTH;
}; };
Input Input
input_data() input_data()
{ {
size_t number_count; size_t number_count=0;
cerr<<"amount of numbers is";
cin >> number_count; cin >> number_count;
Input in; Input in;
in.numbers.resize(number_count); in.numbers.resize(number_count);
cerr<<"let's intro numbers ";
for (size_t i = 0; i < number_count; i++) for (size_t i = 0; i < number_count; i++)
{ {
cin >> in.numbers[i]; cin >> in.numbers[i];
} }
size_t bin_count; size_t bin_count=0;
cin >> bin_count; cerr<<"amount of bins is";
cin >> in.bin_count;
int BLOCK_WIDTH = inbl(); int BLOCK_WIDTH = inbl();
return in; return in;
} }
//4199705
//0x71fdf8
//4199705
int main() int main()
{ {
auto in = input_data(); auto in = input_data();
//std::cout<<in.bin_count<<endl;
auto bins = make_histogram(in.numbers, in.bin_count); auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_svg(&bins,in.BLOCK_WIDTH); show_histogram_svg(bins,in.BLOCK_WIDTH);
return 0; return 0;

@ -0,0 +1,80 @@
#include "svg.h"
#include <iostream>
#include <vector>
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 = "black", string fill = "black")
{
cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='#" << fill << fill << fill << "' />";
}
void
show_histogram_svg(vector<long long unsigned int> & bins, int BLOCK_WIDTH)
{
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;
svg_begin(400, 300);
double top = 0;
const size_t MAX_ASTERISK = IMAGE_WIDTH - TEXT_WIDTH;
auto maxb = bins[0];
for (size_t j = 1; j < bins.size(); j++)
{
if (bins[j] > maxb) maxb = bins[j];
}
auto maxb1 = maxb * BLOCK_WIDTH;
for (size_t bin : bins)
{
double bin_width;
int color = (10 - (bin * 9) / maxb);
if (maxb1 > MAX_ASTERISK)
{
bin_width = BLOCK_WIDTH * MAX_ASTERISK * (bin / maxb1);
}
else bin_width = BLOCK_WIDTH * bin;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "violet", to_string(color));
top += BIN_HEIGHT;
}
svg_end();
}

Двоичные данные
obj/Debug/histogram.o

Двоичный файл не отображается.

Двоичные данные
obj/Debug/main.o

Двоичный файл не отображается.

Двоичные данные
obj/Debug/svg.o

Двоичный файл не отображается.

@ -29,11 +29,11 @@ svg_text(double left, double baseline, string text)
void svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black") void svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black")
{ {
cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='#" << fill << fill << fill << "' />"; cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='#" << fill << fill << fill << "' />"<<endl;
} }
void void
show_histogram_svg( vector<double>& bins, int BLOCK_WIDTH) show_histogram_svg(vector<long long unsigned int> & bins, int BLOCK_WIDTH)
{ {
const auto IMAGE_WIDTH = 400; const auto IMAGE_WIDTH = 400;
@ -44,7 +44,7 @@ show_histogram_svg( vector<double>& bins, int BLOCK_WIDTH)
const auto BIN_HEIGHT = 30; const auto BIN_HEIGHT = 30;
BLOCK_WIDTH=BLOCK_WIDTH+12;
svg_begin(400, 300); svg_begin(400, 300);
double top = 0; double top = 0;
@ -58,12 +58,14 @@ show_histogram_svg( vector<double>& bins, int BLOCK_WIDTH)
auto maxb1 = maxb * BLOCK_WIDTH; auto maxb1 = maxb * BLOCK_WIDTH;
int i = 0;
for (size_t bin : bins) for (size_t bin : bins)
{ {
double bin_width; double bin_width;
int color = (10 - (bin * 9) / maxb); int color = 10;
if (maxb1 > MAX_ASTERISK) if (maxb1 > MAX_ASTERISK)
{ {
@ -74,7 +76,12 @@ show_histogram_svg( vector<double>& bins, int BLOCK_WIDTH)
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "violet", to_string(color)); svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "violet", to_string(color));
top += BIN_HEIGHT; top += BIN_HEIGHT;
if (i == 4)
break;
else
i++;
} }
svg_end(); svg_end();
} }

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