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

...

16 Коммитов

Автор SHA1 Сообщение Дата
7449ef860f code: скорость загрузки по сети, вариант 2024-05-18 21:25:15 +03:00
1b1311833a code: Работа с буфером, загруженным по сети 2024-05-18 21:02:56 +03:00
5135699b21 code: Обработка ошибок 2024-05-18 16:31:08 +03:00
2eaf794295 code: вывод файла из сети на стандартный вывод 2024-05-18 16:17:06 +03:00
bcc61042de code: Получение аргументов программы 2024-05-18 12:34:02 +03:00
d5a9d78924 code: Загрузка данных по сети 2024-05-18 11:47:04 +03:00
7bd4bee170 code: добавлен ввод из произвольного потока 2024-05-18 11:07:25 +03:00
c37b1fed88 doc: добавление модульных тестов к индивидуальной работе 2024-04-20 20:59:54 +03:00
ab7728268d code: ВАР4, цветовая гистограмма 2024-04-20 20:26:27 +03:00
15fb696925 code: вывод гистограммы в формате SVG + хеддер-файлы проекта 2024-04-20 19:24:45 +03:00
74edc12b97 code: функция отладки SVG формата 2024-04-20 14:56:45 +03:00
6f1662469b doc: модульные тесты 2024-04-20 00:48:13 +03:00
f58f512bbf doc: добавление модульных тестов 2024-04-20 00:46:48 +03:00
fc6edeaf09 doc: добавление новой библиотеки 2024-04-20 00:46:48 +03:00
1b19c08c12 doc: создание проекта для тестов 2024-04-20 00:46:48 +03:00
d8f414ff60 code: разделение программы на файл 2024-04-20 01:32:26 +05:00
16 изменённых файлов: 7539 добавлений и 112 удалений

3
.gitignore поставляемый Обычный файл
Просмотреть файл

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

7106
doctest.h Обычный файл

Разница между файлами не показана из-за своего большого размера Загрузить разницу

19
hist_proc.cpp Обычный файл
Просмотреть файл

@@ -0,0 +1,19 @@
#include <iostream>
#include <vector>
#include "hist_proc.h"
#include "hist_proc_internal.h"
using namespace std;
vector<size_t> make_histogram_proc(const vector<double> numbers, size_t bin_count, vector<size_t> bins){
vector<size_t> procent(bin_count);
double sum = 0;
for (size_t i = 0; i < bin_count; i++){
sum = sum + bins[i];
}
double ost = 100;
for (size_t i = 0; i< bin_count-1; i++){
procent[i]=(bins[i]/sum)*100;
ost = ost - procent[i];
}
procent[bin_count-1]=ost;
return procent;
}

7
hist_proc.h Обычный файл
Просмотреть файл

@@ -0,0 +1,7 @@
#ifndef HIST_PROC_H_INCLUDED
#define HIST_PROC_H_INCLUDED
std::vector<size_t>
make_histogram_proc(const std::vector<double> numbers, size_t bin_count, std::vector<size_t> bins);
#endif // HIST_PROC_H_INCLUDED

7
hist_proc_internal.h Обычный файл
Просмотреть файл

@@ -0,0 +1,7 @@
#ifndef HIST_PROC_INTERNAL_H_INCLUDED
#define HIST_PROC_INTERNAL_H_INCLUDED
std::vector<size_t>
make_histogram_proc(const std::vector<double> numbers, size_t bin_count, std::vector<size_t> bins);
#endif // HIST_PROC_INTERNAL_H_INCLUDED

52
histogram.cpp Обычный файл
Просмотреть файл

@@ -0,0 +1,52 @@
#include <vector>
#include <iostream>
#include "histogram.h"
using namespace std;
bool
find_minmax(const vector<double>& numbers, double& min, double& max) {
bool flag;
if (numbers.size() == 0){
flag=false;
}
else {
min = numbers[0];
max = numbers[0];
for (double x : numbers) {
if (x < min) {
min = x;
}
else if (x > max) {
max = x;
}
}
}
return flag;
}
vector<size_t> make_histogram(const vector<double> numbers, size_t bin_count){
vector<size_t> bins(bin_count);
double max, min = 0;
find_minmax(numbers, min, max);
double bin_size = (max - min) / 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++) {
auto lo = min + j * bin_size;
auto hi = min + (j + 1) * bin_size;
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
bins[j]++;
found = true;
}
}
if (!found) {
bins[bin_count - 1]++;
}
}
return bins;
}

