lab34 (VasinaEY) 4 дней назад
Родитель dc9344efc1
Сommit d07d11b436

@ -13,7 +13,13 @@
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
<Add directory="curl/include" />
</Compiler>
<Linker>
<Add option="-LC:\Users\Home\Desktop\lab34\laba01\curl\lib" />
<Add library="libcurl.dll.a" />
<Add directory="C:/Users/Home/Desktop/lab34/laba01/curl/lib" />
</Linker>
</Target>
<Target title="Release">
<Option output="bin/Release/laba01" prefix_auto="1" extension_auto="1" />

@ -1,78 +1,37 @@
#include <curl/curl.h>
#include <sstream>
#include <string>
#include <iostream>
#include <vector>
#include "histogram.h"
#include "text.h"
#include "svg.h"
struct Input
{
std::vector<double> numbers;
struct Input {
vector<double> numbers;
size_t bin_count{};
};
Input input_data(std::istream& in, bool prompt)
{
Input data;
Input input_data() {
Input in;
size_t number_count;
if(prompt)
{
cerr << "Enter the number of elements: ";
}
in >> number_count;
data.numbers.resize(number_count);
if(prompt)
{
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];
}
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){
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);
}
cin >> in.bin_count;
return in;
}
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);
int main() {
auto in = input_data();
auto bins = make_histogram(in.bin_count, in.numbers);
show_histogram_svg(bins);
return 0;
}

@ -34,37 +34,42 @@ svg_rect(double x, double y, double width, double height, string stroke = "black
void
show_histogram_svg(const vector<size_t>& bins)
{
const auto IMAGE_WIDTH = 400;
const auto IMAGE_HEIGHT = 300;
const auto TEXT_LEFT = 20;
const auto TEXT_BASELINE = 20;
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;
svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT);
double top = 0;
double max_count = bins[0];
for (size_t i = 0; i < bins.size(); i++)
{
if (max_count<bins[i])
{
max_count=bins[i];
void show_histogram_svg(const vector<size_t>& bins) {
const double IMAGE_WIDTH = 400.0;
const double IMAGE_HEIGHT = 300.0;
const double LEFT_MARGIN = 50.0;
const double LABEL_MARGIN = 30.0;
const double BIN_HEIGHT = 30.0;
const double LABEL_OFFSET = 5.0;
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);
}
}
for (size_t bin : bins)
{
double bin_width = (MAX_WIDTH)*(bin/max_count);
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, YELLOW, PURPLE);
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
double top = 0.0;
for (size_t v : bins) {
double bar_w;
if (mx > 0) {
bar_w = MAX_WIDTH * static_cast<double>(v) / mx;
} else {
bar_w = 0.0;
}
double x0 = axis - bar_w;
svg_rect(x0, top, bar_w, BIN_HEIGHT, "green", "yellow");
double label_x = x0 + bar_w + LABEL_OFFSET;
double label_y = top + BIN_HEIGHT / 2 + 5;
svg_text(label_x, label_y, to_string(v));
top += BIN_HEIGHT;
}

@ -1,48 +1,46 @@
#include "text.h"
#include <iostream>
void show_histogram(const std::vector<size_t>& bins) {
bool gigant = false;
auto spaces = 0;
void show_histogram(std::vector<size_t> bins) {
size_t mx_count = 0;
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++;
bool need_scale = false;
for (size_t x : bins) {
if (x > mx_count) {
mx_count = x;
}
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++) {
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 << "*";
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);
}
} else {
for (size_t j = 0; j < bins[i]; j++) {
std::cout << "*";
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 << ' ';
}
std::cout << std::endl;
std::cout << s << "\n";
}
}

Двоичные данные
unittest/bin/Debug/unittest.exe

Двоичный файл не отображается.

Двоичные данные
unittest/obj/Debug/svg.o

Двоичный файл не отображается.

@ -2,16 +2,6 @@
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<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>
<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" />
</Cursor>
</File>
<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" />
@ -22,9 +12,19 @@
<Cursor1 position="216" topLine="0" />
</Cursor>
</File>
<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" />
</Cursor>
</File>
<File name="..\doctest.h" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="1170" topLine="0" />
</Cursor>
</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>

Загрузка…
Отмена
Сохранить