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

..

18 Коммитов

Автор SHA1 Сообщение Дата
3a0651cc1a code: добавлен код для варианта 16 2024-05-27 10:43:54 +03:00
3db4b095a9 code: сохранение данных из сети в буфер 2024-05-27 10:33:17 +03:00
1c7fc9a454 code: получение значений из git кафедры с обработкой ошибок 2024-05-27 09:58:45 +03:00
c69b174056 lib: подключена библиотека curl 2024-05-27 09:24:34 +03:00
0b72e913b2 code: добавлен параметр prompt для вывода подсказок 2024-05-27 08:02:27 +03:00
191fd262a0 code: добавлен ввод из произвольного потока 2024-05-27 07:50:45 +03:00
5636df9530 code: выполнено задание для варианта №16 2024-05-13 11:23:29 +03:00
6373e22a80 git: добавлены файлы зависимости в .gitignore 2024-05-13 10:29:57 +03:00
3dae9289ff code: выполнено масштабирование для гистограммы формата SVG 2024-05-13 10:25:42 +03:00
8c94d31a34 code: вывод гистограммы в формате SVG 2024-05-13 10:14:38 +03:00
6ef0b93ced code: модульные тесты; улучшены функции расчета и печати гистограммы для случая с пустым вектором 2024-05-13 10:00:19 +03:00
ece8c021cc lib: добавлен doctest 2024-05-13 09:32:21 +03:00
397562b360 build: заготовка для модульных тестов 2024-05-13 09:28:58 +03:00
fce6d4daeb code: вынос печати текстовой гистограммы в отдельный файл 2024-05-13 08:45:13 +03:00
be7b291133 code: вынос расчёта гистограммы в отдельный файл 2024-05-13 08:28:44 +03:00
cd46e043f3 code: добавлены функции расчёта и вывода текстовой гистограммы 2024-05-13 08:17:38 +03:00
07cf1172cd code: добавлена структура для входных данных, функции ввода и поиска минимума/максимума 2024-05-13 07:47:42 +03:00
444b499d7d git: добавлен файл .gitignore 2024-05-13 07:46:26 +03:00
14 изменённых файлов: 7585 добавлений и 71 удалений

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

@@ -0,0 +1,5 @@
bin/
obj/
curl/
Lab1.depend
unittest.depend

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

@@ -30,10 +30,29 @@
</Build> </Build>
<Compiler> <Compiler>
<Add option="-Wall" /> <Add option="-Wall" />
<Add option="-fexceptions" /> <Add option="-fexceptions -lcurl" />
<Add option="-DCURL_STATICLIB" />
<Add option='-DCURL_STATICLIB &quot;curl-config --cflags --static-libs&quot;' />
<Add option="-DCURL_STATICLIB `curl-config --cflags --static-libs`" />
<Add directory="C:/Users/Gamer/GameMaker 8.1/Desktop/учебный кал/lab03/Lab1/curlic/include" />
</Compiler> </Compiler>
<Linker>
<Add option="-static-libstdc++" />
<Add option="-static-libgcc" />
<Add option="-static" />
<Add directory="C:/Users/Gamer/GameMaker 8.1/Desktop/учебный кал/lab03/Lab1/curlic/lib" />
</Linker>
<Unit filename=".gitignore" />
<Unit filename="histogram.cpp" />
<Unit filename="histogram.h" />
<Unit filename="histogram_internal.h" />
<Unit filename="main.cpp" /> <Unit filename="main.cpp" />
<Unit filename="main.h" /> <Unit filename="main.h" />
<Unit filename="svg.cpp" />
<Unit filename="svg.h" />
<Unit filename="svg_internal.h" />
<Unit filename="text.cpp" />
<Unit filename="text.h" />
<Extensions /> <Extensions />
</Project> </Project>
</CodeBlocks_project_file> </CodeBlocks_project_file>

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

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

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