9
histogram.h Обычный файл
Просмотреть файл

@@ -0,0 +1,9 @@
#ifndef HISTOGRAM_H_INCLUDED
#define HISTOGRAM_H_INCLUDED
#include <iostream>
#include <vector>
std::vector<size_t>
make_histogram(const std::vector<double> numbers, size_t bin_count);
#endif // HISTOGRAM_H_INCLUDED

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

@@ -13,7 +13,11 @@
<Option compiler="gcc" /> <Option compiler="gcc" />
<Compiler> <Compiler>
<Add option="-g" /> <Add option="-g" />
<Add directory="curl/include" />
</Compiler> </Compiler>
<Linker>
<Add library="curl/include" />
</Linker>
</Target> </Target>
<Target title="Release"> <Target title="Release">
<Option output="bin/Release/lab003" prefix_auto="1" extension_auto="1" /> <Option output="bin/Release/lab003" prefix_auto="1" extension_auto="1" />
@@ -31,8 +35,23 @@
<Compiler> <Compiler>
<Add option="-Wall" /> <Add option="-Wall" />
<Add option="-fexceptions" /> <Add option="-fexceptions" />
<Add directory="curl/include" />
</Compiler> </Compiler>
<Linker>
<Add directory="curl/include" />
</Linker>
<Unit filename="hist_proc.cpp" />
<Unit filename="hist_proc.h" />
<Unit filename="histogram.cpp" />
<Unit filename="histogram.h" />
<Unit filename="histogram_internal.h" />
<Unit filename="main.cpp" /> <Unit filename="main.cpp" />
<Unit filename="svg.cpp" />
<Unit filename="svg.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="text.cpp" />
<Unit filename="text.h" />
<Extensions> <Extensions>
<lib_finder disable_auto="1" /> <lib_finder disable_auto="1" />
</Extensions> </Extensions>

190
main.cpp
Просмотреть файл

