code: Добавлен выбор цвета заливки
Этот коммит содержится в:
83
lab1.cpp
83
lab1.cpp
@@ -5,55 +5,102 @@
|
||||
#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 << "Введите количество значений";
|
||||
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<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[])
|
||||
{
|
||||
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<double> numbers_data = in.numbers;
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
show_histogram_svg(bins, in.numbers, in.bin_count);
|
||||
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;
|
||||
}
|
||||
|
||||
4
svg.cpp
4
svg.cpp
@@ -27,7 +27,7 @@ void svg_rect(double x, double y, double width, double height, string stroke = "
|
||||
cout << "<rect x = '" << x << "' y = '" << y << "' width = '" << width << "' height = '" << height << "' stroke = '" << stroke << "' fill = '" << fill << "' />";
|
||||
}
|
||||
|
||||
void show_histogram_svg(const vector<size_t>& bins, const vector<double>& numbers, size_t& bin_count) {
|
||||
void show_histogram_svg(const vector<size_t>& bins, const vector<double>& 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<size_t>& bins, const vector<double>& 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();
|
||||
|
||||
3
svg.h
3
svg.h
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include<vector>
|
||||
#include<string>
|
||||
|
||||
void show_histogram_svg(const std::vector<size_t>& bins, const std::vector<double>& numbers, size_t& bin_count);
|
||||
void show_histogram_svg(const std::vector<size_t>& bins, const std::vector<double>& numbers, std::size_t& bin_count, std::string& fill);
|
||||
Ссылка в новой задаче
Block a user