code: вывод гистограммы в marks.svg
Этот коммит содержится в:
@@ -35,7 +35,14 @@
|
|||||||
<Unit filename=".gitignore">
|
<Unit filename=".gitignore">
|
||||||
<Option target="<{~None~}>" />
|
<Option target="<{~None~}>" />
|
||||||
</Unit>
|
</Unit>
|
||||||
|
<Unit filename="histogram-internal.h" />
|
||||||
|
<Unit filename="histogram.cpp" />
|
||||||
|
<Unit filename="histogram.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>
|
||||||
<lib_finder disable_auto="1" />
|
<lib_finder disable_auto="1" />
|
||||||
</Extensions>
|
</Extensions>
|
||||||
|
|||||||
82
main.cpp
82
main.cpp
@@ -1,6 +1,9 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "histogram.h"
|
||||||
|
#include "text.h"
|
||||||
|
#include "svg.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
struct Input{
|
struct Input{
|
||||||
@@ -29,87 +32,10 @@ input_data()
|
|||||||
return in;
|
return in;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
find_minmax(const vector<double>& numbers, double &min, double &max)
|
|
||||||
{
|
|
||||||
min = numbers[0];
|
|
||||||
max = numbers[0];
|
|
||||||
for (size_t i = 0; i < numbers.size(); i++)
|
|
||||||
{
|
|
||||||
if (numbers[i] > max)
|
|
||||||
max = numbers[i];
|
|
||||||
if (numbers[i] < min)
|
|
||||||
min = numbers[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
vector <size_t> make_histogram (const vector<double>& numbers,size_t &bin_count)
|
|
||||||
{
|
|
||||||
vector <size_t> bins(bin_count);
|
|
||||||
double low, hi;
|
|
||||||
double min, max;
|
|
||||||
find_minmax(numbers, min, max);
|
|
||||||
|
|
||||||
double bin_size = (max - min) / bin_count;
|
|
||||||
low = min;
|
|
||||||
hi = low + bin_size;
|
|
||||||
for (size_t i = 0; i < numbers.size(); i++)
|
|
||||||
{
|
|
||||||
bool found = false;
|
|
||||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++)
|
|
||||||
{
|
|
||||||
low = min + j * bin_size;
|
|
||||||
hi = min + (j + 1) * bin_size;
|
|
||||||
if ((low <= numbers[i]) && (numbers[i] < hi))
|
|
||||||
{
|
|
||||||
bins[j]++;
|
|
||||||
found = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (found == false)
|
|
||||||
{
|
|
||||||
bins[bin_count - 1]++;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
return bins;
|
|
||||||
}
|
|
||||||
void show_histogram_text(const vector<size_t>& bins,size_t &bin_count)
|
|
||||||
{
|
|
||||||
const size_t SCREEN_WIDTH = 80;
|
|
||||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
||||||
|
|
||||||
size_t max_bin = bins[0];
|
|
||||||
for (size_t bin : bins)
|
|
||||||
if (bin > max_bin)
|
|
||||||
max_bin = bin;
|
|
||||||
for (size_t bin : bins)
|
|
||||||
{
|
|
||||||
int height = bin;
|
|
||||||
if (max_bin > MAX_ASTERISK)
|
|
||||||
{
|
|
||||||
height = MAX_ASTERISK * (static_cast<double>(bin) / max_bin);
|
|
||||||
}
|
|
||||||
if (bin < 100)
|
|
||||||
cout << " ";
|
|
||||||
if (bin < 10)
|
|
||||||
cout << " ";
|
|
||||||
cout << bin << "|";
|
|
||||||
|
|
||||||
for (size_t i = 0; i < height; i++)
|
|
||||||
cout << "*";
|
|
||||||
cout << endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
Input in = input_data();
|
Input in = input_data();
|
||||||
//double bin_count{};
|
|
||||||
auto bins=make_histogram(in.numbers,in.bin_count);
|
auto bins=make_histogram(in.numbers,in.bin_count);
|
||||||
//for (int i=0; i<in.bin_count; i++)
|
show_histogram_svg(bins); //,in.bin_count);
|
||||||
//cout << bins[i];
|
|
||||||
show_histogram_text(bins,in.bin_count);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user