@@ -1,8 +1,14 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <string>
#include <hist_proc.h>
#include "histogram.h"
#include <text.h>
#include <svg.h>
#include <curl/curl.h>
#include <sstream>
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
using namespace std; using namespace std;
@@ -14,131 +20,97 @@ struct Input {
}; };
Input Input
input_data() { input_data(istream& in, bool prompt) {
if (prompt == true){
size_t number_count; size_t number_count;
cin >> number_count; cerr << "input number count";
Input in; in >> number_count;
in.numbers.resize(number_count); Input inn;
inn.numbers.resize(number_count);
vector<double> numbers(number_count); vector<double> numbers(number_count);
cerr << "input numbers";
for (size_t i = 0; i < number_count; i++) { for (size_t i = 0; i < number_count; i++) {
cin >> in.numbers[i]; in >> inn.numbers[i];
} }
size_t bin_count; size_t bin_count;
cin >> in.bin_count; cerr << "input amount of bins";
return in; in >> inn.bin_count;
return inn;
} }
void
find_minmax(const vector<double>& numbers, double& min, double& max) {
min = numbers[0];
for (double x : numbers) {
if (x < min) {
min = x;
}
else if (x > max) {
max = x;
}
}
}
vector<size_t> make_histogram(const vector<double> numbers, size_t bin_count){
vector<size_t> bins(bin_count);
double max, min = 0;
find_minmax(numbers, min, max);
double bin_size = (max - min) / 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++) {
auto lo = min + j * bin_size;
auto hi = min + (j + 1) * bin_size;
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
bins[j]++;
found = true;
}
}
if (!found) {
bins[bin_count - 1]++;
}
}
return bins;
}
vector<size_t> make_histogram_proc(const vector<double> numbers, size_t bin_count, vector<size_t> bins){
vector<size_t> procent(bin_count);
double sum = 0;
for (size_t i = 0; i < bin_count; i++){
sum = sum + bins[i];
}
double ost = 100;
for (size_t i = 0; i< bin_count-1; i++){
procent[i]=(bins[i]/sum)*100;
ost = ost - procent[i];
}
procent[bin_count-1]=ost;
return procent;
}
void show_histogram_text(vector<size_t> bins, size_t bin_count, vector<size_t> procent){
size_t max_count = 0;
for (size_t x: bins) {
if (x > max_count) {
max_count = x;
}
}
if (max_count > MAX_ASTERISK) {
for (size_t i = 0; i < bin_count ; i++) {
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
if (bins[i] < 10) {
cout << " " << procent[i] << "%|";
}
else if (bins[i] >= 100) {
cout << procent[i] << "%|";
}
else { else {
cout << " " << procent[i] << "%|"; size_t number_count;
cout << "input number count";
in >> number_count;
Input inn;
inn.numbers.resize(number_count);
vector<double> numbers(number_count);
cout << "input numbers";
for (size_t i = 0; i < number_count; i++) {
in >> inn.numbers[i];
} }
for (size_t i = 0; i < height; i++) { size_t bin_count;
cout << "*"; cout << "input amount of bins";
} in >> inn.bin_count;
cout << "\n"; return inn;
} }
} }
else { static size_t
for (size_t i = 0; i < bin_count; i++) { write_data(void* items, size_t item_size, size_t item_count, void* ctx) {
size_t data_size = item_size * item_count;
if (procent[i] < 10) { stringstream* buffer = reinterpret_cast<stringstream*>(ctx);
cout << " " << procent[i] << "%|"; buffer->write(reinterpret_cast<const char*>(items), data_size);
} return data_size;
else if (procent[i] < 100){
cout << " " << procent[i] << "%|";
}
else if (procent[i] = 100){
cout << procent[i] << "%|";
} }
for (size_t r = 0; r < bins[i]; r++) Input
cout << "*"; download(const string& address, bool proverka_skorosti) {
cout << "\n"; stringstream buffer;
} CURL* curl = curl_easy_init();
if(curl)
}
}
int main()
{ {
auto in = input_data(); CURLcode res;
const char* res2;
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) {
curl_off_t speed;
res = curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD_T, &speed);
if(!res) {
cerr << "Upload speed "<< speed << " bytes/sec\n";
}
}
curl_easy_cleanup(curl);
}
curl_global_init(CURL_GLOBAL_ALL);
return input_data(buffer, true);
}
int main(int argc, char* argv[])
{
Input in;
CURL *curl = curl_easy_init();
if (argc > 1) {
in = download(argv[1], false);
} else {
in = input_data(cin, true);
}
auto bins = make_histogram(in.numbers, in.bin_count); auto bins = make_histogram(in.numbers, in.bin_count);
auto procent = make_histogram_proc(in.numbers, in.bin_count, bins); auto procent = make_histogram_proc(in.numbers, in.bin_count, bins);
show_histogram_text(bins, in.bin_count, procent); show_histogram_svg(bins, procent);
return 0; return 0;
} }

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

@@ -0,0 +1,88 @@
#include <iostream>
#include <vector>
#include <string>
#include <svg.h>
using namespace std;
void
svg_begin(double width, double height) {
cout << "<?xml version='1.0' encoding='UTF-8'?>\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() {
cout << "</svg>\n";
}
void
svg_text(double left, double baseline, string text) {
cout << "<text x='"<< left << "' y='"<< baseline <<"' > "<< text <<" </text>";
}
void
svg_rect(double x, double y, double width, double height){
cout << "<rect x='"<<x<<"' y='"<<y<<"' width='"<<width<<"' height='"<<height<<"' stroke ='cyan' fill='#fce166' />\n";
}
void
svg_procent(double left_procent, double baseline, string text) {
cout << "<text x='"<< left_procent << "' y='"<< baseline << "'> "<< text <<"% </text>";
}
void
show_histogram_svg(const vector<size_t>& bins, const vector<size_t>& procent) {
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;
const double SCALE = IMAGE_WIDTH - TEXT_WIDTH;
svg_begin(800, 300);
double max_count = 0;
for (double x: bins) {
if (x > max_count) {
max_count = x;
}
}
double top = 0;
if ((max_count*BLOCK_WIDTH)>SCALE){
for (size_t i = 0; i < bins.size(); i++) {
const double bin_width = SCALE * ( bins[i] / max_count);
const auto TEXT_LEFT_PROCENT = SCALE+TEXT_WIDTH+20;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bins[i]));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
svg_procent(TEXT_LEFT_PROCENT, top + TEXT_BASELINE, to_string(procent[i]));
top += BIN_HEIGHT;
cout << endl;
cout << bin_width;
}
}
else{
for (size_t i = 0; i < bins.size(); i++) {
const double bin_width = BLOCK_WIDTH * bins[i];
const auto TEXT_LEFT_PROCENT = (max_count * BLOCK_WIDTH)+TEXT_WIDTH+20;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bins[i]));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
svg_procent(TEXT_LEFT_PROCENT, top + TEXT_BASELINE, to_string(procent[i]));
top += BIN_HEIGHT;
cout << endl;
cout << bin_width;
}
}
svg_end();
}

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

