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