code: исправлены ошибки кода
Этот коммит содержится в:
@@ -1,71 +1,55 @@
|
|||||||
#include <math.h>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <conio.h>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <cmath>
|
||||||
#include "histogram.h"
|
#include "histogram.h"
|
||||||
|
#include "histogram_internal.h"
|
||||||
|
#include <conio.h>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
void find_minmax(vector<double> numbers, double& min, double& max)
|
||||||
bool find_minmax(vector<double> numbers, double& min, double& max)
|
|
||||||
{
|
{
|
||||||
if (numbers.empty())
|
min = numbers[0];
|
||||||
|
max = numbers[0];
|
||||||
|
for (double x : numbers)
|
||||||
{
|
{
|
||||||
return true;
|
if (x < min)
|
||||||
}
|
{
|
||||||
else
|
min = x;
|
||||||
{
|
|
||||||
min = numbers[0];
|
|
||||||
for (auto i = 0; i < numbers.size(); i++) {
|
|
||||||
if (numbers[i] < min) {
|
|
||||||
min = numbers[i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
else if (x > max)
|
||||||
max = numbers[0];
|
{
|
||||||
for (auto i = 0; i < numbers.size(); i++) {
|
max = x;
|
||||||
if (numbers[i] > max) {
|
|
||||||
max = numbers[i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
vector <size_t> make_histogramm(vector<double>numbers, size_t bin_count)
|
vector<size_t> make_histogramm (vector<double> numbers, size_t bin_count)
|
||||||
{
|
{
|
||||||
double min, max;
|
double min;
|
||||||
find_minmax(numbers, min, max);
|
double max;
|
||||||
double binSize = (max - min) / bin_count;
|
find_minmax (numbers, min, max);
|
||||||
|
double bin_size = (max - min) / bin_count;
|
||||||
vector<size_t> bins(bin_count);
|
vector<size_t> bins(bin_count);
|
||||||
|
|
||||||
for (size_t i = 0; i < numbers.size(); i++)
|
for (size_t i = 0; i < numbers.size(); i++)
|
||||||
{
|
{
|
||||||
bool found = false;
|
bool found = false;
|
||||||
for (size_t j = 0; (j <= bin_count - 1) && !found; j++)
|
for (size_t j = 0; (j < bin_count - 1) && !found; j++)
|
||||||
|
{
|
||||||
|
auto lo = min + j * bin_size;
|
||||||
|
auto hi = min + (j + 1) * bin_size;
|
||||||
|
if ((lo <= numbers[i]) && (numbers[i] < hi))
|
||||||
{
|
{
|
||||||
auto lo = min + j * binSize;
|
|
||||||
auto hi = min + (j + 1) * binSize;
|
|
||||||
if ((numbers[i] >= lo) && (numbers[i] < hi))
|
|
||||||
{
|
|
||||||
bins[j]++;
|
bins[j]++;
|
||||||
found = true;
|
found = true;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!found)
|
if (!found)
|
||||||
|
{
|
||||||
bins[bin_count - 1]++;
|
bins[bin_count - 1]++;
|
||||||
}
|
}
|
||||||
int max_count = bins[0];
|
|
||||||
for (size_t i = 0; i < bin_count; i++)
|
|
||||||
{
|
|
||||||
if (bins[i] > max_count)
|
|
||||||
max_count = bins[i];
|
|
||||||
}
|
|
||||||
if (max_count > 76)
|
|
||||||
{
|
|
||||||
for (size_t i = 0; i < bin_count; i++)
|
|
||||||
{
|
|
||||||
int count = bins[i];
|
|
||||||
size_t height = 76 * (static_cast<double>(count) / max_count);
|
|
||||||
bins[i] = height;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return bins;
|
return bins;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
#define HISTOGRAM_INTERNAL_H_INCLUDED
|
#define HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
void find_minmax(std::vector<double> numbers, double &min, double &max);
|
void find_minmax(std::vector<double> numbers, double &min, double &max);
|
||||||
|
|
||||||
#endif // HISTOGRAM_INTERNAL_H_INCLUDED
|
#endif // HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
|
|||||||
45
main.cpp
45
main.cpp
@@ -5,42 +5,40 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include "histogram.h"
|
#include "histogram.h"
|
||||||
#include "text.h"
|
#include "text.h"
|
||||||
#include "svg.h"
|
|
||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include "svg.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
struct Input
|
struct Input
|
||||||
{
|
{
|
||||||
vector<double> numbers;
|
vector<double> numbers;
|
||||||
size_t bin_count{};
|
size_t bin_count{};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
Input
|
Input
|
||||||
input_data(istream& in, bool prompt)
|
input_data(istream& in, bool promt)
|
||||||
{
|
{
|
||||||
if (prompt)
|
size_t number_count;
|
||||||
|
if (promt)
|
||||||
{
|
{
|
||||||
cerr << "Enter number count: ";
|
cerr << "Enter number count: ";
|
||||||
}
|
}
|
||||||
size_t number_count;
|
|
||||||
in >> number_count;
|
in >> number_count;
|
||||||
|
|
||||||
Input ik;
|
Input ik;
|
||||||
ik.numbers.resize(number_count);
|
ik.numbers.resize(number_count);
|
||||||
cerr << "Input numbers: ";
|
|
||||||
for (size_t i = 0; i < number_count; i++)
|
for (size_t i = 0; i < number_count; i++)
|
||||||
{
|
{
|
||||||
in >> ik.numbers[i];
|
in >> ik.numbers[i];
|
||||||
}
|
}
|
||||||
cerr << "Input bin count: ";
|
if (promt)
|
||||||
|
{
|
||||||
|
cerr << "Enter bin count: ";
|
||||||
|
}
|
||||||
in>> ik.bin_count;
|
in>> ik.bin_count;
|
||||||
return ik;
|
return ik;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
size_t write_data(void* items, size_t item_size, size_t item_count, void* ctx)
|
size_t write_data(void* items, size_t item_size, size_t item_count, void* ctx)
|
||||||
{
|
{
|
||||||
size_t data_size = item_size * item_count;
|
size_t data_size = item_size * item_count;
|
||||||
@@ -50,7 +48,6 @@ size_t write_data(void* items, size_t item_size, size_t item_count, void* ctx)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Input
|
Input
|
||||||
download(const string& address)
|
download(const string& address)
|
||||||
{
|
{
|
||||||
@@ -63,16 +60,21 @@ download(const string& address)
|
|||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
||||||
res = curl_easy_perform(curl);
|
res = curl_easy_perform(curl);
|
||||||
curl_easy_cleanup(curl);
|
|
||||||
if (res != CURLE_OK)
|
if (res != CURLE_OK)
|
||||||
|
{
|
||||||
{
|
|
||||||
fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
|
fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
curl_easy_cleanup(curl);
|
if(res == CURLE_OK)
|
||||||
|
{
|
||||||
|
long req;
|
||||||
|
res = curl_easy_getinfo(curl, CURLINFO_REQUEST_SIZE, &req);
|
||||||
|
if(!res)
|
||||||
|
cerr<<"Request size: %ld bytes: "<< req;
|
||||||
|
}
|
||||||
|
curl_easy_cleanup(curl);
|
||||||
|
|
||||||
|
}
|
||||||
return input_data(buffer, false);
|
return input_data(buffer, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,12 +92,7 @@ int main(int argc, char* argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
const auto bins = make_histogramm(input.numbers, input.bin_count);
|
const auto bins = make_histogramm(input.numbers, input.bin_count);
|
||||||
|
show_histogram_svg(bins);
|
||||||
show_histogram_svg(bins);
|
|
||||||
|
|
||||||
getch();
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
35
svg.cpp
35
svg.cpp
@@ -22,6 +22,7 @@ svg_end()
|
|||||||
{
|
{
|
||||||
cout << "</svg>\n";
|
cout << "</svg>\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
svg_text(double left, double baseline, string text)
|
svg_text(double left, double baseline, string text)
|
||||||
{
|
{
|
||||||
@@ -29,47 +30,45 @@ svg_text(double left, double baseline, string text)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
svg_rect(double x, double y, double width, double height, string stroke, string fill)
|
svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black")
|
||||||
{
|
{
|
||||||
cout << "<rect x='"<<x<<"' y='"<<y<<"' width='"<<width<<"' height='"<<height<<"' stroke='"<<stroke<<"' fill='"<<fill<<"' />";
|
cout << "<rect x='"<<x<<"' y='"<<y<<"' width='"<<width<<"' height='"<<height<<"' stroke='"<<stroke<<"' fill='"<<fill<<"' />";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
show_histogram_svg(const vector<size_t>& bins) {
|
show_histogram_svg(const vector<size_t>& bins)
|
||||||
|
{
|
||||||
const auto IMAGE_WIDTH = 400;
|
const auto IMAGE_WIDTH = 400;
|
||||||
const auto IMAGE_HEIGHT = 300;
|
const auto IMAGE_HEIGHT = 300;
|
||||||
const auto TEXT_LEFT = 10;
|
const auto TEXT_LEFT = 20;
|
||||||
const auto TEXT_BASELINE = 20;
|
const auto TEXT_BASELINE = 20;
|
||||||
const auto TEXT_WIDTH = 50;
|
const auto TEXT_WIDTH = 50;
|
||||||
const auto BIN_HEIGHT = 30;
|
const auto BIN_HEIGHT = 30;
|
||||||
const auto BLOCK_WIDTH = 10;
|
const auto BLOCK_WIDTH = 10;
|
||||||
|
const auto BLACK = "black";
|
||||||
|
const auto RED = "red";
|
||||||
const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH;
|
const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH;
|
||||||
|
|
||||||
|
|
||||||
svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT);
|
svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT);
|
||||||
|
|
||||||
double top = 0;
|
double top = 0;
|
||||||
double max_count = bins[0];
|
double max_count = bins[0];
|
||||||
for (size_t i = 0; i < bins.size(); i++)
|
for (size_t i = 0; i < bins.size(); i++)
|
||||||
|
{
|
||||||
|
if (max_count<bins[i])
|
||||||
{
|
{
|
||||||
if (bins[i] > max_count)
|
max_count=bins[i];
|
||||||
max_count = bins[i];
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (size_t bin : bins)
|
for (size_t bin : bins)
|
||||||
{
|
{
|
||||||
|
double bin_width = (MAX_WIDTH)*(bin/max_count);
|
||||||
const 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, BLACK, RED);
|
||||||
const double emptiness_width = MAX_WIDTH - bin_width;
|
|
||||||
|
|
||||||
svg_rect(0, top, emptiness_width, BIN_HEIGHT, "white", "#ffffff");
|
|
||||||
|
|
||||||
svg_text(TEXT_LEFT + MAX_WIDTH, top + TEXT_BASELINE, to_string(bin));
|
|
||||||
|
|
||||||
svg_rect(emptiness_width, top, bin_width, BIN_HEIGHT, "red", "#aaffaa");
|
|
||||||
|
|
||||||
top += BIN_HEIGHT;
|
top += BIN_HEIGHT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user