From efe03e19669dd098b554d925fbf0c6f033f772d3 Mon Sep 17 00:00:00 2001 From: "Dmitriy (BerezhkovDA)" Date: Sun, 12 May 2024 11:30:19 +0300 Subject: [PATCH] =?UTF-8?q?lab4=20=D0=B8=D0=BD=D0=B4=D0=B8=D0=B2=D0=B8?= =?UTF-8?q?=D0=B4=D1=83=D0=B0=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20=D0=B2=D0=B0?= =?UTF-8?q?=D1=80=D0=B8=D0=B0=D0=BD=D1=82=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sem2_lab1/sem2_lab1.cpp | 63 ++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/sem2_lab1/sem2_lab1.cpp b/sem2_lab1/sem2_lab1.cpp index 3c8f54d..a8e7d25 100644 --- a/sem2_lab1/sem2_lab1.cpp +++ b/sem2_lab1/sem2_lab1.cpp @@ -9,13 +9,14 @@ #include #include #include +#include using namespace std; struct Input { vector numbers; size_t bin_count{}; }; -Input input_data(istream& in,bool prompt) +Input input_data(istream& in,bool prompt, int bins_cht) { Input in_data; int number_count; @@ -25,8 +26,14 @@ Input input_data(istream& in,bool prompt) for (size_t i = 0; i < number_count; i++) { in >> in_data.numbers[i]; } - if (prompt)cerr << "Enter bin count: "; - in >> in_data.bin_count; + if (bins_cht == 0) + { + if (prompt)cerr << "Enter bin count: "; + in >> in_data.bin_count; + } + else + in_data.bin_count = bins_cht; + return in_data; } @@ -37,7 +44,7 @@ static size_t write_data(void* items, size_t item_size, size_t item_count, void* buffer->write((char*)items, data_size); return data_size; } -Input download(const string& address) +Input download(const string& address, int bins_cht) { stringstream buffer; CURL* curl = curl_easy_init(); @@ -55,20 +62,56 @@ Input download(const string& address) } curl_easy_cleanup(curl); } - return input_data(buffer, false); + return input_data(buffer, false, bins_cht); +} + +bool IsAllDigits(string s) +{ + for(char c : s) if (!isdigit(c)) return false; + return true; } int main(int argc, char* argv[]) { curl_global_init(CURL_GLOBAL_ALL); + setlocale(LC_ALL, "Russian"); Input input; - if (argc > 1) { - input = download(argv[1]); - } - else { - input = input_data(cin, true); + bool get_bins = false; + int get_bins_pos = -1; + int bins_cnt = 0; + string URL = ""; + if (argc > 4) + { + cerr << "Количество параметров не должно превышать трех"; + exit(1); + } + if (argc > 1) + { + for (int i = 1; i < argc; i++) + { + if (strcmp(argv[i], "-bins")==0) + { + get_bins = true; + get_bins_pos = i; + } + else + if ((IsAllDigits(argv[i])) && (get_bins_pos == (i-1))) + bins_cnt = atoi(argv[i]); + else + URL = argv[i]; + } + if (get_bins && (bins_cnt == 0)) + { + cerr << "После ключа -bins должно следовать натуральное число"; + exit(1); + } } + if (URL == "") + input = input_data(cin, true, bins_cnt); + else + input = download(URL, bins_cnt); + auto bins = make_histogram(input.numbers, input.bin_count); show_histogram_svg(bins, input.numbers.size()); return 0;