Родитель
7df403d50d
Сommit
995f8ba4ae
@ -0,0 +1,52 @@
|
||||
#include "histogram.h"
|
||||
|
||||
bool
|
||||
find_minmax(const std::vector<double>& numbers, double& min, double& max){
|
||||
if (numbers.size() != 0) {
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
|
||||
for (double x : numbers) {
|
||||
if (x < min) {
|
||||
min = x;
|
||||
}
|
||||
else if (x > max) {
|
||||
max = x;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
std::vector<size_t>
|
||||
make_histogram(const std::vector<double>& numbers, size_t bin_count){
|
||||
|
||||
std::vector<size_t> bins(bin_count);
|
||||
|
||||
double min, max;
|
||||
find_minmax(numbers, min, max);
|
||||
|
||||
double bin_size = (max - min) / bin_count;
|
||||
|
||||
for (size_t i = 0; i < numbers.size(); i++) {
|
||||
bool found = false; // òåêóùåå ñîñòîÿíèå òåêóùåãî ýëåìåíòà. Ýëåìåíò íå ðàñïðåäåëåí â êîðçèíó
|
||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++) { // áåð¸ì j-þ êîðçèíó
|
||||
auto lo = min + j * bin_size; // îïðåäåëÿåì ãðàíèöû äàííîé êîðçèíû
|
||||
auto hi = min + (j + 1) * bin_size;
|
||||
if ((lo <= numbers[i]) && (numbers[i] < hi)) { // ïîäñòàâëÿåì ïîä ýòè ãðàíèöû i-é ýëåìåíò èç numbers
|
||||
bins[j]++; // åñëè ýëåìåíò íàõîäèòñÿ â äàííîì äèàïàçîíå (ãðàíèöàõ), òî óâåëè÷èâàåì ñ÷åò÷èê j-îé êîðçèíû
|
||||
found = true; // ïåðåêëþ÷àåì "ôëàæîê", ÷òîáû ïðåêðàòèòü ïåðåáîð êîðçèí è ïåðåéòè ê ñäåä. ýë. èç numbers
|
||||
} // òî åñòü òåïåðü ýëåìåíò ðàñïðåäåëåí â êîðçèíó è íåò íóæäû ïåðåáèðàòü îñòàëüíûå êîðçèíû
|
||||
}
|
||||
if (!found) { // à åñëè æå âñ¸ òàêè ýë. íå ðàñïðåäåëåí, òî ýòî çíà÷èò, ÷òî îí ïîïàäàåò â ïîñëåäíèé äèàïàçîí
|
||||
bins[bin_count - 1]++; // äîáàâëÿåì ýòîò ýë. â ïîñëåäíþþ êîðçèíó
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
#ifndef HISTOGRAM_H_INCLUDED
|
||||
#define HISTOGRAM_H_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
|
||||
std::vector<size_t>
|
||||
make_histogram(const std::vector<double>& numbers, size_t bin_count);
|
||||
|
||||
#endif // HISTOGRAM_H_INCLUDED
|
@ -0,0 +1,10 @@
|
||||
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
|
||||
#define HISTOGRAM_INTERNAL_H_INCLUDED
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
bool find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
||||
|
||||
std::string
|
||||
bin_color(size_t bin, size_t max_count);
|
||||
#endif // HISTOGRAM_INTERNAL_H_INCLUDED
|
@ -0,0 +1,45 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "histogram.h"
|
||||
#include "svg.h"
|
||||
#include <curl/curl.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Input {
|
||||
vector<double> numbers;
|
||||
size_t bin_count{};
|
||||
};
|
||||
|
||||
Input
|
||||
input_data(istream& in, bool prompt){
|
||||
|
||||
if (prompt == true) cerr << "Enter number_count: ";
|
||||
|
||||
size_t number_count;
|
||||
in >> number_count;
|
||||
|
||||
Input data;
|
||||
data.numbers.resize(number_count);
|
||||
|
||||
if (prompt == true) cerr << "Enter numbers: ";
|
||||
|
||||
for (size_t i = 0; i < number_count; i++){
|
||||
in >> data.numbers[i];
|
||||
}
|
||||
|
||||
if (prompt == true) cerr << "Enter bin count: ";
|
||||
|
||||
in >> data.bin_count;
|
||||
return data;
|
||||
}
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
auto in = input_data(cin, false);
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
show_histogram_svg(bins);
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_project_file>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="my_project" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Build>
|
||||
<Target title="Debug">
|
||||
<Option output="bin/Debug/my_project" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Debug/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-g" />
|
||||
<Add directory="curl/include" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add library="curl/lib/libcurl.dill.a" />
|
||||
</Linker>
|
||||
</Target>
|
||||
<Target title="Release">
|
||||
<Option output="bin/Release/my_project" 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" />
|
||||
<Add directory="curl/include" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add library="curl/lib/libcurl.dll.a" />
|
||||
</Linker>
|
||||
<Unit filename="histogram.cpp" />
|
||||
<Unit filename="histogram.h" />
|
||||
<Unit filename="histogram_internal.h" />
|
||||
<Unit filename="main.cpp" />
|
||||
<Unit filename="mainmain.c">
|
||||
<Option compilerVar="CC" />
|
||||
</Unit>
|
||||
<Unit filename="svg.cpp" />
|
||||
<Unit filename="svg.h">
|
||||
<Option target="<{~None~}>" />
|
||||
</Unit>
|
||||
<Unit filename="text.cpp" />
|
||||
<Unit filename="text.h" />
|
||||
<Extensions />
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
Загрузка…
Ссылка в новой задаче