Сравнить коммиты
3 Коммитов
07473c60bf
...
main
| Автор | SHA1 | Дата | |
|---|---|---|---|
| cc72ac4e5c | |||
| 5d4f217423 | |||
| 9565d8769f |
73
main.cpp
73
main.cpp
@@ -2,6 +2,8 @@
|
||||
#include "histogram.h"
|
||||
#include "svg.h"
|
||||
#include <vector>
|
||||
#include <curl/curl.h>
|
||||
#include <sstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -11,17 +13,20 @@ struct Input
|
||||
size_t bin_count{};
|
||||
};
|
||||
|
||||
Input input_data(istream& sin, bool prompt)
|
||||
Input input_data(istream& sin, bool prompt = true)
|
||||
{
|
||||
if (prompt){
|
||||
Input in;
|
||||
size_t number_count;
|
||||
cerr << "Input number count" << endl;
|
||||
sin >> number_count;
|
||||
in.numbers.resize(number_count);
|
||||
for (size_t i = 0; i < number_count; i++)
|
||||
{
|
||||
cerr << "Input number "<< i << endl;
|
||||
sin >> in.numbers[i];
|
||||
}
|
||||
cerr << "Input bin count" << endl;
|
||||
sin >> in.bin_count;
|
||||
return in;
|
||||
}
|
||||
@@ -39,9 +44,69 @@ Input input_data(istream& sin, bool prompt)
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
Input in = input_data(cin, 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;
|
||||
}
|
||||
|
||||
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) {
|
||||
cout << curl_easy_strerror(res);
|
||||
exit(1);
|
||||
}
|
||||
curl_easy_cleanup(curl);
|
||||
}
|
||||
return input_data(buffer, false);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
Input in;
|
||||
string link, color = "";
|
||||
bool flag = false;
|
||||
if (argc == 2) {
|
||||
in = download(argv[1]);
|
||||
}
|
||||
else if (argc == 3) {
|
||||
cout << "Wrong argument, use correct input -stroke <color> or leave input empty" << endl;
|
||||
exit(2);
|
||||
}
|
||||
else if (argc > 3) {
|
||||
for (size_t i = 1; i < argc; i++) {
|
||||
string cur = argv[i];
|
||||
if (cur.find("://") != string::npos) {
|
||||
link = cur;
|
||||
}
|
||||
else if (i != argc - 1){
|
||||
string curm = argv[i+1];
|
||||
if (cur == "-stroke" && curm.find("://") == string::npos) {
|
||||
flag = true;
|
||||
color = curm;
|
||||
}
|
||||
}
|
||||
else if (cur == "-stroke" && i == (argc - 1)) {
|
||||
cout << "Wrong argument, use correct input -stroke <color> or leave input empty" << endl;
|
||||
exit(2);
|
||||
}
|
||||
}
|
||||
in = download(link);
|
||||
}
|
||||
else {
|
||||
in = input_data(cin, false);
|
||||
}
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
show_histogram_svg(bins);
|
||||
show_histogram_svg(bins, color);
|
||||
return 0;
|
||||
}
|
||||
|
||||
13
svg.cpp
13
svg.cpp
@@ -31,16 +31,18 @@ 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, string stroke = "black", string fill = "black"){
|
||||
void
|
||||
svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black"){
|
||||
cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << "' />";
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
show_histogram_svg(const vector<size_t>& bins) {
|
||||
show_histogram_svg(const vector<size_t>& bins, string stcolor) {
|
||||
double max = 0;
|
||||
int maxlen = IMAGE_WIDTH - TEXT_WIDTH;
|
||||
int bnumber = 0;
|
||||
for (auto el: bins) {
|
||||
bnumber++;
|
||||
if (max < el) {
|
||||
max = el;
|
||||
}
|
||||
@@ -49,6 +51,11 @@ show_histogram_svg(const vector<size_t>& bins) {
|
||||
|
||||
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
||||
string stroke[] = {"red", "green", "blue"};
|
||||
if (stcolor != "") {
|
||||
for (int i = 0; i < bnumber; i++){
|
||||
stroke[i] = stcolor;
|
||||
}
|
||||
}
|
||||
string fill[] = {"red", "green", "blue"};
|
||||
int i = 0;
|
||||
double top = 0;
|
||||
|
||||
Ссылка в новой задаче
Block a user