BobrovnikYA 1 неделю назад
Родитель dc84cefcc9
Сommit d398de7076

1
.gitignore поставляемый

@ -1,2 +1,3 @@
/bin /bin
/obj /obj
/curl

@ -14,6 +14,10 @@
<Compiler> <Compiler>
<Add option="-g" /> <Add option="-g" />
</Compiler> </Compiler>
<Linker>
<Add option="-static-libstdc++" />
<Add option="-static" />
</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" />
@ -31,7 +35,15 @@
<Compiler> <Compiler>
<Add option="-Wall" /> <Add option="-Wall" />
<Add option="-fexceptions" /> <Add option="-fexceptions" />
<Add directory="curl/include" />
</Compiler> </Compiler>
<Linker>
<Add option="-static-libstdc++" />
<Add option="-static-libgcc" />
<Add option="-static" />
<Add library="curl/lib/libcurl.dll.a" />
<Add directory="curl/lib" />
</Linker>
<Unit filename=".gitignore" /> <Unit filename=".gitignore" />
<Unit filename="histogram.cpp" /> <Unit filename="histogram.cpp" />
<Unit filename="histogram.h" /> <Unit filename="histogram.h" />

@ -30,12 +30,15 @@
"svg.h" "svg.h"
<string> <string>
1745706239 source:c:\users\liza\desktop\lab01\lab01\main.cpp 1749210284 source:c:\users\liza\desktop\lab01\lab01\main.cpp
<iostream> <iostream>
<vector> <vector>
<sstream>
<string>
"histogram.h" "histogram.h"
"text.h"
"svg.h" "svg.h"
"text.h"
<curl/curl.h>
1746171356 c:\users\liza\desktop\lab01\lab01\histogram.h 1746171356 c:\users\liza\desktop\lab01\lab01\histogram.h
<vector> <vector>
@ -50,3 +53,72 @@
<vector> <vector>
<cstddef> <cstddef>
1746171356 source:c:\users\liza\desktop\lab01\lab01\histogram.cpp
"histogram.h"
<iostream>
<cstddef>
1748597934 source:c:\users\liza\desktop\lab01\lab01\svg.cpp
"svg.h"
<iostream>
<vector>
1746177922 source:c:\users\liza\desktop\lab01\lab01\text.cpp
"text.h"
<iostream>
<vector>
<cstddef>
1748873299 c:\users\liza\desktop\lab01\lab01\curl\include\curl\curl.h
"curlver.h"
"system.h"
<stdio.h>
<limits.h>
<sys/param.h>
<sys/types.h>
<time.h>
<winsock2.h>
<ws2tcpip.h>
<sys/select.h>
<sys/socket.h>
<sys/time.h>
"easy.h"
"multi.h"
"urlapi.h"
"options.h"
"header.h"
"websockets.h"
"mprintf.h"
"typecheck-gcc.h"
1748873299 c:\users\liza\desktop\lab01\lab01\curl\include\curl\curlver.h
1748873299 c:\users\liza\desktop\lab01\lab01\curl\include\curl\system.h
<ConditionalMacros.h>
<inttypes.h>
<inttypes.h>
<sys/types.h>
<sys/socket.h>
<sys/poll.h>
1748873299 c:\users\liza\desktop\lab01\lab01\curl\include\curl\easy.h
1748873299 c:\users\liza\desktop\lab01\lab01\curl\include\curl\multi.h
"curl.h"
1748873299 c:\users\liza\desktop\lab01\lab01\curl\include\curl\urlapi.h
"curl.h"
1748873299 c:\users\liza\desktop\lab01\lab01\curl\include\curl\options.h
1748873299 c:\users\liza\desktop\lab01\lab01\curl\include\curl\header.h
1748873299 c:\users\liza\desktop\lab01\lab01\curl\include\curl\websockets.h
1748873299 c:\users\liza\desktop\lab01\lab01\curl\include\curl\mprintf.h
<stdarg.h>
<stdio.h>
"curl.h"
1748873299 c:\users\liza\desktop\lab01\lab01\curl\include\curl\typecheck-gcc.h

@ -2,19 +2,49 @@
<CodeBlocks_layout_file> <CodeBlocks_layout_file>
<FileVersion major="1" minor="0" /> <FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" /> <ActiveTarget name="Debug" />
<File name="svg.h" open="1" top="0" tabpos="7" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="195" topLine="0" />
</Cursor>
</File>
<File name="histogram_internal.h" open="1" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="37" topLine="0" />
</Cursor>
</File>
<File name="histogram.cpp" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <File name="histogram.cpp" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="0" topLine="0" /> <Cursor1 position="0" topLine="0" />
</Cursor> </Cursor>
</File> </File>
<File name="text.cpp" open="1" top="1" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <File name="svg.cpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="77" topLine="0" /> <Cursor1 position="356" topLine="0" />
</Cursor> </Cursor>
</File> </File>
<File name="svg.cpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <File name="histogram.h" open="1" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="201" topLine="0" />
</Cursor>
</File>
<File name=".gitignore" open="1" top="0" tabpos="9" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
<File name="text.h" open="1" top="0" tabpos="8" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="214" topLine="0" />
</Cursor>
</File>
<File name="text.cpp" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="79" topLine="6" />
</Cursor>
</File>
<File name="main.cpp" open="1" top="1" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="55" topLine="0" /> <Cursor1 position="1822" topLine="60" />
</Cursor> </Cursor>
</File> </File>
</CodeBlocks_layout_file> </CodeBlocks_layout_file>

