Сравнить коммиты

..

22 Коммитов

Автор SHA1 Сообщение Дата
e6cf551e5c Merge branch 'master' of http://uit.mpei.ru/git/SpesivtsevYM/cs-lab33 2025-05-25 21:36:28 +03:00
670a9f51be конец 2025-05-25 21:27:11 +03:00
85854badfe окончательный код 2025-05-25 21:24:10 +03:00
c3dfd48d9c работа с буфером, загруженным по сети 2025-05-25 21:10:46 +03:00
dce3a5e3ea добавлена обработка ошибок 2025-05-25 19:08:32 +03:00
09bdc941a0 работа с curl_easy_init 2025-05-25 18:56:38 +03:00
f3ae62da78 статическая сборка программы и откат всех изменений с помощью Git 2025-05-25 18:41:52 +03:00
96f7324eb0 подключение к cURL к проекту и проверка запуска - успешно 2025-05-25 17:29:59 +03:00
75383e1fd3 добавление обработки строки 2025-05-25 17:11:41 +03:00
f2c4302ba4 добавлен prompt 2025-05-25 16:39:24 +03:00
677c96e4eb добавлен поток 2025-05-25 16:36:43 +03:00
60b185ed4e исходный main 2025-05-25 16:34:06 +03:00
d04adf230e argc 2025-05-18 22:00:53 +03:00
68ebbd1eba Add input_data() 2025-05-13 22:19:01 +03:00
0aee2ca9c5 Copy libcurl-x64.dll 2025-05-12 14:28:06 +03:00
e03167e6b7 Add input_data() new parameter: bool prompt 2025-05-11 21:48:36 +03:00
b78aaf0024 Refactoring the input_data() function 2025-05-11 21:41:22 +03:00
5311fee313 Изменил(а) на 'Histogram/histogram.cpp' 2025-04-28 11:43:40 +00:00
451f768394 working with show_histogram_svg 2025-04-28 02:04:29 +03:00
e8f60325e9 Add svg.cpp 2025-04-28 01:57:48 +03:00
aa5eb0f096 Create files for SVG and add svg.h 2025-04-28 01:57:26 +03:00
95a128b320 small fixed main.cpp 2025-04-28 01:53:23 +03:00
15 изменённых файлов: 301 добавлений и 111 удалений

1
Histogram/.gitignore поставляемый
Просмотреть файл

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

Просмотреть файл

@@ -13,7 +13,13 @@
<Option compiler="gcc" /> <Option compiler="gcc" />
<Compiler> <Compiler>
<Add option="-g" /> <Add option="-g" />
<Add option="-DCURL_STATICLIB" />
<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/Histogram" prefix_auto="1" extension_auto="1" /> <Option output="bin/Release/Histogram" prefix_auto="1" extension_auto="1" />
@@ -22,20 +28,32 @@
<Option compiler="gcc" /> <Option compiler="gcc" />
<Compiler> <Compiler>
<Add option="-O2" /> <Add option="-O2" />
<Add option="-DCURL_STATICLIB" />
<Add directory="curl/include" />
</Compiler> </Compiler>
<Linker> <Linker>
<Add option="-s" /> <Add option="-s" />
<Add library="libcurl.dll.a" />
<Add directory="curl/lib" />
</Linker> </Linker>
</Target> </Target>
</Build> </Build>
<Compiler> <Compiler>
<Add option="-Wall" /> <Add option="-Wall" />
<Add option="-fexceptions" /> <Add option="-fexceptions" />
<Add option="-DCURL_STATICLIB" />
<Add directory="curl/include" />
</Compiler> </Compiler>
<Linker>
<Add library="libcurl.dll.a" />
<Add directory="curl/lib" />
</Linker>
<Unit filename="histogram.cpp" /> <Unit filename="histogram.cpp" />
<Unit filename="histogram.h" /> <Unit filename="histogram.h" />
<Unit filename="histogram_internal.h" /> <Unit filename="histogram_internal.h" />
<Unit filename="main.cpp" /> <Unit filename="main.cpp" />
<Unit filename="svg.cpp" />
<Unit filename="svg.h" />
<Unit filename="text.cpp" /> <Unit filename="text.cpp" />
<Unit filename="text.h" /> <Unit filename="text.h" />
<Extensions> <Extensions>

91
Histogram/Histogram.depend Обычный файл
Просмотреть файл