@@ -0,0 +1,7 @@
#ifndef SVG_H_INCLUDED
#define SVG_H_INCLUDED
void
show_histogram_svg(const std::vector<size_t>& bins, const std::vector<size_t>& procent);
#endif // SVG_H_INCLUDED

60
text.cpp Обычный файл
Просмотреть файл

@@ -0,0 +1,60 @@
#include <iostream>
#include <vector>
#include <text.h>
using namespace std;
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
void
show_histogram(size_t bin_count, std::vector<size_t> bins, std::vector<size_t> procent){
size_t max_count = 0;
for (size_t x: bins) {
if (x > max_count) {
max_count = x;
}
}
if (max_count > MAX_ASTERISK) {
for (size_t i = 0; i < bin_count ; i++) {
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
if (bins[i] < 10) {
cout << " " << procent[i] << "%|";
}
else if (bins[i] >= 100) {
cout << procent[i] << "%|";
}
else {
cout << " " << procent[i] << "%|";
}
for (size_t i = 0; i < height; i++) {
cout << "*";
}
cout << "\n";
}
}
else {
for (size_t i = 0; i < bin_count; i++) {
if (procent[i] < 10) {
cout << " " << procent[i] << "%|";
}
else if (procent[i] < 100){
cout << " " << procent[i] << "%|";
}
else if (procent[i] = 100){
cout << procent[i] << "%|";
}
for (size_t r = 0; r < bins[i]; r++)
cout << "*";
cout << "\n";
}
}
}

7
text.h Обычный файл
Просмотреть файл

@@ -0,0 +1,7 @@
#ifndef TEXT_H_INCLUDED
#define TEXT_H_INCLUDED
void
show_histogram(size_t bin_count, std::vector<size_t> bins, std::vector<size_t> procent);
#endif // TEXT_H_INCLUDED

42
unittest.cbp Обычный файл
Просмотреть файл

@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="unittest" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/unittest" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/unittest" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Unit filename="doctest.h" />
<Unit filename="histogram.cpp" />
<Unit filename="histogram_internal.h" />
<Unit filename="unittest.cpp" />
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>

12
unittest4var-2.cpp Обычный файл
Просмотреть файл

@@ -0,0 +1,12 @@
#define DOCTEST_CONFIG_NO_MULTITHREADING
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include "hist_proc_internal.h"
TEST_CASE("distinct positive numbers") {
std::vector<size_t> procent(3);
procent = make_histogram_proc({1}, 3, {1, 0, 0});
CHECK(procent[0] == 100);
CHECK(procent[1] == 0);
CHECK(procent[2] == 0);
}

17
unittest4var.cpp Обычный файл
Просмотреть файл

@@ -0,0 +1,17 @@
#define DOCTEST_CONFIG_NO_MULTITHREADING
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include "hist_proc_internal.h"
TEST_CASE("distinct positive numbers") {
std::vector<size_t> procent(4);
procent = make_histogram_proc({1, 2, 3}, 3, {1, 1, 1});
CHECK(procent[0] == 33);
CHECK(procent[1] == 33);
CHECK(procent[2] == 34);
procent = make_histogram_proc({1}, 3, {1, 0, 0});
CHECK(procent[0] == 100);
CHECK(procent[1] == 0);
CHECK(procent[2] == 0);
}