5. добавлен вывод svg гистограммы
Этот коммит содержится в:
@@ -32,7 +32,13 @@
|
|||||||
<Add option="-Wall" />
|
<Add option="-Wall" />
|
||||||
<Add option="-fexceptions" />
|
<Add option="-fexceptions" />
|
||||||
</Compiler>
|
</Compiler>
|
||||||
|
<Unit filename="histogram.cpp" />
|
||||||
|
<Unit filename="histogram.h">
|
||||||
|
<Option target="Release" />
|
||||||
|
</Unit>
|
||||||
<Unit filename="main.cpp" />
|
<Unit filename="main.cpp" />
|
||||||
|
<Unit filename="text.cpp" />
|
||||||
|
<Unit filename="text.h" />
|
||||||
<Extensions>
|
<Extensions>
|
||||||
<lib_finder disable_auto="1" />
|
<lib_finder disable_auto="1" />
|
||||||
</Extensions>
|
</Extensions>
|
||||||
|
|||||||
100
main.cpp
100
main.cpp
@@ -1,10 +1,11 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "histogram.h"
|
||||||
|
#include "text.h"
|
||||||
|
#include "histogram_internal.h"
|
||||||
|
#include "svg.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
const size_t SCREEN_WIDTH = 80;
|
|
||||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
||||||
struct Input {
|
struct Input {
|
||||||
vector<double> numbers;
|
vector<double> numbers;
|
||||||
size_t bin_count{};
|
size_t bin_count{};
|
||||||
@@ -28,102 +29,11 @@ input_data(){
|
|||||||
return in;
|
return in;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
find_minmax(const vector<double>& numbers, double& min, double& max){
|
|
||||||
min = numbers[0];
|
|
||||||
max = numbers[0];
|
|
||||||
for (double x : numbers)
|
|
||||||
{
|
|
||||||
if (x < min)
|
|
||||||
{
|
|
||||||
min = x;
|
|
||||||
}
|
|
||||||
else if (x > max)
|
|
||||||
{
|
|
||||||
max = x;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
vector <size_t>
|
|
||||||
make_histogram(const vector<double>& numbers, double bin_count){
|
|
||||||
double min, max;
|
|
||||||
find_minmax(numbers, min, max);
|
|
||||||
double bin_size = (max - min) / bin_count; //ðàçìåð êîðçèíû
|
|
||||||
vector<size_t> binss(bin_count);
|
|
||||||
size_t number_count = numbers.size();
|
|
||||||
//çàïîëíåíèå ìàññèâà áèíñ
|
|
||||||
for (size_t i = 0; i < number_count; i++)
|
|
||||||
{
|
|
||||||
bool found = false;
|
|
||||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++)
|
|
||||||
{
|
|
||||||
auto lo = min + j * bin_size;
|
|
||||||
auto hi = min + (j + 1) * bin_size;
|
|
||||||
if ((lo <= numbers[i]) && (numbers[i] < hi))
|
|
||||||
{
|
|
||||||
binss[j]++;
|
|
||||||
found = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!found)
|
|
||||||
{
|
|
||||||
binss[bin_count - 1]++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return binss;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
show_histogram_text(const vector<size_t>& bins, double bin_count){
|
|
||||||
double max_count;
|
|
||||||
//ìàêñèìàäáíîå êîëâî ÷èñåë â êîðçèíå ñðåäè âñåõ êîðçèí
|
|
||||||
max_count = bins[0];
|
|
||||||
for (size_t i = 0; i < bin_count; i++)
|
|
||||||
{
|
|
||||||
if (bins[i] > max_count)
|
|
||||||
{
|
|
||||||
max_count = bins[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//âûâîä ñ âûðàâíèâàíèåì è ìàñøòàáèðîâàíèåì
|
|
||||||
for (size_t i = 0; i < bin_count; i++)
|
|
||||||
{
|
|
||||||
size_t height;
|
|
||||||
if (max_count <= MAX_ASTERISK)
|
|
||||||
{
|
|
||||||
height = bins[i];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bins[i] < 10){
|
|
||||||
cout << " ";
|
|
||||||
}
|
|
||||||
if (bins[i] < 100){
|
|
||||||
cout << " ";
|
|
||||||
}
|
|
||||||
cout << bins[i] << "|";
|
|
||||||
for (size_t j = 0; j < height; j++)
|
|
||||||
{
|
|
||||||
cout << "*";
|
|
||||||
}
|
|
||||||
cout << "\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main()
|
int 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, in.bin_count);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user