Вы не можете выбрать более 25 тем
Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
112 строки
3.1 KiB
C++
112 строки
3.1 KiB
C++
#include <iostream>
|
|
#include <conio.h>
|
|
#include <vector>
|
|
#include "histogram.h"
|
|
#include "text.h"
|
|
#include "svg.h"
|
|
#include <curl/curl.h>
|
|
#include <sstream>
|
|
#include <string>
|
|
using namespace std;
|
|
struct Input {
|
|
vector<double> numbers;
|
|
size_t bin_count{};
|
|
string fill="black";
|
|
};
|
|
|
|
Input input_data(istream& in_stream, bool prompt) {
|
|
Input in;
|
|
size_t number_count;
|
|
if (prompt) {
|
|
cerr << "Введите количество значений\n";
|
|
}
|
|
in_stream >> number_count;
|
|
in.numbers.resize(number_count);
|
|
if (prompt) {
|
|
cerr << "Введите значения\n";
|
|
}
|
|
for (size_t i = 0; i < number_count; i++) {
|
|
in_stream >> in.numbers[i];
|
|
}
|
|
if (prompt) {
|
|
cerr << "Введите количество столбцов гистограммы\n";
|
|
}
|
|
in_stream >> in.bin_count;
|
|
return in;
|
|
}
|
|
|
|
size_t write_data(void* items, size_t item_size, size_t item_count, void* ctx) {
|
|
stringstream* buffer = reinterpret_cast<stringstream*>(ctx);
|
|
const char* char_items = reinterpret_cast<const char*>(items);
|
|
size_t data_size = item_size * item_count;
|
|
buffer->write(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_WRITEFUNCTION, write_data);
|
|
curl_easy_setopt(curl, CURLOPT_URL, address.c_str());
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
|
res = curl_easy_perform(curl);
|
|
if (res != 0) {
|
|
cerr << curl_easy_strerror(res);
|
|
exit(1);
|
|
}
|
|
}
|
|
curl_easy_cleanup(curl);
|
|
|
|
return input_data(buffer, false);
|
|
}
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
Input input;
|
|
bool input_created = false;
|
|
if (argc == 2) {
|
|
input = download(argv[1]);
|
|
input_created = true;
|
|
}
|
|
if (argc > 2) {
|
|
for (int i = 0; i < argc; i++) {
|
|
if (strcmp(argv[i], "-fill") == 0) {
|
|
if (argc < 4) {
|
|
cerr << "-fill must be followed by a color name or hex code";
|
|
exit(1);
|
|
}
|
|
else if (i == 3) {
|
|
cerr << "-fill must be followed by a color name or hex code";
|
|
exit(1);
|
|
}
|
|
else {
|
|
if (i == 2) {
|
|
input = download(argv[1]);
|
|
}
|
|
else {
|
|
input = download(argv[3]);
|
|
}
|
|
input.fill = argv[i + 1];
|
|
input_created = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
input = input_data(cin, true);
|
|
}
|
|
if (!input_created) {
|
|
cerr << "invalid arguements";
|
|
exit(1);
|
|
}
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
|
const size_t SCREEN_WIDTH = 80;
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
vector<double> numbers_data = input.numbers;
|
|
auto bins = make_histogram(input.numbers, input.bin_count);
|
|
show_histogram_svg(bins, input.numbers, input.bin_count, input.fill);
|
|
return 0;
|
|
}
|