code: общий вариант(масшиабирование)

main
TabolinIA 11 месяцев назад
Родитель 1c660f9378
Сommit 2de0e2d5f9

@ -17,12 +17,12 @@ find_minmax(const vector<double>& numbers, double& min, double& max) {
} }
} }
vector <double> make_histogram(const vector<double>& numbers, size_t bin_count){ vector <size_t> make_histogram(const vector<double>& numbers, size_t bin_count){
double min, max; double min, max;
find_minmax(numbers, min, max); find_minmax(numbers, min, max);
double bin_size = (max - min) / bin_count; double bin_size = (max - min) / bin_count;
vector <double> bins; vector <size_t> bins;
bins.resize(bin_count); bins.resize(bin_count);
for (std::size_t i = 0; i < numbers.size(); i++) for (std::size_t i = 0; i < numbers.size(); i++)
@ -43,16 +43,6 @@ vector <double> make_histogram(const vector<double>& numbers, size_t bin_count){
bins[bin_count - 1]++; bins[bin_count - 1]++;
} }
} }
double out;
for (std::size_t i = 0; i < bin_count-1; i++)
{
if (bins[i] > bins[i+1])
{
out = bins[i];
bins[i] = bins[i + 1];
bins[i + 1] = out;
}
}
return bins; return bins;
} }

@ -1,5 +1,5 @@
#ifndef HISTOGRAM_H_INCLUDED #ifndef HISTOGRAM_H_INCLUDED
#define HISTOGRAM_H_INCLUDED #define HISTOGRAM_H_INCLUDED
std::vector<double> std::vector<size_t>
make_histogram(const std::vector<double>& numbers, const std::size_t bin_count); make_histogram(const std::vector<double>& numbers, const std::size_t bin_count);
#endif // HISTOGRAM_H_INCLUDED #endif // HISTOGRAM_H_INCLUDED

@ -35,7 +35,12 @@
<Unit filename=".gitignore" /> <Unit filename=".gitignore" />
<Unit filename="histogram.cpp" /> <Unit filename="histogram.cpp" />
<Unit filename="histogram.h" /> <Unit filename="histogram.h" />
<Unit filename="histogram_internal.h" />
<Unit filename="main.cpp" /> <Unit filename="main.cpp" />
<Unit filename="svg.cpp" />
<Unit filename="svg.h" />
<Unit filename="text.cpp" />
<Unit filename="text.h" />
<Extensions /> <Extensions />
</Project> </Project>
</CodeBlocks_project_file> </CodeBlocks_project_file>

@ -2,6 +2,7 @@
#include <vector> #include <vector>
#include "histogram.h" #include "histogram.h"
#include "text.h" #include "text.h"
#include "svg.h"
using namespace std; using namespace std;
@ -14,7 +15,7 @@ struct Input {
Input Input
input_data() { input_data() {
size_t number_count; size_t number_count;
cout << "Enter number count: "; cerr << "Enter number count: ";
cin >> number_count; cin >> number_count;
Input in; Input in;
in.numbers.resize(number_count); in.numbers.resize(number_count);
@ -33,6 +34,6 @@ main()
{ {
auto in = input_data(); auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count); auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins, in.bin_count); show_histogram_svg(bins);
return 0; return 0;
} }

@ -40,6 +40,28 @@ show_histogram_svg(const vector<size_t>& bins) {
const auto BIN_HEIGHT = 30; const auto BIN_HEIGHT = 30;
const auto BLOCK_WIDTH = 10; const auto BLOCK_WIDTH = 10;
const size_t MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH - 1;
size_t maxbin = 0;
for (size_t bin : bins){
if (bin > maxbin){
maxbin = bin;
}
}
if (maxbin > MAX_WIDTH) {
svg_begin(400, 300);
double top = 0;
for (size_t bin : bins) {
const double bin_width = MAX_WIDTH * (bin / maxbin);
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
top += BIN_HEIGHT;
}
svg_end();
}
svg_begin(400, 300); svg_begin(400, 300);
double top = 0; double top = 0;
for (size_t bin : bins) { for (size_t bin : bins) {

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