Сравнить коммиты

...

13 Коммитов

4 изменённых файлов: 130 добавлений и 40 удалений

1
.gitignore поставляемый
Просмотреть файл

@@ -1,2 +1,3 @@
*.exe
*.dSYM/
/curl

109
l03.cpp
Просмотреть файл

@@ -3,49 +3,114 @@
#include "histogram.h"
#include "svg.h"
#include "text.h"
#include <curl/curl.h>
#include <sstream>
#include <string>
using namespace std;
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
struct Input
{
vector<double> numbers;
size_t bin_count{};
};
Input input_data()
Input input_data(istream &in, bool promt)
{
size_t number_count, bin_count;
cerr << "Enter number count: ";
cin >> number_count;
if (promt)
{
cerr << "Enter number count: ";
}
in >> number_count;
Input in;
in.numbers.resize(number_count);
Input input;
cerr << "Enter numbers: ";
// vector<double> numbers(number_count);
input.numbers.resize(number_count);
if (promt)
{
cerr << "Enter numbers: ";
}
for (size_t i = 0; i < number_count; i++)
{
cin >> in.numbers[i];
in >> input.numbers[i];
}
cerr << "Count of baskets: ";
cin >> in.bin_count;
if (promt)
{
cerr << "Count of baskets: ";
}
in >> input.bin_count;
return in;
return input;
}
int main()
size_t
write_data(void *items, size_t item_size, size_t item_count, void *ctx)
{
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_svg(bins);
return 0;
size_t data_size = item_count * item_size;
stringstream *buffer = reinterpret_cast<stringstream *>(ctx);
char *item = reinterpret_cast<char *>(items);
//(*buffer).write(item, data_size);
buffer->write(item, data_size);
return data_size;
}
Input download(const string &address)
{
stringstream buffer;
// address.c_str();
curl_global_init(CURL_GLOBAL_ALL);
{
CURL *curl = curl_easy_init();
if (curl)
{
CURLcode res;
double total;
curl_easy_setopt(curl, CURLOPT_URL, address.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
exit(1);
}
if (CURLE_OK == res)
{
res = curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total);
cerr << "Time spending for downloading file: " << total << endl;
}
curl_easy_cleanup(curl);
}
}
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);
}
const auto bins = make_histogram(input.numbers, input.bin_count);
show_histogram_svg(bins);
}

58
svg.cpp
Просмотреть файл

@@ -3,14 +3,6 @@
#include "svg.h"
// #include"svg_rect.h"
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 BLOCK_WIDTH = 10;
void svg_begin(double width, double height)
{
std::cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
@@ -26,35 +18,59 @@ void svg_end()
std::cout << "</svg>\n";
}
void svg_text(double left, double baseline, std::string text)
void svg_text(double left, double baseline, std::string text, int check)
{
// std::cout << "<text x='20' y='35'>text</text>";
std::cout << "<text x=' " << left << " ' y='" << baseline << "' > " << text << " </text>";
if (check == 0)
{
std::cout << "<text x=' " << left << " ' y='" << baseline << "'> uncorectly width of image </text>";
//std::cout << "<text x='20' y='35' >uncorectly width of image </text>"; //больше 100 ничего нет
}
if (check == 1)
{
std::cout << "<text x=' " << left << " ' y='" << baseline << "' > " << text << " </text>";
}
// std::cout << "<text x='20' y='" << baseline << "' >2</text>";
}
void svg_rect(double x, double y, double width, double height)
void svg_rect(double x, double y, double width, double height, int check)
{
//if (check == 0)
//{
//std::cout << "<rect x=' " << x << " ' y='" << y << "' width='" << width << "' height='" << height << " ' stroke='yellow' >uncorectly width of image</rect>";
//}
std::cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << " ' stroke='yellow' fill='#9932CC' ></rect>";
if ( check == 1)
{
std::cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << " ' stroke='yellow' fill='#9932CC' ></rect>";
}
}
void show_histogram_svg(const std::vector<int> &bins)
void show_histogram_svg(const std::vector<int> &bins, size_t IMAGE_WIDTH)
{
const auto SCALE = (IMAGE_WIDTH - TEXT_WIDTH);
const auto IMAGE_WIDTH = 400;
// std::cout << IMAGE_WIDTH <<std::endl;
// const auto IMAGE_WIDTH = 400;
// if (IMAGE_WIDTH > 70 && IMAGE_WIDTH < 800){
// return 1;
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 BLOCK_WIDTH = 10;
const auto SCALE = (IMAGE_WIDTH - TEXT_WIDTH);
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
double top = 0;
for (size_t bin : bins)
{
int check = 0;
double bin_width = BLOCK_WIDTH * bin;
if (bin_width > SCALE)
@@ -62,8 +78,16 @@ void show_histogram_svg(const std::vector<int> &bins)
bin_width = SCALE;
}
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
//if (IMAGE_WIDTH < 70 && IMAGE_WIDTH > 800)
//{
//check = 0;
//}
if (IMAGE_WIDTH > 70 && IMAGE_WIDTH < 800 && IMAGE_WIDTH > 1./3*(bin*BLOCK_WIDTH))
{
check = 1;
}
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin), check);
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, check);
top += BIN_HEIGHT;
}

2
svg.h
Просмотреть файл

@@ -3,6 +3,6 @@
#include <vector>
void
show_histogram_svg(const std::vector<int>& bins);
show_histogram_svg(const std::vector<int>& bins, size_t IMAGE_WIDTH);
#endif //SVG_H_INCLUDED