From 2a189de275e719eae48064264c02394bfd92bb27 Mon Sep 17 00:00:00 2001 From: MamakinYR Date: Mon, 20 May 2024 02:43:18 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=B2=D1=8B=D0=B1=D0=BE=D1=80=20=D1=86=D0=B2?= =?UTF-8?q?=D0=B5=D1=82=D0=B0=20=D0=B7=D0=B0=D0=BB=D0=B8=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab1.cpp | 83 ++++++++++++++++++++++++++++++++++++++++++++------------ svg.cpp | 4 +-- svg.h | 3 +- 3 files changed, 69 insertions(+), 21 deletions(-) diff --git a/lab1.cpp b/lab1.cpp index 743d50d..4975db0 100644 --- a/lab1.cpp +++ b/lab1.cpp @@ -5,55 +5,102 @@ #include "text.h" #include "svg.h" #include +#include +#include using namespace std; struct Input { vector 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 << "Введите количество значений"; + cerr << "Введите количество значений\n"; } in_stream >> number_count; in.numbers.resize(number_count); if (prompt) { - cerr << "Введите значения"; + cerr << "Введите значения\n"; } for (size_t i = 0; i < number_count; i++) { in_stream >> in.numbers[i]; } if (prompt) { - cerr << "Введите количество столбцов гистограммы"; + 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(ctx); + const char* char_items = reinterpret_cast(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[]) { - if (argc > 1) { - CURL* curl = curl_easy_init(); - if (curl) { - CURLcode res; - curl_easy_setopt(curl, CURLOPT_URL, argv[1]); - res = curl_easy_perform(curl); - if (res != 0) { - cerr << curl_easy_strerror(res); - exit(1); + 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 an rgb or hex code"; + exit(1); + } + else if (i == 3) { + cerr << "-fill must be followed by an rgb or hex code"; + exit(1); + } + else { + input = download(argv[1]); + input.fill = argv[i + 1]; + input_created = true; + } } } - curl_easy_cleanup(curl); - return 0; + } + 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; - auto in = input_data(cin, false); - vector numbers_data = in.numbers; - auto bins = make_histogram(in.numbers, in.bin_count); - show_histogram_svg(bins, in.numbers, in.bin_count); + vector 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; } diff --git a/svg.cpp b/svg.cpp index 0b2b0b8..94969b4 100644 --- a/svg.cpp +++ b/svg.cpp @@ -27,7 +27,7 @@ void svg_rect(double x, double y, double width, double height, string stroke = " cout << ""; } -void show_histogram_svg(const vector& bins, const vector& numbers, size_t& bin_count) { +void show_histogram_svg(const vector& bins, const vector& numbers, size_t& bin_count, string& fill) { const auto IMAGE_WIDTH = 400; const auto IMAGE_HEIGHT = 300; const auto TEXT_LEFT = 20; @@ -49,7 +49,7 @@ void show_histogram_svg(const vector& bins, const vector& number top += BIN_HEIGHT; } svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bins[i])); - svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "purple", "#ffcc00"); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "purple", fill); top += BIN_HEIGHT; } svg_end(); diff --git a/svg.h b/svg.h index c504824..a0284cb 100644 --- a/svg.h +++ b/svg.h @@ -1,4 +1,5 @@ #pragma once #include +#include -void show_histogram_svg(const std::vector& bins, const std::vector& numbers, size_t& bin_count); \ No newline at end of file +void show_histogram_svg(const std::vector& bins, const std::vector& numbers, std::size_t& bin_count, std::string& fill); \ No newline at end of file