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

...

11 Коммитов

Автор SHA1 Сообщение Дата
MovsisianRG
ae96b6ec94 code: edited for variant 2024-05-21 22:42:06 +03:00
MovsisianRG
dac7bf496d code: correct exmaple work 2024-05-21 22:41:31 +03:00
MovsisianRG
0dac6860b1 code: added error processing 2024-05-21 22:40:08 +03:00
MovsisianRG
ca54d0f95d code: edited argv and argc 2024-05-21 22:39:38 +03:00
MovsisianRG
3488801792 code: added argv and argc 2024-05-21 22:37:18 +03:00
MovsisianRG
1aceeea07c code: added lcurl 2024-05-21 22:30:15 +03:00
MovsisianRG
440d3821db code: new promt 2024-05-21 22:29:49 +03:00
MovsisianRG
a606379143 files: gitignore 2024-05-21 22:27:05 +03:00
MovsisianRG
866f780a1f code: added istream 2024-05-19 17:51:22 +03:00
MovsisianRG
c9f0862835 code: code for var 1 2024-05-19 17:50:27 +03:00
MovsisianRG
bdab39b266 code: all files 2024-05-19 17:49:43 +03:00
14 изменённых файлов: 370 добавлений и 22 удалений

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

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

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

@@ -0,0 +1,55 @@
#include <iostream>
#include <vector>
#include "histogram.h"
void find_minmax(const std::vector<double> &numbers, double &min, double &max)
{
//min = numbers[0];
//max = numbers[0];
for (double x : numbers)
{
if (x < min)
{
min = x;
}
else if (x > max)
{
max = x;
}
}
}
std::vector<int> make_histogram(const std::vector<double>& numbers, size_t bin_count)
{
std::vector<int> bins(bin_count);
int max_count = bins[0];
double min = numbers[0];
double max = numbers[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;
}

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

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

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

@@ -32,7 +32,15 @@
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="histogram.cpp" />
<Unit filename="histogram.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="main.cpp" />
<Unit filename="text.cpp" />
<Unit filename="text.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Extensions />
</Project>
</CodeBlocks_project_file>

15
lab1var1.layout Обычный файл
Просмотреть файл

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="main.cpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="429" topLine="0" />
</Cursor>
</File>
<File name="histogram.cpp" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="855" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>

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

@@ -3,49 +3,114 @@
#include "histogram.h"
#include "svg.h"
#include "text.h"
#include <curl/curl.h>
#include <sstream>
#include <string>
using namespace std;
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
struct Input
{
vector<double> numbers;
size_t bin_count{};
};
Input input_data()
Input input_data(istream &in, bool promt)
{
size_t number_count, bin_count;
cerr << "Enter number count: ";
cin >> number_count;
if (promt)
{
cerr << "Enter number count: ";
}
in >> number_count;
Input in;
in.numbers.resize(number_count);
Input input;
cerr << "Enter numbers: ";
// vector<double> numbers(number_count);
input.numbers.resize(number_count);
if (promt)
{
cerr << "Enter numbers: ";
}
for (size_t i = 0; i < number_count; i++)
{
cin >> in.numbers[i];
in >> input.numbers[i];
}
cerr << "Count of baskets: ";
cin >> in.bin_count;
if (promt)
{
cerr << "Count of baskets: ";
}
in >> input.bin_count;
return in;
return input;
}
int main()
size_t
write_data(void *items, size_t item_size, size_t item_count, void *ctx)
{
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_svg(bins);
return 0;
size_t data_size = item_count * item_size;
stringstream *buffer = reinterpret_cast<stringstream *>(ctx);
char *item = reinterpret_cast<char *>(items);
//(*buffer).write(item, data_size);
buffer->write(item, data_size);
return data_size;
}
Input download(const string &address)
{
stringstream buffer;
// address.c_str();
curl_global_init(CURL_GLOBAL_ALL);
{
CURL *curl = curl_easy_init();
if (curl)
{
CURLcode res;
double total;
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)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
exit(1);
}
if (CURLE_OK == res)
{
res = curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total);
cerr << "Time spending for downloading file: " << total << endl;
}
curl_easy_cleanup(curl);
}
}
return input_data(buffer, false);
}
int main(int argc, char *argv[])
{
Input input;
if (argc > 1)
{
input = download(argv[1]);
}
else
{
input = input_data(cin, true);
}
const auto bins = make_histogram(input.numbers, input.bin_count);
show_histogram_svg(bins);
}

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

@@ -0,0 +1,71 @@
#include <iostream>
#include <vector>
#include "svg.h"
// #include"svg_rect.h"
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;
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_text(double left, double baseline, std::string text)
{
// std::cout << "<text x='20' y='35'>text</text>";
std::cout << "<text x=' " << left << " ' y='" << baseline << "' > " << text << " </text>";
// std::cout << "<text x='20' y='" << baseline << "' >2</text>";
}
void svg_rect(double x, double y, double width, double height)
{
std::cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << " ' stroke='yellow' fill='#9932CC' ></rect>";
}
void show_histogram_svg(const std::vector<int> &bins)
{
const auto SCALE = (IMAGE_WIDTH - TEXT_WIDTH);
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);
double top = 0;
for (size_t bin : bins)
{
double bin_width = BLOCK_WIDTH * bin;
if (bin_width > SCALE)
{
bin_width = SCALE;
}
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
top += BIN_HEIGHT;
}
svg_end();
}

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

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

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

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

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

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

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

@@ -0,0 +1 @@

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

@@ -0,0 +1,38 @@
<?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>
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>

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

@@ -0,0 +1,12 @@
#define DOCTEST_CONFIG_NO_MULTITHREADING
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include "histogram_internal.h"
TEST_CASE("distinct positive numbers") {
double min = 0;
double max = 0;
find_minmax({1, 2}, min, max);
CHECK(min == 1);
CHECK(max == 2);
}

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

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
</CodeBlocks_layout_file>