Сравнить коммиты
Ничего общего в коммитах. '64f8e58dd425bf1747bd37310aea0b98d18dc074' и '7d31711f139c94dfcdd92d28b83842336d4ed5c1' имеют совершенно разные истории.
64f8e58dd4
...
7d31711f13
@ -1,37 +1,78 @@
|
|||||||
|
#include <curl/curl.h>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "histogram.h"
|
#include "histogram.h"
|
||||||
#include "text.h"
|
#include "text.h"
|
||||||
#include "svg.h"
|
#include "svg.h"
|
||||||
struct Input {
|
|
||||||
vector<double> numbers;
|
struct Input
|
||||||
|
{
|
||||||
|
std::vector<double> numbers;
|
||||||
size_t bin_count{};
|
size_t bin_count{};
|
||||||
};
|
};
|
||||||
|
|
||||||
Input input_data() {
|
Input input_data(std::istream& in, bool prompt)
|
||||||
Input in;
|
{
|
||||||
|
Input data;
|
||||||
size_t number_count;
|
size_t number_count;
|
||||||
|
if(prompt)
|
||||||
cerr << "Enter the number of elements: ";
|
{
|
||||||
cin >> number_count;
|
cerr << "Enter the number of elements: ";
|
||||||
|
|
||||||
in.numbers.resize(number_count);
|
|
||||||
|
|
||||||
cerr << "\nEnter " << number_count << " elements:" << endl;
|
|
||||||
for (size_t i = 0; i < number_count; i++) {
|
|
||||||
cin >> in.numbers[i];
|
|
||||||
}
|
}
|
||||||
|
in >> number_count;
|
||||||
cerr << "Enter the number of bins: ";
|
data.numbers.resize(number_count);
|
||||||
cin >> in.bin_count;
|
if(prompt)
|
||||||
|
{
|
||||||
return in;
|
cerr << "\nEnter " << number_count << " elements:" << endl;
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < number_count; i++)
|
||||||
|
{
|
||||||
|
in >> data.numbers[i];
|
||||||
|
}
|
||||||
|
if(prompt)
|
||||||
|
{
|
||||||
|
cerr << "Enter the number of bins: ";
|
||||||
|
}
|
||||||
|
in >> data.bin_count;
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
|
size_t write_data(void* items, size_t item_size, size_t item_count, void* ctx){
|
||||||
int main() {
|
stringstream* buffer = reinterpret_cast<stringstream*>(ctx);
|
||||||
auto in = input_data();
|
size_t data_size = item_size * item_count;
|
||||||
auto bins = make_histogram(in.bin_count, in.numbers);
|
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)
|
||||||
|
{
|
||||||
|
curl_easy_setopt(curl, CURLOPT_URL, address.c_str());
|
||||||
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
||||||
|
CURLcode res = curl_easy_perform(curl);
|
||||||
|
curl_easy_cleanup(curl);
|
||||||
|
if (res != CURLE_OK)
|
||||||
|
{
|
||||||
|
cerr << "cURL error: " << curl_easy_strerror(res) << endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
return input_data(buffer, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int
|
||||||
|
main(int argc, char* argv[]) {
|
||||||
|
Input input;
|
||||||
|
if (argc > 1) {
|
||||||
|
input = download(argv[1]);
|
||||||
|
} else {
|
||||||
|
input = input_data(cin, true);
|
||||||
|
}
|
||||||
|
auto bins = make_histogram(input.bin_count, input.numbers);
|
||||||
show_histogram_svg(bins);
|
show_histogram_svg(bins);
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,46 +1,48 @@
|
|||||||
#include "text.h"
|
#include "text.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
void show_histogram(std::vector<size_t> bins) {
|
void show_histogram(const std::vector<size_t>& bins) {
|
||||||
|
|
||||||
|
|
||||||
|
bool gigant = false;
|
||||||
|
auto spaces = 0;
|
||||||
size_t mx_count = 0;
|
size_t mx_count = 0;
|
||||||
bool need_scale = false;
|
|
||||||
for (size_t x : bins) {
|
for (auto x : bins) {
|
||||||
if (x > mx_count) {
|
if (x > 76) gigant = true;
|
||||||
mx_count = x;
|
if (x > mx_count) mx_count = x;
|
||||||
}
|
|
||||||
if (x > 76) {
|
auto len = 0;
|
||||||
need_scale = true;
|
auto num = x;
|
||||||
|
while (num > 0) {
|
||||||
|
num /= 10;
|
||||||
|
len++;
|
||||||
}
|
}
|
||||||
|
if (len > spaces) spaces = len;
|
||||||
}
|
}
|
||||||
size_t max_bar = need_scale ? 76 : mx_count;
|
|
||||||
size_t digits = 1;
|
|
||||||
for (size_t t = mx_count; t >= 10; t /= 10) {
|
|
||||||
++digits;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (size_t count : bins) {
|
for (size_t i = 0; i < bins.size(); i++) {
|
||||||
size_t bar_len = 0;
|
|
||||||
if (need_scale) {
|
int len = 1;
|
||||||
if (count == mx_count) {
|
auto num = bins[i];
|
||||||
bar_len = max_bar;
|
for (; num /= 10; ++len);
|
||||||
} else {
|
while (len < spaces) {
|
||||||
bar_len = static_cast<size_t>(max_bar * static_cast<double>(count) / mx_count);
|
std::cout << " ";
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
std::cout << bins[i] << "|";
|
||||||
|
|
||||||
|
|
||||||
|
if (gigant) {
|
||||||
|
size_t stars = (mx_count > 0) ? (76 * bins[i] / mx_count) : 0;
|
||||||
|
for (size_t j = 0; j < stars; j++) {
|
||||||
|
std::cout << "*";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
bar_len = count;
|
for (size_t j = 0; j < bins[i]; j++) {
|
||||||
}
|
std::cout << "*";
|
||||||
size_t spaces = max_bar - bar_len;
|
}
|
||||||
for (size_t i = 0; i < spaces; ++i) {
|
|
||||||
std::cout << ' ';
|
|
||||||
}
|
|
||||||
for (size_t i = 0; i < bar_len; ++i) {
|
|
||||||
std::cout << '*';
|
|
||||||
}
|
|
||||||
std::cout << "| ";
|
|
||||||
std::string s = std::to_string(count);
|
|
||||||
for (size_t i = s.size(); i < digits; ++i) {
|
|
||||||
std::cout << ' ';
|
|
||||||
}
|
}
|
||||||
std::cout << s << "\n";
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Загрузка…
Ссылка в новой задаче