Вы не можете выбрать более 25 тем
Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
97 строки
2.1 KiB
C++
97 строки
2.1 KiB
C++
#include <algorithm>
|
|
#include <vector>
|
|
#include <iostream>
|
|
#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{};
|
|
};
|
|
|
|
|
|
|
|
Input
|
|
input_data(istream& in, bool prompt = true){
|
|
size_t number_count;
|
|
Input iiin;
|
|
if (prompt == true) {
|
|
|
|
cerr << "Enter number_count ";
|
|
}
|
|
in >> number_count;
|
|
|
|
iiin.numbers.resize(number_count);
|
|
if (prompt == true) {
|
|
cerr << "Enter numbers ";
|
|
}
|
|
for (int i = 0; i < number_count;i++) {
|
|
in >> iiin.numbers[i];
|
|
}
|
|
if (prompt == true) {
|
|
cerr << "Enter bin count \n";
|
|
}
|
|
in >> iiin.bin_count;
|
|
return iiin;
|
|
}
|
|
|
|
size_t
|
|
write_data(void* items, size_t item_size, size_t item_count, void* ctx) {
|
|
stringstream* buffer = reinterpret_cast<stringstream*>(ctx);
|
|
size_t data_size = item_size * item_count;
|
|
buffer->write(reinterpret_cast<const char*>(items), data_size);
|
|
|
|
return data_size;
|
|
}
|
|
|
|
Input
|
|
download(const string& address, const string& option) {
|
|
stringstream buffer;
|
|
|
|
CURL* curl = curl_easy_init();
|
|
if ((option != "") && (address != "-verbose") && (option != "-verbose")) {
|
|
cout << "Invalid argument, please use -verbose" << endl;
|
|
exit(2);
|
|
}
|
|
if (curl) {
|
|
CURLcode res;
|
|
if (option == "" || option == "-verbose") {
|
|
curl_easy_setopt(curl, CURLOPT_URL, address.c_str());
|
|
}
|
|
else if (address == "-verbose") {
|
|
curl_easy_setopt(curl, CURLOPT_URL, option.c_str());
|
|
}
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
|
if (address == "-verbose" || option == "-verbose") {
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, option == "-verbose" ? option.c_str() : address.c_str());
|
|
}
|
|
res = curl_easy_perform(curl);
|
|
if (res != CURLE_OK) {
|
|
cout << curl_easy_strerror(res);
|
|
exit(1);
|
|
}
|
|
curl_easy_cleanup(curl);
|
|
}
|
|
return input_data(buffer, false);
|
|
}
|
|
|
|
int main(int argc, char* argv[]) {
|
|
Input in;
|
|
if (argc > 1) {
|
|
in = download(argv[1], argv[2]);
|
|
}
|
|
else {
|
|
in = input_data(cin, true);
|
|
}
|
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
|
show_histogram_svg(bins);
|
|
return 0;
|
|
}
|