@@ -0,0 +1,91 @@
# depslib dependency file v1.0
1748183279 source:c:\users\yaros_tm2sc6p\Ðàáî÷èé ñòîë\cs-lab34\histogram\main.cpp
<iostream>
<vector>
"histogram.h"
"text.h"
<string>
<sstream>
<curl/curl.h>
1745795032 c:\users\yaros_tm2sc6p\Ðàáî÷èé ñòîë\cs-lab34\histogram\histogram.h
<vector>
1745791758 c:\users\yaros_tm2sc6p\Ðàáî÷èé ñòîë\cs-lab34\histogram\text.h
<vector>
1747164718 source:c:\users\yaros_tm2sc6p\Ðàáî÷èé ñòîë\cs-lab34\histogram\svg.cpp
"svg.h"
<iostream>
<string>
<vector>
1745841059 c:\users\yaros_tm2sc6p\Ðàáî÷èé ñòîë\cs-lab34\histogram\svg.h
<iostream>
<string>
<vector>
1746443355 source:c:\users\yaros_tm2sc6p\Ðàáî÷èé ñòîë\cs-lab34\histogram\text.cpp
"text.h"
<iostream>
<vector>
1745841101 source:c:\users\yaros_tm2sc6p\Ðàáî÷èé ñòîë\cs-lab34\histogram\histogram.cpp
"histogram.h"
<vector>
<string>
"svg.h"
1747048790 c:\users\yaros_tm2sc6p\Ðàáî÷èé ñòîë\cs-lab34\histogram\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"
1747048790 c:\users\yaros_tm2sc6p\Ðàáî÷èé ñòîë\cs-lab34\histogram\curl\include\curl\curlver.h
1747048790 c:\users\yaros_tm2sc6p\Ðàáî÷èé ñòîë\cs-lab34\histogram\curl\include\curl\system.h
<ConditionalMacros.h>
<inttypes.h>
<inttypes.h>
<sys/types.h>
<sys/socket.h>
<sys/poll.h>
1747048790 c:\users\yaros_tm2sc6p\Ðàáî÷èé ñòîë\cs-lab34\histogram\curl\include\curl\easy.h
1747048790 c:\users\yaros_tm2sc6p\Ðàáî÷èé ñòîë\cs-lab34\histogram\curl\include\curl\multi.h
"curl.h"
1747048791 c:\users\yaros_tm2sc6p\Ðàáî÷èé ñòîë\cs-lab34\histogram\curl\include\curl\urlapi.h
"curl.h"
1747048790 c:\users\yaros_tm2sc6p\Ðàáî÷èé ñòîë\cs-lab34\histogram\curl\include\curl\options.h
1747048790 c:\users\yaros_tm2sc6p\Ðàáî÷èé ñòîë\cs-lab34\histogram\curl\include\curl\header.h
1747048791 c:\users\yaros_tm2sc6p\Ðàáî÷èé ñòîë\cs-lab34\histogram\curl\include\curl\websockets.h
1747048790 c:\users\yaros_tm2sc6p\Ðàáî÷èé ñòîë\cs-lab34\histogram\curl\include\curl\mprintf.h
<stdarg.h>
<stdio.h>
"curl.h"
1747048791 c:\users\yaros_tm2sc6p\Ðàáî÷èé ñòîë\cs-lab34\histogram\curl\include\curl\typecheck-gcc.h

Просмотреть файл

@@ -2,34 +2,44 @@
<CodeBlocks_layout_file> <CodeBlocks_layout_file>
<FileVersion major="1" minor="0" /> <FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" /> <ActiveTarget name="Debug" />
<File name="text.cpp" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <File name="text.h" open="0" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="339" topLine="0" />
</Cursor>
</File>
<File name="histogram.cpp" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="843" topLine="0" />
</Cursor>
</File>
<File name="histogram_internal.h" open="1" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="116" topLine="0" />
</Cursor>
</File>
<File name="text.h" open="1" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="93" topLine="0" /> <Cursor1 position="93" topLine="0" />
</Cursor> </Cursor>
</File> </File>
<File name="histogram.h" open="1" top="0" tabpos="2" 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="205" topLine="0" /> <Cursor1 position="0" topLine="17" />
</Cursor> </Cursor>
</File> </File>
<File name="main.cpp" open="1" top="1" tabpos="1" 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="297" topLine="9" /> <Cursor1 position="931" topLine="12" />
</Cursor>
</File>
<File name="text.cpp" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
<File name="main.cpp" open="1" top="1" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="1632" topLine="55" />
</Cursor>
</File>
<File name="svg.h" open="0" top="0" tabpos="7" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="323" topLine="0" />
</Cursor>
</File>
<File name="histogram_internal.h" open="0" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="116" topLine="0" />
</Cursor>
</File>
<File name="histogram.h" open="0" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="33" topLine="0" />
</Cursor> </Cursor>
</File> </File>
</CodeBlocks_layout_file> </CodeBlocks_layout_file>