@@ -0,0 +1,85 @@
#include "histogram.h"
using namespace std;
void
find_minmax(const vector<double>& numbers, double& min, double& max, bool& empty_vector)
{
if (numbers.empty())
{
empty_vector = true;
}
else
{
min = numbers[0];
max = numbers[0];
for (double x : numbers)
{
if (x > max )
{
max = x;
}
if (x < min)
{
min = x;
}
}
}
}
vector<size_t>
make_histogram(const vector<double>& numbers, size_t& bin_count)
{
double min, max;
bool empty_vector = false;
find_minmax(numbers, min, max, empty_vector);
double bin_size = (max - min) / bin_count;
vector<size_t> bins(bin_count);
if (empty_vector)
{
for (size_t y : bins)
{
y = 0;
}
}
for (size_t i = 0; i < numbers.size(); i++)
{
for (size_t j = i + 1; j < numbers.size(); j++)
{
if (numbers[i] == numbers[j])
{
//numbers.erase(numbers.begin() + j);
//number_count--;
//i--;
//cout << "Removed number " << numbers[j] << endl;
}
}
}
for (size_t i = 0; i < numbers.size(); i++)
{
bool found = false;
for (size_t j = 0; (j < bin_count - 1) && !found; j++)
{
auto low_bound = min + j * bin_size;
auto high_bound = min + (j + 1) * bin_size;
if ((low_bound <= numbers[i]) && (numbers[i] < high_bound))
{
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 <vector>
std::vector<size_t>
make_histogram(const std::vector<double>& numbers, size_t& bin_count);
#endif // HISTOGRAM_H_INCLUDED

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

@@ -0,0 +1,9 @@
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
#define HISTOGRAM_INTERNAL_H_INCLUDED
#include <vector>
void
find_minmax(const std::vector<double>& numbers, double& min, double& max, bool& empty_vector);
#endif // HISTOGRAM_INTERNAL_H_INCLUDED

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

@@ -1,87 +1,109 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <string>
#include <sstream>
#include "histogram.h"
//#include "text.h"
#include "svg.h"
#include <curl/curl.h>
using namespace std; using namespace std;
int main() struct Input {
{ vector<double> numbers;
const size_t SCREEN_WIDTH = 80; vector<string> colors;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; size_t bin_count{};
};
Input
input_data(istream& in_stream,bool prompt) {
size_t number_count; size_t number_count;
size_t bin_count; Input in;
cerr << "Enter number count: "; if (prompt) cerr << "Enter number count: ";
cin >> number_count; in_stream >> number_count;
vector<double> numbers(number_count); vector<double> numbers(number_count);
for (size_t i = 0; i < number_count; i++) in.numbers.resize(number_count);
cin >> numbers[i];
double min = numbers[0]; for (size_t i = 0; i < number_count; i++) {
double max = numbers[0]; in_stream >> in.numbers[i];
double max_count = 0;
for (double value : numbers)
{
if (value < min) {min = value;}
else if (value > max) {max = value;}
} }
cerr << "Enter bins count: "; if (prompt) cerr << "Enter bins count and colors: ";
cin >> bin_count; in_stream >> in.bin_count;
vector<size_t> bins(bin_count); vector<string> colors(in.bin_count);
in.colors.resize(in.bin_count);
double bin_size = (max - min) / bin_count; for (size_t i = 0; i < in.bin_count; i++) {
//in_stream >> in.colors[i];
for (size_t i = 0; i < number_count; i++) in.colors[i]="red";
{ bool check = check_color(in.colors[i]);
for (size_t j = i + 1; j < number_count; j++) while (!check) {
{ if (prompt) cerr << "Incorrect input. Color doesn't contain # or has spaces." << endl;
if (numbers[i] == numbers[j]) in_stream >> in.colors[i];
{ //in.colors[i]="red";
//numbers.erase(numbers.begin() + j); check = check_color(in.colors[i]);
//number_count--;
//i--;
//cout << "Removed number " << numbers[j] << endl;
} }
} }
return in;
}
size_t write_data(void* items, size_t item_size, size_t item_count, void* ctx) {
size_t data_size = item_size * item_count;
stringstream* buffer = reinterpret_cast<stringstream*>(ctx);
(*buffer).write(reinterpret_cast<const char*>(items), data_size);
return data_size;
}
size_t write_header(char* buffer, size_t size, size_t nitems, void* userdata)
{
return size * nitems;
}
Input download(const string& address) {
curl_global_init(CURL_GLOBAL_ALL);
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);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_header);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, NULL);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res != CURLE_OK) {
cout << curl_easy_strerror(res);
exit(1);
}
else
{
double *ct;
res = curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME, &ct);
if((CURLE_OK == res) && ct)
printf("Time: %f\n",ct);
}
} }
for (size_t i = 0; i < number_count; i++) return input_data(buffer, false);
{
bool found = false;
for (size_t j = 0; (j < bin_count - 1) && !found; j++)
{
auto low_bound = min + j * bin_size;
auto high_bound = min + (j + 1) * bin_size;
if ((low_bound <= numbers[i]) && (numbers[i] < high_bound))
{
bins[j]++;
found = true;
if (bins[j] > max_count)
max_count = bins[j];
}
} }
if (!found) int main(int argc, char* argv[])
{ {
bins[bin_count - 1]++; Input in;
if (bins[bin_count - 1] > max_count) if (argc > 1)
max_count = bins[bin_count - 1];
}
}
for (size_t bin : bins)
{ {
if (bin < 100) cout << " "; in = download(argv[1]);
if (bin < 10) cout << " ";
cout << bin;
cout << "|";
size_t height = MAX_ASTERISK * (static_cast<double>(bin) / max_count);
if (max_count <= MAX_ASTERISK) height = bin;
for (size_t i = 0; i < height; i++) cout << "*";
cout << endl;
} }
else
return 0; {
in = input_data(cin,true);
}
vector<size_t> bins = make_histogram(in.numbers, in.bin_count);
//show_histogram_text(bins, in.bin_count);
show_histogram_svg(bins, in.colors);
} }

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

