Сравнить коммиты
10 Коммитов
726a011b05
...
13c7d45255
Автор | SHA1 | Дата |
---|---|---|
|
13c7d45255 | 11 месяцев назад |
|
90b76949e4 | 11 месяцев назад |
|
5c046e4d7e | 11 месяцев назад |
|
1ba3aef39d | 11 месяцев назад |
|
d1974b9116 | 11 месяцев назад |
|
2bc0af5cc5 | 11 месяцев назад |
|
b9d5c4c35d | 11 месяцев назад |
|
b8a8ebdeba | 11 месяцев назад |
|
712f9245b7 | 12 месяцев назад |
|
a9dbc4eccf | 12 месяцев назад |
@ -1,34 +1,74 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <curl/curl.h>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include "histogram.h"
|
||||
#include "text.h"
|
||||
#include "svg.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Input {
|
||||
struct Input
|
||||
{
|
||||
vector<double> numbers;
|
||||
size_t bin_count{};
|
||||
};
|
||||
|
||||
Input
|
||||
input_data() {
|
||||
Input input_data(istream& in, bool prompt = true)
|
||||
{
|
||||
size_t number_count;
|
||||
if (prompt)
|
||||
cerr << "Enter number count: ";
|
||||
cin >> number_count;
|
||||
Input in;
|
||||
in.numbers.resize(number_count);
|
||||
cerr << "Enter numbers: ";
|
||||
for (size_t i = 0; i < number_count; i++) {
|
||||
cin >> in.numbers[i];
|
||||
in >> number_count;
|
||||
Input input;
|
||||
input.numbers.resize(number_count);
|
||||
for (size_t i = 0; i < number_count; i++)
|
||||
{
|
||||
in >> input.numbers[i];
|
||||
}
|
||||
if (prompt)
|
||||
cerr << "Enter bin count: ";
|
||||
cin >> in.bin_count;
|
||||
return in;
|
||||
in >> input.bin_count;
|
||||
return input;
|
||||
}
|
||||
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() {
|
||||
auto in = input_data();
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
show_histogram_text(bins);
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
bool show_prompts;
|
||||
cout << "Show prompts? (1 for yes, 0 for no): ";
|
||||
cin >> show_prompts;
|
||||
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);
|
||||
}
|
||||
|
@ -0,0 +1,51 @@
|
||||
#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();
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
#ifndef SVG_H_INCLUDED
|
||||
#define SVG_H_INCLUDED
|
||||
#include <vector>
|
||||
|
||||
void
|
||||
show_histogram_svg(const std::vector<size_t>& bins);
|
||||
|
||||
#endif // SVG_H_INCLUDED
|
Загрузка…
Ссылка в новой задаче