Сравнить коммиты
2 Коммитов
7d31711f13
...
main
| Автор | SHA1 | Дата | |
|---|---|---|---|
| 64f8e58dd4 | |||
| d07d11b436 |
81
main.cpp
81
main.cpp
@@ -1,78 +1,37 @@
|
|||||||
#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 {
|
||||||
struct Input
|
vector<double> numbers;
|
||||||
{
|
|
||||||
std::vector<double> numbers;
|
|
||||||
size_t bin_count{};
|
size_t bin_count{};
|
||||||
};
|
};
|
||||||
|
|
||||||
Input input_data(std::istream& in, bool prompt)
|
Input input_data() {
|
||||||
{
|
Input in;
|
||||||
Input data;
|
|
||||||
size_t number_count;
|
size_t number_count;
|
||||||
if(prompt)
|
|
||||||
{
|
|
||||||
cerr << "Enter the number of elements: ";
|
cerr << "Enter the number of elements: ";
|
||||||
}
|
cin >> number_count;
|
||||||
in >> number_count;
|
|
||||||
data.numbers.resize(number_count);
|
in.numbers.resize(number_count);
|
||||||
if(prompt)
|
|
||||||
{
|
|
||||||
cerr << "\nEnter " << number_count << " elements:" << endl;
|
cerr << "\nEnter " << number_count << " elements:" << endl;
|
||||||
|
for (size_t i = 0; i < number_count; i++) {
|
||||||
|
cin >> in.numbers[i];
|
||||||
}
|
}
|
||||||
for (size_t i = 0; i < number_count; i++)
|
|
||||||
{
|
|
||||||
in >> data.numbers[i];
|
|
||||||
}
|
|
||||||
if(prompt)
|
|
||||||
{
|
|
||||||
cerr << "Enter the number of bins: ";
|
cerr << "Enter the number of bins: ";
|
||||||
|
cin >> in.bin_count;
|
||||||
|
|
||||||
|
return in;
|
||||||
}
|
}
|
||||||
in >> data.bin_count;
|
|
||||||
return data;
|
int main() {
|
||||||
}
|
auto in = input_data();
|
||||||
size_t write_data(void* items, size_t item_size, size_t item_count, void* ctx){
|
auto bins = make_histogram(in.bin_count, in.numbers);
|
||||||
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);
|
show_histogram_svg(bins);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
55
svg.cpp
55
svg.cpp
@@ -34,37 +34,42 @@ svg_rect(double x, double y, double width, double height, string stroke = "black
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void
|
void show_histogram_svg(const vector<size_t>& bins) {
|
||||||
show_histogram_svg(const vector<size_t>& bins)
|
const double IMAGE_WIDTH = 400.0;
|
||||||
{
|
const double IMAGE_HEIGHT = 300.0;
|
||||||
const auto IMAGE_WIDTH = 400;
|
const double LEFT_MARGIN = 50.0;
|
||||||
const auto IMAGE_HEIGHT = 300;
|
const double LABEL_MARGIN = 30.0;
|
||||||
const auto TEXT_LEFT = 20;
|
const double BIN_HEIGHT = 30.0;
|
||||||
const auto TEXT_BASELINE = 20;
|
const double LABEL_OFFSET = 5.0;
|
||||||
const auto TEXT_WIDTH = 50;
|
|
||||||
const auto BIN_HEIGHT = 30;
|
|
||||||
const auto YELLOW = "yellow";
|
|
||||||
const auto PURPLE = "purple";
|
|
||||||
const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH;
|
|
||||||
|
|
||||||
|
const double axis = IMAGE_WIDTH - LABEL_MARGIN;
|
||||||
|
const double MAX_WIDTH = axis - LEFT_MARGIN;
|
||||||
|
|
||||||
|
double mx = 0.0;
|
||||||
|
for (size_t v : bins) {
|
||||||
|
if (static_cast<double>(v) > mx) {
|
||||||
|
mx = static_cast<double>(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
||||||
|
|
||||||
double top = 0;
|
double top = 0.0;
|
||||||
double max_count = bins[0];
|
for (size_t v : bins) {
|
||||||
for (size_t i = 0; i < bins.size(); i++)
|
double bar_w;
|
||||||
{
|
if (mx > 0) {
|
||||||
if (max_count<bins[i])
|
bar_w = MAX_WIDTH * static_cast<double>(v) / mx;
|
||||||
{
|
} else {
|
||||||
max_count=bins[i];
|
bar_w = 0.0;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t bin : bins)
|
double x0 = axis - bar_w;
|
||||||
{
|
svg_rect(x0, top, bar_w, BIN_HEIGHT, "green", "yellow");
|
||||||
double bin_width = (MAX_WIDTH)*(bin/max_count);
|
|
||||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
double label_x = x0 + bar_w + LABEL_OFFSET;
|
||||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, YELLOW, PURPLE);
|
double label_y = top + BIN_HEIGHT / 2 + 5;
|
||||||
|
svg_text(label_x, label_y, to_string(v));
|
||||||
|
|
||||||
top += BIN_HEIGHT;
|
top += BIN_HEIGHT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
72
text.cpp
72
text.cpp
@@ -1,48 +1,46 @@
|
|||||||
#include "text.h"
|
#include "text.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
void show_histogram(const std::vector<size_t>& bins) {
|
void show_histogram(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 (auto x : bins) {
|
for (size_t x : bins) {
|
||||||
if (x > 76) gigant = true;
|
if (x > mx_count) {
|
||||||
if (x > mx_count) mx_count = x;
|
mx_count = x;
|
||||||
|
|
||||||
auto len = 0;
|
|
||||||
auto num = x;
|
|
||||||
while (num > 0) {
|
|
||||||
num /= 10;
|
|
||||||
len++;
|
|
||||||
}
|
}
|
||||||
if (len > spaces) spaces = len;
|
if (x > 76) {
|
||||||
|
need_scale = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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 i = 0; i < bins.size(); i++) {
|
for (size_t count : bins) {
|
||||||
|
size_t bar_len = 0;
|
||||||
int len = 1;
|
if (need_scale) {
|
||||||
auto num = bins[i];
|
if (count == mx_count) {
|
||||||
for (; num /= 10; ++len);
|
bar_len = max_bar;
|
||||||
while (len < spaces) {
|
} else {
|
||||||
std::cout << " ";
|
bar_len = static_cast<size_t>(max_bar * static_cast<double>(count) / mx_count);
|
||||||
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 {
|
||||||
for (size_t j = 0; j < bins[i]; j++) {
|
bar_len = count;
|
||||||
std::cout << "*";
|
}
|
||||||
}
|
size_t spaces = max_bar - bar_len;
|
||||||
}
|
for (size_t i = 0; i < spaces; ++i) {
|
||||||
std::cout << std::endl;
|
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";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,15 @@
|
|||||||
<CodeBlocks_layout_file>
|
<CodeBlocks_layout_file>
|
||||||
<FileVersion major="1" minor="0" />
|
<FileVersion major="1" minor="0" />
|
||||||
<ActiveTarget name="Debug" />
|
<ActiveTarget name="Debug" />
|
||||||
|
<<<<<<< HEAD
|
||||||
|
<File name="..\histogram_internal.h" open="0" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||||
|
<Cursor>
|
||||||
|
<Cursor1 position="208" topLine="0" />
|
||||||
|
=======
|
||||||
<File name="..\svg.cpp" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
<File name="..\svg.cpp" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||||
<Cursor>
|
<Cursor>
|
||||||
<Cursor1 position="721" topLine="18" />
|
<Cursor1 position="721" topLine="18" />
|
||||||
|
>>>>>>> 7d31711f139c94dfcdd92d28b83842336d4ed5c1
|
||||||
</Cursor>
|
</Cursor>
|
||||||
</File>
|
</File>
|
||||||
<File name="..\histogram.cpp" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
<File name="..\histogram.cpp" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||||
@@ -12,6 +18,11 @@
|
|||||||
<Cursor1 position="216" topLine="0" />
|
<Cursor1 position="216" topLine="0" />
|
||||||
</Cursor>
|
</Cursor>
|
||||||
</File>
|
</File>
|
||||||
|
<<<<<<< HEAD
|
||||||
|
<File name="..\svg.cpp" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||||
|
<Cursor>
|
||||||
|
<Cursor1 position="721" topLine="18" />
|
||||||
|
=======
|
||||||
<File name="..\unittest.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
<File name="..\unittest.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||||
<Cursor>
|
<Cursor>
|
||||||
<Cursor1 position="858" topLine="4" />
|
<Cursor1 position="858" topLine="4" />
|
||||||
@@ -20,6 +31,7 @@
|
|||||||
<File name="..\histogram_internal.h" open="0" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
<File name="..\histogram_internal.h" open="0" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||||
<Cursor>
|
<Cursor>
|
||||||
<Cursor1 position="208" topLine="0" />
|
<Cursor1 position="208" topLine="0" />
|
||||||
|
>>>>>>> 7d31711f139c94dfcdd92d28b83842336d4ed5c1
|
||||||
</Cursor>
|
</Cursor>
|
||||||
</File>
|
</File>
|
||||||
<File name="..\doctest.h" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
<File name="..\doctest.h" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||||
@@ -27,4 +39,9 @@
|
|||||||
<Cursor1 position="1170" topLine="0" />
|
<Cursor1 position="1170" topLine="0" />
|
||||||
</Cursor>
|
</Cursor>
|
||||||
</File>
|
</File>
|
||||||
|
<File name="..\unittest.cpp" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||||
|
<Cursor>
|
||||||
|
<Cursor1 position="858" topLine="4" />
|
||||||
|
</Cursor>
|
||||||
|
</File>
|
||||||
</CodeBlocks_layout_file>
|
</CodeBlocks_layout_file>
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user