code: lab4 add curl file
Этот коммит содержится в:
@@ -1,8 +1,8 @@
|
||||
#include "histogram.h"
|
||||
#include <vector>
|
||||
using std::vector;
|
||||
// Ïîèñê ìèíèìóìà è ìàêñèìóìà
|
||||
void find_minmax(const std::vector<double>& numbers, double& min, double& max) {
|
||||
using namespace std;
|
||||
|
||||
void find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||
if (numbers.empty())
|
||||
{
|
||||
min = 0;
|
||||
@@ -17,21 +17,18 @@ void find_minmax(const std::vector<double>& numbers, double& min, double& max) {
|
||||
}
|
||||
}
|
||||
|
||||
// Ðàñ÷¸ò ãèñòîãðàììû
|
||||
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count) {
|
||||
std::vector<size_t> bins(bin_count, 0);
|
||||
|
||||
vector<size_t> make_histogram(vector<double> numbers, size_t bin_count) {
|
||||
double min, max;
|
||||
find_minmax(numbers, min, max);
|
||||
|
||||
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;
|
||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++) {
|
||||
double lo = min + j * bin_size;
|
||||
double hi = min + (j + 1) * bin_size;
|
||||
if ((lo <= number) && (number < hi)) {
|
||||
auto lo = min + j * bin_size;
|
||||
auto hi = min + (j + 1) * bin_size;
|
||||
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
@@ -40,6 +37,5 @@ std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bi
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
}
|
||||
|
||||
return bins;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#ifndef HISTOGRAM_H_INCLUDED
|
||||
#define HISTOGRAM_H_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
using std::vector;
|
||||
std::vector<size_t>
|
||||
make_histogram(const std::vector<double>& numbers, size_t bin_count);
|
||||
using namespace std;
|
||||
|
||||
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
|
||||
#define HISTOGRAM_INTERNAL_H_INCLUDED
|
||||
#include <vector>
|
||||
using std::vector;
|
||||
|
||||
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" />
|
||||
<Compiler>
|
||||
<Add option="-g" />
|
||||
<Add directory="curl/include" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add library="libcurl.dll.a" />
|
||||
<Add directory="curl/lib" />
|
||||
</Linker>
|
||||
</Target>
|
||||
<Target title="Release">
|
||||
<Option output="bin/Release/lab01" prefix_auto="1" extension_auto="1" />
|
||||
|
||||
93
main.cpp
93
main.cpp
@@ -3,55 +3,76 @@
|
||||
#include "histogram.h"
|
||||
#include "text.h"
|
||||
#include "svg.h"
|
||||
#include <curl/curl.h>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
struct Input {
|
||||
vector<double> numbers;
|
||||
size_t bin_count;
|
||||
size_t image_width;
|
||||
};
|
||||
|
||||
Input input_data() {
|
||||
Input in;
|
||||
Input input_data(istream& in, bool prompt) {
|
||||
Input inp;
|
||||
size_t number_count;
|
||||
cerr << "Enter number count: ";
|
||||
cin >> number_count;
|
||||
|
||||
in.numbers.resize(number_count);
|
||||
cerr << "Enter numbers: ";
|
||||
for (size_t i = 0; i < number_count; i++) {
|
||||
cin >> in.numbers[i];
|
||||
if (prompt){
|
||||
cerr << "Enter number 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 >> number_count;
|
||||
inp.numbers.resize(number_count);
|
||||
if (prompt){
|
||||
cerr << "Enter numbers: ";
|
||||
}
|
||||
for (size_t i = 0; i < number_count; i++) {
|
||||
in >> inp.numbers[i];
|
||||
}
|
||||
|
||||
return in;
|
||||
if (prompt) {
|
||||
cerr << "Enter bin count: ";
|
||||
}
|
||||
in >> inp.bin_count;
|
||||
return inp;
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto in = input_data();
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
show_histogram_svg(bins, in.image_width);
|
||||
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& 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;
|
||||
}
|
||||
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
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()) {
|
||||
cerr << "Error: Empty bins vector\n";
|
||||
return;
|
||||
}
|
||||
|
||||
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 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;
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
6
svg.h
6
svg.h
@@ -2,8 +2,8 @@
|
||||
#define SVG_H_INCLUDED
|
||||
|
||||
#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