code: lab4 add curl file
Этот коммит содержится в:
@@ -1,8 +1,8 @@
|
|||||||
#include "histogram.h"
|
#include "histogram.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
using std::vector;
|
using namespace std;
|
||||||
// Ïîèñê ìèíèìóìà è ìàêñèìóìà
|
|
||||||
void find_minmax(const std::vector<double>& numbers, double& min, double& max) {
|
void find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||||
if (numbers.empty())
|
if (numbers.empty())
|
||||||
{
|
{
|
||||||
min = 0;
|
min = 0;
|
||||||
@@ -17,21 +17,18 @@ void find_minmax(const std::vector<double>& numbers, double& min, double& max) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ðàñ÷¸ò ãèñòîãðàììû
|
vector<size_t> make_histogram(vector<double> numbers, size_t bin_count) {
|
||||||
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count) {
|
|
||||||
std::vector<size_t> bins(bin_count, 0);
|
|
||||||
|
|
||||||
double min, max;
|
double min, max;
|
||||||
find_minmax(numbers, min, max);
|
find_minmax(numbers, min, max);
|
||||||
|
|
||||||
double bin_size = (max - min) / bin_count;
|
double bin_size = (max - min) / bin_count;
|
||||||
|
vector<size_t> bins(bin_count);
|
||||||
|
|
||||||
for (double number : numbers) {
|
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++) {
|
||||||
double lo = min + j * bin_size;
|
auto lo = min + j * bin_size;
|
||||||
double hi = min + (j + 1) * bin_size;
|
auto hi = min + (j + 1) * bin_size;
|
||||||
if ((lo <= number) && (number < hi)) {
|
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
|
||||||
bins[j]++;
|
bins[j]++;
|
||||||
found = true;
|
found = true;
|
||||||
}
|
}
|
||||||
@@ -40,6 +37,5 @@ std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bi
|
|||||||
bins[bin_count - 1]++;
|
bins[bin_count - 1]++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return bins;
|
return bins;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
#ifndef HISTOGRAM_H_INCLUDED
|
#ifndef HISTOGRAM_H_INCLUDED
|
||||||
#define HISTOGRAM_H_INCLUDED
|
#define HISTOGRAM_H_INCLUDED
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
using std::vector;
|
using namespace std;
|
||||||
std::vector<size_t>
|
|
||||||
make_histogram(const std::vector<double>& numbers, size_t bin_count);
|
|
||||||
|
|
||||||
|
vector<size_t> make_histogram(vector<double> numbers, size_t bin_count);
|
||||||
|
|
||||||
#endif // HISTOGRAM_H_INCLUDED
|
#endif
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
|
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
#define HISTOGRAM_INTERNAL_H_INCLUDED
|
#define HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
#include <vector>
|
|
||||||
using std::vector;
|
|
||||||
void find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
void find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
||||||
|
|
||||||
#endif // HISTOGRAM_INTERNAL_H_INCLUDED
|
#endif
|
||||||
|
|||||||
@@ -13,7 +13,12 @@
|
|||||||
<Option compiler="gcc" />
|
<Option compiler="gcc" />
|
||||||
<Compiler>
|
<Compiler>
|
||||||
<Add option="-g" />
|
<Add option="-g" />
|
||||||
|
<Add directory="curl/include" />
|
||||||
</Compiler>
|
</Compiler>
|
||||||
|
<Linker>
|
||||||
|
<Add library="libcurl.dll.a" />
|
||||||
|
<Add directory="curl/lib" />
|
||||||
|
</Linker>
|
||||||
</Target>
|
</Target>
|
||||||
<Target title="Release">
|
<Target title="Release">
|
||||||
<Option output="bin/Release/lab01" prefix_auto="1" extension_auto="1" />
|
<Option output="bin/Release/lab01" prefix_auto="1" extension_auto="1" />
|
||||||
|
|||||||
87
main.cpp
87
main.cpp
@@ -3,55 +3,76 @@
|
|||||||
#include "histogram.h"
|
#include "histogram.h"
|
||||||
#include "text.h"
|
#include "text.h"
|
||||||
#include "svg.h"
|
#include "svg.h"
|
||||||
|
#include <curl/curl.h>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
struct Input {
|
struct Input {
|
||||||
vector<double> numbers;
|
vector<double> numbers;
|
||||||
size_t bin_count;
|
size_t bin_count;
|
||||||
size_t image_width;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Input input_data() {
|
Input input_data(istream& in, bool prompt) {
|
||||||
Input in;
|
Input inp;
|
||||||
size_t number_count;
|
size_t number_count;
|
||||||
|
if (prompt){
|
||||||
cerr << "Enter number count: ";
|
cerr << "Enter number count: ";
|
||||||
cin >> number_count;
|
}
|
||||||
|
in >> number_count;
|
||||||
in.numbers.resize(number_count);
|
inp.numbers.resize(number_count);
|
||||||
|
if (prompt){
|
||||||
cerr << "Enter numbers: ";
|
cerr << "Enter numbers: ";
|
||||||
|
}
|
||||||
for (size_t i = 0; i < number_count; i++) {
|
for (size_t i = 0; i < number_count; i++) {
|
||||||
cin >> in.numbers[i];
|
in >> inp.numbers[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (prompt) {
|
||||||
cerr << "Enter bin count: ";
|
cerr << "Enter bin count: ";
|
||||||
cin >> in.bin_count;
|
|
||||||
|
|
||||||
const size_t BLOCK_WIDTH = 10;
|
|
||||||
const size_t MIN_WIDTH = 70;
|
|
||||||
const size_t MAX_WIDTH = 800;
|
|
||||||
size_t required_min_width = number_count * BLOCK_WIDTH / 3;
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
cerr << "Enter image width (" << MIN_WIDTH << "-" << MAX_WIDTH << "), minimum " << required_min_width << ": ";
|
|
||||||
cin >> in.image_width;
|
|
||||||
|
|
||||||
if (in.image_width < MIN_WIDTH) {
|
|
||||||
cerr << "Width is too small (minimum " << MIN_WIDTH << ")\n";
|
|
||||||
} else if (in.image_width > MAX_WIDTH) {
|
|
||||||
cerr << "Width is too large (maximum " << MAX_WIDTH << ")\n";
|
|
||||||
} else if (in.image_width < required_min_width) {
|
|
||||||
cerr << "Width is less than 1/3 of numbers count * block width (" << required_min_width << ")\n";
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
in >> inp.bin_count;
|
||||||
|
return inp;
|
||||||
return in;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
size_t
|
||||||
auto in = input_data();
|
write_data(void* items, size_t item_size, size_t item_count, void* ctx) {
|
||||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
stringstream* buffer = reinterpret_cast<stringstream*>(ctx);
|
||||||
show_histogram_svg(bins, in.image_width);
|
size_t data_size = item_size * item_count;
|
||||||
|
(*buffer).write(reinterpret_cast<const char*>(items), data_size);
|
||||||
|
return data_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
Input
|
||||||
|
download(const string& adress){
|
||||||
|
stringstream buffer;
|
||||||
|
CURL* curl = curl_easy_init();
|
||||||
|
if(curl) {
|
||||||
|
|
||||||
|
CURLcode res;
|
||||||
|
curl_easy_setopt(curl, CURLOPT_URL, adress.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){
|
||||||
|
cerr << curl_easy_strerror(res);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto bins = make_histogram(input.numbers, input.bin_count);
|
||||||
|
show_histogram_svg(bins);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
8
svg.cpp
8
svg.cpp
@@ -27,20 +27,21 @@ void svg_rect(double x, double y, double width, double height, string stroke = "
|
|||||||
<< "' stroke='" << stroke << "' fill='" << fill << "' />\n";
|
<< "' stroke='" << stroke << "' fill='" << fill << "' />\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void show_histogram_svg(const vector<size_t>& bins, size_t image_width) {
|
void show_histogram_svg(const vector<size_t>& bins) {
|
||||||
if (bins.empty()) {
|
if (bins.empty()) {
|
||||||
cerr << "Error: Empty bins vector\n";
|
cerr << "Error: Empty bins vector\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const auto IMAGE_WIDTH = 400;
|
||||||
const auto IMAGE_HEIGHT = 300;
|
const auto IMAGE_HEIGHT = 300;
|
||||||
const auto TEXT_LEFT = 20;
|
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 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;
|
||||||
size_t max_count = bins[0];
|
size_t max_count = bins[0];
|
||||||
@@ -59,3 +60,4 @@ void show_histogram_svg(const vector<size_t>& bins, size_t image_width) {
|
|||||||
|
|
||||||
svg_end();
|
svg_end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
6
svg.h
6
svg.h
@@ -2,8 +2,8 @@
|
|||||||
#define SVG_H_INCLUDED
|
#define SVG_H_INCLUDED
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cstddef> // Äëÿ size_t
|
#include <cstddef> // Äëÿ size_t
|
||||||
|
|
||||||
void show_histogram_svg(const std::vector<size_t>& bins, size_t image_width);
|
void show_histogram_svg(const std::vector<size_t>& bins);
|
||||||
|
|
||||||
#endif
|
#endif // SVG_H_INCLUDED
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user