@ -1,8 +1,11 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <sstream>
#include <string>
#include "histogram.h" #include "histogram.h"
#include "text.h"
#include "svg.h" #include "svg.h"
#include "text.h"
#include <curl/curl.h>
using namespace std; using namespace std;
@ -12,27 +15,82 @@ struct Input
size_t bin_count{}; size_t bin_count{};
}; };
Input input_data(istream& ine) Input input_data(istream& in, bool prompt = true) {
{ Input data;
size_t number_count; size_t number_count;
cerr << "Number count: ";
ine >> number_count;
Input in;
in.numbers.resize(number_count);
cerr << "Numbers: "; if (prompt) {
cerr << "Enter number count: ";
}
in >> number_count;
data.numbers.resize(number_count);
if (prompt) {
cerr << "Enter numbers: ";
}
for (size_t i = 0; i < number_count; i++) { for (size_t i = 0; i < number_count; i++) {
ine >> in.numbers[i]; in >> data.numbers[i];
} }
cerr << "Enter bin count:";
ine >> in.bin_count; if (prompt) {
return in; cerr << "Enter number of bins: ";
}
in >> data.bin_count;
return data;
} }
int main() 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;
}
Input download(const string& address) {
stringstream buffer;
CURL* curl = curl_easy_init();
if (!curl) {
cerr << "Error: Failed to invent cURL" << endl;
exit(1);
}
curl_easy_setopt(curl, CURLOPT_URL, address.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
cerr << "Loading error: " << curl_easy_strerror(res) << endl;
exit(1);
}
// Ïîëó÷åíèå âðåìåíè name lookup
double name_lookup_time;
curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME, &name_lookup_time);
cerr << "Name lookup time: " << name_lookup_time << " seconds" << endl;
curl_easy_cleanup(curl);
return input_data(buffer, false);
}
int main(int argc, char* argv[]) {
curl_global_init(CURL_GLOBAL_ALL);
Input input;
if (argc > 1) {
input = download(argv[1]);
}
else {
input = input_data(cin, true);
}
size_t max_count; size_t max_count;
auto in = input_data(cin); auto bins = make_histogram(input.numbers, input.bin_count);
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_svg(bins); show_histogram_svg(bins);
curl_global_cleanup();
return 0;
} }

@ -1,6 +1,7 @@
#include "svg.h" #include "svg.h"
#include <string> #include <iostream>
#include <cstddef> #include <vector>
using namespace std; using namespace std;
void svg_begin(double width, double height) { void svg_begin(double width, double height) {
@ -20,10 +21,8 @@ void svg_text(double left, double baseline, string text) {
cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>"; cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>";
} }
void svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black") void 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) { void show_histogram_svg(const vector<size_t>& bins) {
@ -34,22 +33,28 @@ void show_histogram_svg(const vector<size_t>& bins) {
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 MAX_BIN_WIDTH = IMAGE_WIDTH - 2.3 * TEXT_WIDTH;
auto max_bins = bins[0];
size_t sum_bins = 0;
svg_begin(400, 300); for (size_t bin : bins) sum_bins += bin;
double top = 0;
for (size_t bin : bins) {
if (bin > max_bins) max_bins = bin;
}
auto koef = MAX_BIN_WIDTH / BLOCK_WIDTH / max_bins;
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
double top = 0;
for (size_t bin : bins) { for (size_t bin : bins) {
const double bin_width = BLOCK_WIDTH * bin; const double bin_width = BLOCK_WIDTH * bin * koef;
if(bin_width> IMAGE_WIDTH) {
const double bin_width= IMAGE_WIDTH - TEXT_WIDTH; size_t percent = bin * 100 / sum_bins;
}
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "blue", "green"); svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
svg_text((IMAGE_WIDTH - TEXT_WIDTH), top + TEXT_BASELINE, to_string(percent));
top += BIN_HEIGHT; top += BIN_HEIGHT;
} }
//svg_text(20, 20, to_string(bins[0]));
//svg_rect(50, 0, bins[0] * 10, 30);
svg_end(); svg_end();
} }

@ -2,6 +2,7 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <cstddef> #include <cstddef>
using namespace std; using namespace std;
void show_histogram_text(const vector <size_t>& bins,size_t bin_count, size_t max_count) void show_histogram_text(const vector <size_t>& bins,size_t bin_count, size_t max_count)

@ -7,14 +7,14 @@
<Cursor1 position="250" topLine="0" /> <Cursor1 position="250" topLine="0" />
</Cursor> </Cursor>
</File> </File>
<File name="histogram.cpp" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <File name="histogram.cpp" open="0" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="205" topLine="0" /> <Cursor1 position="223" topLine="0" />
</Cursor> </Cursor>
</File> </File>
<File name="unittest.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <File name="unittest.cpp" open="1" top="1" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="623" topLine="6" /> <Cursor1 position="372" topLine="0" />
</Cursor> </Cursor>
</File> </File>
</CodeBlocks_layout_file> </CodeBlocks_layout_file>

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