@@ -0,0 +1,83 @@
#include <iostream>
#include <string>
#include <vector>
#include "svg.h"
using namespace std;
bool
check_color(string color) {
if (color[0]=='#'||color.find(' ')==(-1))
return true;
else
return false;
}
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, string stroke = "black", string fill = "black") {
cout << "<rect x='" << x
<< "' y='" << y
<< "' width='" << width
<< "' height='" << height
<< "' stroke='" << stroke
<< "' fill='" << fill
<< "' />\n";
}
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
show_histogram_svg(const vector<size_t>& bins, const vector<string>& colors) {
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 auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
size_t max_count = 0;
for (size_t x : bins) {
if (x > max_count) {
max_count = x;
}
}
if (max_count == 0) {
max_count = 1;
}
auto scale_factor = static_cast<double>(MAX_WIDTH) / (max_count * BLOCK_WIDTH);
if (scale_factor > 1) {
scale_factor = 1;
}
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
double top = 0;
for (size_t bin : bins) {
const double bin_width = BLOCK_WIDTH * bin;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black",colors[top/BIN_HEIGHT]);
top += BIN_HEIGHT;
}
svg_end();
}

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

@@ -0,0 +1,13 @@
#ifndef SVG_H_INCLUDED
#define SVG_H_INCLUDED
#include <vector>
#include <string>
void
show_histogram_svg(const std::vector<size_t>& bins, const std::vector<std::string>& colors);
bool
check_color(std::string color);
#endif // SVG_H_INCLUDED

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

@@ -0,0 +1,9 @@
#ifndef SVG_INTERNAL_H_INCLUDED
#define SVG_INTERNAL_H_INCLUDED
#include <string>
bool
check_color(std::string color);
#endif // SVG_INTERNAL_H_INCLUDED

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

@@ -0,0 +1,36 @@
#include <iostream>
#include "text.h"
using namespace std;
void
show_histogram_text(const vector<size_t>& bins, size_t& bin_count) {
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
size_t max_count = 0;
for (size_t s = 0; s < bin_count; s++)
{
if (bins[s] > max_count)
{
max_count = bins[s];
}
}
if (max_count == 0)
{
max_count = 1;
}
for (size_t bin : bins)
{
if (bin < 100) cout << " ";
if (bin < 10) cout << " ";
cout << bin;
cout << "|";
size_t height = MAX_ASTERISK * (static_cast<double>(bin) / max_count);
if (max_count <= MAX_ASTERISK) height = bin;
for (size_t i = 0; i < height; i++) cout << "*";
cout << endl;
}
}

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

@@ -0,0 +1,9 @@
#ifndef TEXT_H_INCLUDED
#define TEXT_H_INCLUDED
#include <vector>
void
show_histogram_text(const std::vector<size_t>& bins, size_t& bin_count);
#endif // TEXT_H_INCLUDED

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

@@ -0,0 +1,39 @@
<?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="histogram.cpp" />
<Unit filename="histogram_internal.h" />
<Unit filename="unittest.cpp" />
<Extensions />
</Project>
</CodeBlocks_project_file>

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

@@ -0,0 +1,70 @@
#define DOCTEST_CONFIG_NO_MULTITHREADING
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include "histogram_internal.h"
#include "svg_internal.h"
#include <string>
TEST_CASE("distinct positive numbers") {
double min = 0;
double max = 0;
bool empty_vector = false;
find_minmax({1, 2}, min, max, empty_vector);
CHECK(empty_vector == false);
CHECK(min == 1);
CHECK(max == 2);
}
TEST_CASE("single number") {
double min = 0;
double max = 0;
bool empty_vector = false;
find_minmax({1}, min, max, empty_vector);
CHECK(empty_vector == false);
CHECK(min == 1);
CHECK(max == 1);
}
TEST_CASE("negative numbers") {
double min = 0;
double max = 0;
bool empty_vector = false;
find_minmax({-1, -2}, min, max, empty_vector);
CHECK(empty_vector == false);
CHECK(min == -2);
CHECK(max == -1);
}
TEST_CASE("identical numbers") {
double min = 0;
double max = 0;
bool empty_vector = false;
find_minmax({2, 2}, min, max, empty_vector);
CHECK(empty_vector == false);
CHECK(min == 2);
CHECK(max == 2);
}
TEST_CASE("empty vector") {
double min = 0;
double max = 0;
bool empty_vector = false;
find_minmax({}, min, max, empty_vector);
CHECK(empty_vector == true);
CHECK(min == 0);
CHECK(max == 0);
}
TEST_CASE("correct color") {
bool check = check_color("red");
CHECK(check == true);
check = check_color("#FF FFF");
CHECK(check == true);
}
TEST_CASE("incorrect color") {
bool check = check_color("re d");
CHECK(check == false);
check = check_color("red green blue");
CHECK(check == false);
}