Сравнить коммиты

..

Ничего общего в коммитах. 'e7d98a435b2b9519290902ef62bb61fc40c98b84' и '4bfc5445de3814ff29486b7ff94e5303c584a991' имеют совершенно разные истории.

@ -1,6 +1,5 @@
#include "histogram.h" #include "histogram.h"
#include "svg.h" #include "svg.h"
using namespace std; using namespace std;
// Ïîèñê ìàêñèìóìà è ìèíèìóìà ñðåäè îöåíîê. // Ïîèñê ìàêñèìóìà è ìèíèìóìà ñðåäè îöåíîê.
@ -22,19 +21,15 @@ void find_minmax(const vector<double>& numbers, double& min, double& max)
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count)
{ {
// Ïîèñê ìàêñèìóìà è ìèíèìóìà ñðåäè îöåíîê. // Ïîèñê ìàêñèìóìà è ìèíèìóìà ñðåäè îöåíîê.
vector<size_t> bins(bin_count);
if (numbers.empty() || bin_count == 0)
return bins;
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<size_t> bins(bin_count);
for (size_t i = 0; i < bin_count; i++) for (size_t i = 0; i < bin_count; i++)
bins[i] = 0; bins[i] = 0;
@ -58,3 +53,38 @@ vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count)
} }
return bins; return bins;
} }
// Ñîçäàíèå ãèñòîãðàììû íà îñíîâàíèè êîðçèí â ôîðìàòå SVG.
void show_histogram_svg(const vector<size_t>& bins)
{
// Íåîáõîäèìûå êîíñòàíòû.
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;
size_t max_bins = bins[0];
// Ïîèñê ìàêñèìàëüíîé êîðçèíû.
for (size_t x: bins)
if (x > max_bins)
max_bins = x;
// Âûðàâíèâàíèå ãèñòîãðàììû ïî ìàêñèìàëüíîìó ñòîëáöó.
const auto BLOCK_WIDTH = (IMAGE_WIDTH - TEXT_WIDTH)/max_bins;//BLOCK_WIDTH = 10
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
double top = 0;
for (size_t bin : bins)
{
const double bin_width = BLOCK_WIDTH * bin;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
top += BIN_HEIGHT;
}
svg_end();
}

@ -8,4 +8,9 @@
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count); std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count);
// Создание гистограммы на основании корзин в формате SVG.
void show_histogram_svg(const std::vector<size_t>& bins);
#endif // HISTOGRAM_H_INCLUDED #endif // HISTOGRAM_H_INCLUDED

@ -5,7 +5,6 @@
#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;
@ -19,12 +18,12 @@ using namespace std;
// Ââîä îöåíêîê ñ êëàâèàòóðû. // Ââîä îöåíêîê ñ êëàâèàòóðû.
Input input_data(istream& in, bool prompt) Input
input_data(istream& in, bool prompt)
{ {
Input local; Input local;
size_t number_count; size_t number_count;
if (prompt)
cerr << "Enter number count: "; cerr << "Enter number count: ";
in >> number_count; in >> number_count;
local.numbers.resize(number_count); local.numbers.resize(number_count);
@ -36,20 +35,10 @@ Input input_data(istream& in, bool prompt)
return local; return local;
} }
// Ñîõðàíåíèå ôàéëà èç ñåòè â áóôåð.
size_t write_data(void* items, size_t item_size, size_t item_count, void* ctx)
{
size_t data_size = item_size * item_count;
stringstream* buffer = reinterpret_cast<stringstream*>(ctx);
(*buffer).write(reinterpret_cast<const char*>(items), data_size);
return data_size;
}
// Çàãðóçêà ôàéëà èç ñåòè â áóôåð. // Çàãðóçêà ôàéëà èç ñåòè â áóôåð.
Input download(const string& address) Input
download(const string& address)
{ {
stringstream buffer; stringstream buffer;
@ -57,9 +46,7 @@ Input download(const string& address)
if(curl) if(curl)
{ {
CURLcode res; CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, address.c_str()); curl_easy_setopt(curl, CURLOPT_URL, address);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
if (res != CURLE_OK) if (res != CURLE_OK)
{ {
@ -71,9 +58,20 @@ Input download(const string& address)
return input_data(buffer, false); return input_data(buffer, false);
} }
// Ñîõðàíåíèå ôàéëà èç ñåòè â áóôåð.
size_t write_data(void* items, size_t item_size, size_t item_count, void* ctx)
{
size_t data_size = item_size * item_count;
stringstream* buffer = reinterpret_cast<stringstream*>(ctx);
buffer->write(reinterpret_cast<const char*>(items), data_size);
return data_size;
}
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
curl_global_init(CURL_GLOBAL_ALL);
Input input; Input input;
if (argc > 1) if (argc > 1)
input = download(argv[1]); input = download(argv[1]);
@ -82,7 +80,5 @@ int main(int argc, char* argv[])
const auto bins = make_histogram(input.numbers, input.bin_count); const auto bins = make_histogram(input.numbers, input.bin_count);
show_histogram_svg(bins); show_histogram_svg(bins);
return 0;
} }

@ -35,50 +35,3 @@ void svg_rect(double x, double y, double width, double height, string stroke, st
cout << "<rect x='" << x << "' y='" << y << "' width='" << width; cout << "<rect x='" << x << "' y='" << y << "' width='" << width;
cout << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << "'/>"; cout << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << "'/>";
} }
// Ñîçäàíèå ãèñòîãðàììû íà îñíîâàíèè êîðçèí â ôîðìàòå SVG.
void show_histogram_svg(const vector<size_t>& bins)
{
// Íåîáõîäèìûå êîíñòàíòû.
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;
// const auto BLOCK_WIDTH = 10
size_t max_bins = bins[0];
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
// Ïîèñê ìàêñèìàëüíîé êîðçèíû.
for (size_t x: bins)
if (x > max_bins)
max_bins = x;
// Ïðîâåðêà ìàêñèìàëüíîãî çíà÷åíèÿ è ñîçäàíèå ãèñòîãðàììû.
if (max_bins == 0)
{
string message = "The count of numbers or columns is zero.";
svg_text(TEXT_LEFT, TEXT_BASELINE, message);
}
else
{
const auto BLOCK_WIDTH = (IMAGE_WIDTH - TEXT_WIDTH)/max_bins;
double top = 0;
for (size_t bin : bins)
{
const double bin_width = BLOCK_WIDTH * bin;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
top += BIN_HEIGHT;
}
}
svg_end();
}

@ -20,8 +20,4 @@ void svg_text(double left, double baseline, std::string text);
void svg_rect(double x, double y, double width, double height, std::string stroke = "black", std::string fill = "green");//Öâåòà çàäàíû ïî óìîë÷àíèþ. void svg_rect(double x, double y, double width, double height, std::string stroke = "black", std::string fill = "green");//Öâåòà çàäàíû ïî óìîë÷àíèþ.
// Создание гистограммы на основании корзин в формате SVG.
void show_histogram_svg(const std::vector<size_t>& bins);
#endif // SVG_H_INCLUDED #endif // SVG_H_INCLUDED

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