Двоичные данные
Histogram/bin/Debug/Histogram.exe

Двоичный файл не отображается.

Просмотреть файл

@@ -1,6 +1,7 @@
#include "histogram.h" #include "histogram.h"
#include <vector> #include <vector>
#include <algorithm> #include <string>
#include "svg.h"
size_t find_minmax(const std::vector<double>& numbers, double& min, double& max) { size_t find_minmax(const std::vector<double>& numbers, double& min, double& max) {
if (numbers.empty()) { if (numbers.empty()) {
@@ -27,16 +28,44 @@ std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bi
double min, max; double min, max;
find_minmax(numbers, min, max); find_minmax(numbers, min, max);
if (max == min) { if (max == min) {
return bins; // Âîçâðàùàåì ïóñòûå êîðçèíû, findòàê êàê ãèñòîãðàììà íå ìîæåò áûòü ïîñòðîåíà
return bins;
return bins;
} }
double bin_width = (max - min) / bin_count; double bin_width = (max - min) / bin_count;
for (double number : numbers) { for (double number : numbers) {
size_t bin_index = static_cast<size_t>((number - min) / bin_width); size_t bin_index = static_cast<size_t>((number - min) / bin_width);
if (bin_index >= bin_count) { if (bin_index >= bin_count) {
bin_index = bin_count - 1; // Îáðàáîòêà êðàéíåãî ñëó÷àÿ max == number
bin_index = bin_count - 1;
bin_index = bin_count - 1;
} }
bins[bin_index]++; bins[bin_index]++;
} }
return bins; return bins;
}; };
void show_histogram_svg(const std::vector<size_t>& bins) {
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 BLOCK_WIDTH = 10;
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
for (size_t i = 0; i < bins.size(); ++i) {
const double bin_width = BLOCK_WIDTH * bins[i];
const double top = i * BIN_HEIGHT;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bins[i]));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", "#aaffaa");
}
svg_end();
}

Просмотреть файл

@@ -2,3 +2,4 @@
#include <vector> #include <vector>
size_t find_minmax(const std::vector<double>& numbers, double& min, double& max); size_t 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> make_histogram(const std::vector<double>& numbers, size_t bin_count);
void show_histogram_svg(const std::vector<size_t>& bins);

Просмотреть файл

@@ -1,109 +1,82 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include "histogram.h"
#include "text.h"
#include "svg.h"
#include <curl/curl.h>
#include <sstream>
#include <string>
struct Input{ struct Input{
std::vector<double> numbers; std::vector<double> numbers;
size_t bin_count{}; size_t bin_count{};
}; };
Input input_data() { Input input_data(std::istream& in, bool prompt) {
size_t number_count; size_t number_count;
if (prompt){
std::cerr << "Enter number count: "; std::cerr << "Enter number count: ";
std::cin >> number_count; }
in >> number_count;
if (prompt){
std::cerr << "Enter numbers: \n"; std::cerr << "Enter numbers: \n";
Input in;
in.numbers.resize(number_count);
for (size_t i = 0; i < number_count; ++i) {
std::cin >> in.numbers[i];
} }
std::cerr << "Enter bucket: "; Input result;
std::cin >> in.bin_count; result.numbers.resize(number_count);
for (size_t i = 0; i < number_count; ++i) {
in >> result.numbers[i];
}
return in; if (prompt){
std::cerr << "Enter bucket: ";
}
in >> result.bin_count;
return result;
}; };
size_t write_data(void*items,size_t item_size, size_t item_count, void* ctx){
size_t data_size = item_size * item_count;
std::stringstream* buffer = static_cast<std::stringstream*>(ctx);
(*buffer).write(reinterpret_cast<const char*>(items), data_size);
return data_size;
}
Input download(const std::string&address){
std::stringstream buffer;
CURL* curl = curl_easy_init();
if(curl) {
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, address.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){
std::cerr << curl_easy_strerror(res);
curl_easy_cleanup(curl);
exit(1);
}
curl_easy_cleanup(curl);
}
return input_data(buffer, false);
}
using namespace std; using namespace std;
int main() int main(int argc, char* argv[]) {
{ Input input;
const size_t SCREEN_WIDTH = 80; if (argc > 1) {
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; input = download(argv[1]);
} else {
input = input_data(cin, true);
int number_count, bucket;
cerr << "Enter number count: "; cin >> number_count;
std::cerr << "Enter numbers: \n";
vector <double> numbers(number_count);
for (int i = 0; i < number_count; i++) cin >> numbers[i];
float min = numbers[0];
float max = numbers[0];
for (float x : numbers)
{
if (x < min) min = x;
else if (x > max) max = x;
}
if (max == min) {// ïðîâåðêà íà àíîìàëèþ
std::cout << " Unable to create a histogram." << std::endl;
return 1;
}
cerr << "Enter bucket: "; cin >> bucket;
float k = (max - min) / bucket;
if (numbers.empty()) {//áûëè ëè ââåäåíû ÷èñëà
std::cout << "Empty. unable to create histogram." << std::endl;
return 1;
}
vector <int> stolb(bucket);
vector <int> proz(stolb);
for (int j = 0; j < bucket; j++) stolb[j] = 0;
for (int i = 0; i < number_count; i++)
{
bool flag = false;
for (int j = 0; (j < bucket && !flag); j++)
{
if (numbers[i] >= (min + k * j) && numbers[i] < (min + k * (1 + j)))
{
stolb[j]++;
flag = true;
}
}
if (!flag) stolb[bucket - 1]++;
}
int maxlen = 0;
for (int j = 0; j < bucket; j++)
{
if (maxlen < stolb[j]) maxlen = stolb[j];
}
for (int j = 0; j < bucket; j++)
{
if (stolb[j] < 100) cout << " ";
if (stolb[j] < 10) cout << "";
cout << stolb[j] << " |";
size_t height = stolb[j];
if (maxlen > MAX_ASTERISK)
{
if (maxlen != stolb[j]) height = MAX_ASTERISK * (static_cast <float>(stolb[j]) / maxlen);
else if (maxlen == stolb[j]) height = MAX_ASTERISK;
}
for (int i = 0; i < height; i++) cout << "*";
cout << "\n";
} }
auto bins = make_histogram(input.numbers, input.bin_count);
show_histogram_svg(bins);
return 0; return 0;
} }

