code: исправлены ошибки кода

main
LiTekho 2 лет назад
Родитель 8c602901e5
Сommit ae349b5c93

@ -1,71 +1,55 @@
#include <math.h>
#include <iostream>
#include <conio.h>
#include <vector>
#include <cmath>
#include "histogram.h"
#include "histogram_internal.h"
#include <conio.h>
using namespace std;
bool find_minmax(vector<double> numbers, double& min, double& max)
void find_minmax(vector<double> numbers, double& min, double& max)
{
if (numbers.empty())
min = numbers[0];
max = numbers[0];
for (double x : numbers)
{
return true;
}
else
{
min = numbers[0];
for (auto i = 0; i < numbers.size(); i++) {
if (numbers[i] < min) {
min = numbers[i];
}
if (x < min)
{
min = x;
}
max = numbers[0];
for (auto i = 0; i < numbers.size(); i++) {
if (numbers[i] > max) {
max = numbers[i];
}
else if (x > max)
{
max = x;
}
}
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;
find_minmax(numbers, min, max);
double binSize = (max - min) / bin_count;
double min;
double max;
find_minmax (numbers, min, max);
double bin_size = (max - min) / bin_count;
vector<size_t> bins(bin_count);
for (size_t i = 0; i < numbers.size(); i++)
{
{
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]++;
found = true;
}
}
if (!found)
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++)
if (!found)
{
int count = bins[i];
size_t height = 76 * (static_cast<double>(count) / max_count);
bins[i] = height;
bins[bin_count - 1]++;
}
}
return bins;
}

@ -2,7 +2,6 @@
#define HISTOGRAM_INTERNAL_H_INCLUDED
#include <vector>
void find_minmax(std::vector<double> numbers, double &min, double &max);
#endif // HISTOGRAM_INTERNAL_H_INCLUDED

@ -5,42 +5,40 @@
#include <fstream>
#include "histogram.h"
#include "text.h"
#include "svg.h"
#include <curl/curl.h>
#include <sstream>
#include <string>
#include "svg.h"
using namespace std;
struct Input
{
vector<double> numbers;
size_t bin_count{};
};
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: ";
}
size_t number_count;
in >> number_count;
Input ik;
ik.numbers.resize(number_count);
cerr << "Input numbers: ";
for (size_t i = 0; i < number_count; i++)
{
{
in >> ik.numbers[i];
}
cerr << "Input bin count: ";
}
if (promt)
{
cerr << "Enter bin count: ";
}
in>> ik.bin_count;
return ik;
}
size_t write_data(void* items, size_t item_size, size_t item_count, void* ctx)
{
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
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_WRITEDATA, &buffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res != CURLE_OK)
{
{
fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
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);
}
@ -90,12 +92,7 @@ int main(int argc, char* argv[])
}
const auto bins = make_histogramm(input.numbers, input.bin_count);
show_histogram_svg(bins);
getch();
return 0;
show_histogram_svg(bins);
}

@ -22,6 +22,7 @@ svg_end()
{
cout << "</svg>\n";
}
void
svg_text(double left, double baseline, string text)
{
@ -29,47 +30,45 @@ svg_text(double left, double baseline, string text)
}
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
show_histogram_svg(const vector<size_t>& bins) {
show_histogram_svg(const vector<size_t>& bins)
{
const auto IMAGE_WIDTH = 400;
const auto IMAGE_HEIGHT = 300;
const auto TEXT_LEFT = 10;
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 BLACK = "black";
const auto RED = "red";
const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH;
svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT);
double top = 0;
double max_count = bins[0];
double max_count = bins[0];
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)
{
const double bin_width = MAX_WIDTH * (bin/max_count);
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");
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);
top += BIN_HEIGHT;
}

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