Сравнить коммиты
22 Коммитов
07c73e5335
...
ef0fcfca11
| Автор | SHA1 | Дата | |
|---|---|---|---|
| ef0fcfca11 | |||
| e32d622ada | |||
| ce1828c63b | |||
| 2a9b70a833 | |||
| 928e229a58 | |||
| c89c94151c | |||
| 663e485fdb | |||
| 4813bf8c13 | |||
| 09c75c3057 | |||
| 2e7cc7b34b | |||
| 2817ee274c | |||
| 0c00522f14 | |||
| 5867964daa | |||
| 90318f8151 | |||
| 83e2103ca7 | |||
| 2f22e192b3 | |||
| f021197658 | |||
| d16031c3f4 | |||
| 80f146ba36 | |||
| c250c10e38 | |||
| abf30f27d1 | |||
| d5f702afa3 |
1
.gitignore
поставляемый
1
.gitignore
поставляемый
@@ -1,5 +1,6 @@
|
||||
/bin/
|
||||
/obj/
|
||||
/curl/
|
||||
*.rsuser
|
||||
*.suo
|
||||
*.user
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
|
||||
#include "histogram.h"
|
||||
#include "histogram_internal.h"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||
if (numbers.empty())
|
||||
if (numbers.empty()) {
|
||||
return false;
|
||||
}
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (double x : numbers) {
|
||||
@@ -16,7 +16,7 @@ bool find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||
if (x > max)
|
||||
max = x;
|
||||
}
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) {
|
||||
@@ -27,6 +27,7 @@ vector<size_t> make_histogram(const vector<double>& numbers, size_t 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++) {
|
||||
double lo = min + j * bin_size;
|
||||
double hi = min + (j + 1) * bin_size;
|
||||
@@ -35,15 +36,9 @@ vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found)
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
return bins;
|
||||
}
|
||||
|
||||
double compute_bin_size(const std::vector<double>& numbers, size_t bin_count) {
|
||||
double min, max;
|
||||
if (!find_minmax(numbers, min, max))
|
||||
return 0.0;
|
||||
return (max - min) / bin_count;
|
||||
}
|
||||
|
||||
@@ -2,5 +2,3 @@
|
||||
#include <vector>
|
||||
|
||||
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count);
|
||||
double compute_bin_size(const std::vector<double>& numbers, size_t bin_count);
|
||||
void show_histogram_svg(const std::vector<size_t>& bins, double bin_size, double min_val);
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include "histogram.h"
|
||||
#include "histogram_internal.h"
|
||||
#include "svg.h"
|
||||
#include "text.h"
|
||||
#include <string>
|
||||
#include <curl/curl.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -13,35 +14,62 @@ struct Input {
|
||||
size_t bin_count{};
|
||||
};
|
||||
|
||||
Input input_data() {
|
||||
Input input_data(istream& in, bool prompt) {
|
||||
if (prompt) cerr << "Enter number count: ";
|
||||
size_t number_count;
|
||||
cerr << "Enter number count: ";
|
||||
cin >> number_count;
|
||||
|
||||
Input in;
|
||||
in.numbers.resize(number_count);
|
||||
cerr << "Enter " << number_count << " numbers: ";
|
||||
in >> number_count;
|
||||
Input data;
|
||||
data.numbers.resize(number_count);
|
||||
if (prompt) cerr << "Enter " << number_count << " numbers: ";
|
||||
for (size_t i = 0; i < number_count; i++) {
|
||||
cin >> in.numbers[i];
|
||||
in >> data.numbers[i];
|
||||
}
|
||||
if (prompt) cerr << "Enter bin count: ";
|
||||
in >> data.bin_count;
|
||||
return data;
|
||||
}
|
||||
|
||||
cerr << "Enter bin count: ";
|
||||
cin >> in.bin_count;
|
||||
|
||||
return in;
|
||||
static size_t write_data(void* items, size_t item_size, size_t item_count, void* ctx) {
|
||||
size_t data_size = item_size * item_count;
|
||||
auto* buffer = reinterpret_cast<stringstream*>(ctx);
|
||||
buffer->write(reinterpret_cast<const char*>(items), data_size);
|
||||
return data_size;
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto in = input_data();
|
||||
Input download(const string& address) {
|
||||
stringstream buffer;
|
||||
CURL* curl = curl_easy_init();
|
||||
|
||||
double min_val, max_val;
|
||||
find_minmax(in.numbers, min_val, max_val);
|
||||
curl_easy_setopt(curl, CURLOPT_URL, address.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
||||
CURLcode res = curl_easy_perform(curl);
|
||||
if (res != CURLE_OK) {
|
||||
cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << "\n";
|
||||
curl_easy_cleanup(curl);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
double bin_size = compute_bin_size(in.numbers, in.bin_count);
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
double download_speed = 0.0;
|
||||
curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &download_speed);
|
||||
cerr << "Download speed: " << download_speed << " bytes/sec\n";
|
||||
|
||||
show_histogram_svg(bins, bin_size, min_val);
|
||||
curl_easy_cleanup(curl);
|
||||
return input_data(buffer, false);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
Input in;
|
||||
if (argc > 1) {
|
||||
in = download(argv[1]);
|
||||
}
|
||||
else {
|
||||
in = input_data(cin, true);
|
||||
}
|
||||
const auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
show_histogram_svg(bins);
|
||||
show_histogram_text(bins);
|
||||
curl_global_cleanup();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -76,10 +76,16 @@
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>
|
||||
</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>
|
||||
</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>
|
||||
</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
@@ -90,12 +96,18 @@
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>
|
||||
</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>
|
||||
</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>
|
||||
</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
@@ -104,11 +116,18 @@
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>C:\Users\brega\OneDrive\Рабочий стол\lab03\laba01upd3\curl\include</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>C:\Users\brega\OneDrive\Рабочий стол\lab03\laba01upd3\curl\lib</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>;libcurl.dll.a;libcurl.dll.a;libcrypto.a;libssl.a</AdditionalDependencies>
|
||||
</Link>
|
||||
<PostBuildEvent>
|
||||
<Command>
|
||||
</Command>
|
||||
</PostBuildEvent>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
@@ -118,12 +137,15 @@
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>C:\Users\brega\OneDrive\Рабочий стол\lab03\laba01upd3\curl\include</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>C:\Users\brega\OneDrive\Рабочий стол\lab03\laba01upd3\curl\lib</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>libcurl.lib;%(AdditionalDependencies);libcurl.dll.a;libcurl.dll.a;libcrypto.a;libssl.a</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
|
||||
44
svg.cpp
44
svg.cpp
@@ -4,11 +4,11 @@ using namespace std;
|
||||
|
||||
void svg_begin(double width, double height) {
|
||||
cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
|
||||
cout << "<svg "
|
||||
<< "width='" << width << "' "
|
||||
<< "height='" << height << "' "
|
||||
<< "viewBox='0 0 " << width << " " << height << "' "
|
||||
<< "xmlns='http://www.w3.org/2000/svg'>\n";
|
||||
cout << "<svg ";
|
||||
cout << "width='" << width << "' ";
|
||||
cout << "height='" << height << "' ";
|
||||
cout << "viewBox='0 0 " << width << " " << height << "' ";
|
||||
cout << "xmlns='http://www.w3.org/2000/svg'>\n";
|
||||
}
|
||||
|
||||
void svg_end() {
|
||||
@@ -33,40 +33,38 @@ void svg_rect(double x, double y, double width, double height,
|
||||
<< "' />\n";
|
||||
}
|
||||
|
||||
void show_histogram_svg(const std::vector<size_t>& bins, double bin_size, double min_val) {
|
||||
void show_histogram_svg(const vector<size_t>& bins) {
|
||||
const double IMAGE_WIDTH = 400;
|
||||
const double IMAGE_HEIGHT = 300;
|
||||
const double TEXT_LEFT = 20;
|
||||
const double TEXT_BASELINE = 20;
|
||||
const double TEXT_WIDTH = 50;
|
||||
const double BIN_HEIGHT = 30;
|
||||
const double VSPACE = BIN_HEIGHT + TEXT_BASELINE * 2 + 5;
|
||||
const double BLOCK_WIDTH = 10;
|
||||
|
||||
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
||||
|
||||
size_t max_count = 0;
|
||||
for (size_t c : bins)
|
||||
if (c > max_count)
|
||||
max_count = c;
|
||||
double max_width = IMAGE_WIDTH - TEXT_WIDTH - 100;
|
||||
for (size_t count : bins) {
|
||||
if (count > max_count) {
|
||||
max_count = count;
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t j = 0; j < bins.size(); ++j) {
|
||||
size_t count = bins[j];
|
||||
double y = j * VSPACE;
|
||||
|
||||
double width;
|
||||
double max_width = IMAGE_WIDTH - TEXT_WIDTH;
|
||||
double top = 0;
|
||||
for (size_t count : bins) {
|
||||
double bin_width;
|
||||
if (max_count > 0) {
|
||||
width = static_cast<double>(count) / max_count * max_width;
|
||||
bin_width = static_cast<double>(count) / max_count * max_width;
|
||||
}
|
||||
else {
|
||||
width = 0.0;
|
||||
bin_width = 0.0;
|
||||
}
|
||||
|
||||
svg_text(TEXT_LEFT, y + TEXT_BASELINE, to_string(count));
|
||||
svg_rect(TEXT_WIDTH, y, width, BIN_HEIGHT, "blue", "blue");
|
||||
double boundary = min_val + bin_size * (j + 1);
|
||||
svg_text(TEXT_WIDTH,y + BIN_HEIGHT + TEXT_BASELINE,to_string(boundary)
|
||||
);
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(count));
|
||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "blue", "blue");
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
|
||||
svg_end();
|
||||
|
||||
2
svg.h
2
svg.h
@@ -9,4 +9,4 @@ void svg_text(double left, double baseline, const std::string& text);
|
||||
void svg_rect(double x, double y, double width, double height,
|
||||
const std::string& stroke = "black",
|
||||
const std::string& fill = "black");
|
||||
void show_histogram_svg(const std::vector<size_t>& bins, double bin_size, double min_val);
|
||||
void show_histogram_svg(const std::vector<size_t>& bins);
|
||||
|
||||
Ссылка в новой задаче
Block a user