Двоичные данные
Histogram/obj/Debug/main.o

Двоичный файл не отображается.

46
Histogram/svg.cpp Обычный файл
Просмотреть файл

@@ -0,0 +1,46 @@
#include "svg.h"
#include <iostream>
#include <string>
#include <vector>
void svg_begin(double width, double height) {
std::cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
std::cout << "<svg ";
std::cout << "width='" << width << "' ";
std::cout << "height='" << height << "' ";
std::cout << "viewBox='0 0 " << width << " " << height << "' ";
std::cout << "xmlns='http://www.w3.org/2000/svg'>\n";
}
void svg_end() {
std::cout << "</svg>\n";
}
void svg_rect(double x, double y, double width, double height, std::string stroke, std::string fill) {
std::cout << "<rect x='" << x<< "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << "' />\n";
}
void svg_text(double left, double baseline, std::string text) {
std::cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>\n";
}
void show_histogram_svg(const std::vector<size_t>& bins) {
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 BLOCK_WIDTH = 10;
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
for (size_t i = 0; i < bins.size(); ++i) {
const double bin_width = BLOCK_WIDTH * bins[i];
const double top = i * BIN_HEIGHT;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bins[i]));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", "#aaffaa");
}
svg_end();
}

10
Histogram/svg.h Обычный файл
Просмотреть файл

@@ -0,0 +1,10 @@
#pragma once
#include <iostream>
#include <string>
#include <vector>
void svg_begin(double width, double height);
void svg_end();
void svg_rect(double x, double y, double width, double height, std::string stroke = "black", std::string fill = "black");
void svg_text(double left, double baseline, std::string text);
void show_histogram_svg(const std::vector<size_t>& bins);

Двоичные данные
Histogram/svg.o Обычный файл

Двоичный файл не отображается.

Просмотреть файл

@@ -24,4 +24,7 @@ void show_histogram_text(const std::vector<size_t>& bins) {
} }
std::cout << "\n"; std::cout << "\n";
} }
}
}; };

Просмотреть файл

@@ -31,6 +31,9 @@
<Compiler> <Compiler>
<Add option="-Wall" /> <Add option="-Wall" />
</Compiler> </Compiler>
<Unit filename="Histogram/histogram.cpp" />
<Unit filename="Histogram/histogram_internal.h" />
<Unit filename="unittest.cpp" />
<Extensions> <Extensions>
<lib_finder disable_auto="1" /> <lib_finder disable_auto="1" />
</Extensions> </Extensions>

Просмотреть файл

@@ -2,4 +2,9 @@
<CodeBlocks_layout_file> <CodeBlocks_layout_file>
<FileVersion major="1" minor="0" /> <FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" /> <ActiveTarget name="Debug" />
<File name="unittest.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="271" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file> </CodeBlocks_layout_file>