Сравнить коммиты
Ничего общего в коммитах. '13c7d452558bb94bdb9fe767f5ed45ee84437ef4' и '726a011b053396d2e656bf0394c45e138272aead' имеют совершенно разные истории.
13c7d45255
...
726a011b05
@ -1,74 +1,34 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <curl/curl.h>
|
|
||||||
#include <sstream>
|
|
||||||
#include <string>
|
|
||||||
#include "histogram.h"
|
#include "histogram.h"
|
||||||
#include "text.h"
|
#include "text.h"
|
||||||
#include "svg.h"
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
struct Input
|
struct Input {
|
||||||
{
|
|
||||||
vector<double> numbers;
|
vector<double> numbers;
|
||||||
size_t bin_count{};
|
size_t bin_count{};
|
||||||
};
|
};
|
||||||
|
|
||||||
Input input_data(istream& in, bool prompt = true)
|
Input
|
||||||
{
|
input_data() {
|
||||||
size_t number_count;
|
size_t number_count;
|
||||||
if (prompt)
|
|
||||||
cerr << "Enter number count: ";
|
cerr << "Enter number count: ";
|
||||||
in >> number_count;
|
cin >> number_count;
|
||||||
Input input;
|
Input in;
|
||||||
input.numbers.resize(number_count);
|
in.numbers.resize(number_count);
|
||||||
for (size_t i = 0; i < number_count; i++)
|
cerr << "Enter numbers: ";
|
||||||
{
|
for (size_t i = 0; i < number_count; i++) {
|
||||||
in >> input.numbers[i];
|
cin >> in.numbers[i];
|
||||||
}
|
}
|
||||||
if (prompt)
|
|
||||||
cerr << "Enter bin count: ";
|
cerr << "Enter bin count: ";
|
||||||
in >> input.bin_count;
|
cin >> in.bin_count;
|
||||||
return input;
|
return in;
|
||||||
}
|
|
||||||
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) {
|
|
||||||
stringstream buffer;
|
|
||||||
CURL *curl = curl_easy_init();
|
|
||||||
if(curl) {
|
|
||||||
CURLcode res;
|
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, address.c_str());
|
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
|
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
|
||||||
res = curl_easy_perform(curl);
|
|
||||||
if (res!=CURLE_OK){
|
|
||||||
cerr << curl_easy_strerror(res);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
curl_easy_cleanup(curl);
|
|
||||||
}
|
|
||||||
return input_data(buffer, false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int
|
||||||
{
|
main() {
|
||||||
bool show_prompts;
|
auto in = input_data();
|
||||||
cout << "Show prompts? (1 for yes, 0 for no): ";
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||||
cin >> show_prompts;
|
show_histogram_text(bins);
|
||||||
Input input;
|
|
||||||
if (argc > 1) {
|
|
||||||
input = download(argv[1]);
|
|
||||||
} else {
|
|
||||||
input = input_data(cin, true);
|
|
||||||
}
|
|
||||||
const auto bins = make_histogram(input.numbers, input.bin_count);
|
|
||||||
show_histogram_svg(bins);
|
|
||||||
}
|
}
|
||||||
|
@ -1,51 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
|
||||||
#include "svg.h"
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
void svg_text(double left, double baseline, string text) {
|
|
||||||
cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>";
|
|
||||||
}
|
|
||||||
|
|
||||||
void svg_rect(double x, double y, double width, double height) {
|
|
||||||
cout << "<rect x='" << x<< "' y='" << y<< "' width='" << width<< "' height='" << height<< "' />\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
void svg_begin(double width, double height) {
|
|
||||||
cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
|
|
||||||
cout << "<svg ";
|
|
||||||
cout << "width='" << width << "' ";
|
|
||||||
cout << "height='" << height << "' ";
|
|
||||||
cout << "viewBox='0 0 " << width << " " << height << "' ";
|
|
||||||
cout << "xmlns='http://www.w3.org/2000/svg'>\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
void svg_end() {
|
|
||||||
cout << "</svg>\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
void show_histogram_svg(const vector<size_t>& bins) {
|
|
||||||
const auto IMAGE_WIDTH = 400;
|
|
||||||
const auto IMAGE_HEIGHT = 300;
|
|
||||||
const auto TEXT_BASELINE = 20;
|
|
||||||
const auto BIN_HEIGHT = 30;
|
|
||||||
const auto BLOCK_WIDTH = 10;
|
|
||||||
svg_begin(400, 300);
|
|
||||||
double top = 0;
|
|
||||||
size_t max_bin = 0;
|
|
||||||
for (size_t bin : bins) {
|
|
||||||
if (bin > max_bin) {
|
|
||||||
max_bin = bin;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (size_t bin : bins) {
|
|
||||||
const double bin_width = BLOCK_WIDTH * bin;
|
|
||||||
const double text_left = BLOCK_WIDTH * max_bin;
|
|
||||||
const double rect_left = text_left - bin_width;
|
|
||||||
svg_rect(rect_left, top, bin_width, BIN_HEIGHT);
|
|
||||||
svg_text(text_left, top + TEXT_BASELINE, to_string(bin));
|
|
||||||
top += BIN_HEIGHT;
|
|
||||||
}
|
|
||||||
svg_end();
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
#ifndef SVG_H_INCLUDED
|
|
||||||
#define SVG_H_INCLUDED
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
void
|
|
||||||
show_histogram_svg(const std::vector<size_t>& bins);
|
|
||||||
|
|
||||||
#endif // SVG_H_INCLUDED
|
|
Загрузка…
Ссылка в новой задаче