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

..

13 Коммитов

11 изменённых файлов: 7429 добавлений и 83 удалений

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

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

Двоичные данные
project/.gitignore поставляемый

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

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

@@ -0,0 +1,63 @@
#include <iostream>
#include <vector>
#include "histogram.h"
using namespace std;
bool
find_minmax(vector<double> numbers, double& min, double& max)
{
bool check;
if(numbers.size()==0)
{
check = false;
}
else
{
min = numbers[0];
max = numbers[0];
for (double x : numbers)
{
if (x < min)
{
min = x;
}
else if (x > max)
{
max = x;
}
}
}
return check;
}
vector<size_t>
make_histogram(vector<double> numbers, size_t bin_count)
{
vector <size_t> bins(bin_count);
double min, max;
bool check;
check = find_minmax(numbers, min, max);
size_t countt;
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
project/histogram.h Обычный файл
Просмотреть файл

@@ -0,0 +1,8 @@
#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

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

@@ -1,110 +1,91 @@
#include <curl/curl.h>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include "histogram.h"
#include "text.h"
#include "svg.h"
#include "emptiness.h"
#include <sstream>
#include <string>
using namespace std; using namespace std;
const size_t SCREEN_WIDTH = 80; struct Input
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; {
struct Input {
vector<double> numbers; vector<double> numbers;
size_t bin_count{}; size_t bin_count{};
}; };
Input Input
input_data(){ input_data(istream& tin, bool prompt)
{
size_t number_count; size_t number_count;
cerr << "Enter number count: "; if (prompt)
cin >> number_count; cerr << "Enter number count: ";
tin >> number_count;
Input in; Input in;
in.numbers.resize(number_count); in.numbers.resize(number_count);
for(int i;i<number_count;i++) for(int i; i<number_count; i++)
{ {
cin >> in.numbers[i]; tin >> in.numbers[i];
} }
cerr << "Enter bin count: "; if (prompt)
cin >> in.bin_count; cerr << "Enter bin count: ";
tin >> in.bin_count;
return in; return in;
} }
void size_t write_data(void* items, size_t item_size, size_t item_count, void* ctx)
find_minmax(vector<double> numbers, double& min, double& max) { {
min = numbers[0]; size_t data_size = item_size * item_count;
max = numbers[0]; stringstream* buffer = reinterpret_cast<stringstream*>(ctx);
for (double x : numbers) { buffer->write(reinterpret_cast<const char*>(items), data_size);
if (x < min) { return data_size;
min = x;
}
else if (x > max) {
max = x;
}
}
} }
vector<size_t> Input
make_histogram(vector<double> numbers, size_t bin_count) { download(const string& address)
vector <size_t> bins(bin_count); {
double min, max; stringstream buffer;
find_minmax(numbers, min, max); CURL *curl = curl_easy_init();
size_t countt; if(curl)
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;
}
void
show_histogram_text(auto bins, size_t bin_count){
size_t max_count = 0;
for (size_t i = 0; i < bin_count; i++){
if(bins[i]>max_count)
max_count = bins[i];
}
size_t height;
for (size_t i = 0; i < bin_count; i++)
{ {
if (bins[i] < 100) CURLcode res;
cout << " "; curl_off_t speed;
if (bins[i] < 10) curl_easy_setopt(curl, CURLOPT_URL, address.c_str());
cout << " "; curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
cout<<bins[i]<<"|"; curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
if(max_count>MAX_ASTERISK){ res = curl_easy_perform(curl);
for (size_t j = 0; j <(height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count)) ; j++)
if (res != CURLE_OK)
{ {
cout<<"*"; fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
exit(1);
} }
cout<<endl; res = curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD_T, &speed);
} if(!res)
else
{ {
for (size_t j = 0; j <bins[i] ; j++) cerr<<"Download speed bytes/sec: " <<speed;
{
cout<<"*";
}
cout<<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);
} }
int main()
{ Input in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins, in.bin_count);
return 0;
}

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

@@ -13,7 +13,12 @@
<Option compiler="gcc" /> <Option compiler="gcc" />
<Compiler> <Compiler>
<Add option="-g" /> <Add option="-g" />
<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/project" prefix_auto="1" extension_auto="1" /> <Option output="bin/Release/project" prefix_auto="1" extension_auto="1" />
@@ -32,7 +37,31 @@
<Add option="-Wall" /> <Add option="-Wall" />
<Add option="-fexceptions" /> <Add option="-fexceptions" />
</Compiler> </Compiler>
<Unit filename=".gitignore" />
<Unit filename="emptiness.cpp" />
<Unit filename="emptiness.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="histogram.cpp" />
<Unit filename="histogram.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="histogram_internal.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="main.cpp" /> <Unit filename="main.cpp" />
<Unit filename="svg.cpp" />
<Unit filename="svg.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="tasktest.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="test.cpp" />
<Unit filename="text.cpp" />
<Unit filename="text.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Extensions /> <Extensions />
</Project> </Project>
</CodeBlocks_project_file> </CodeBlocks_project_file>

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

@@ -0,0 +1,65 @@
#include <math.h>
#include <iostream>
#include <conio.h>
#include <vector>
#include <string>
#include "svg.h"
#include "emptiness.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, string stroke, string fill) {
cout << "<rect x='"<<x<<"' y='"<<y<<"' width='"<<width<<"' height='"<<height<<"' stroke='"<<stroke<<"' fill='"<<fill<<"' />";
}
void
show_histogram_svg(const vector<size_t>& bins) {
const auto IMAGE_WIDTH = 400;
const auto IMAGE_HEIGHT = 300;
const auto TEXT_LEFT = 10;
const auto TEXT_BASELINE = 20;
const auto TEXT_WIDTH = 50;
const auto BIN_HEIGHT = 30;
const auto BLOCK_WIDTH = 10;
const double MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH;
svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT);
double top = 0;
double max_count = bins[0];
for (size_t i = 0; i < bins.size(); i++) {
if (bins[i] > max_count)
max_count = bins[i];
}
for (size_t bin : bins) {
const double bin_width = MAX_WIDTH * (bin/max_count);
double emptiness_width = emptiness(MAX_WIDTH,bin_width);
svg_text(TEXT_LEFT + MAX_WIDTH, top + TEXT_BASELINE, to_string(bin));
svg_rect(emptiness_width, top, bin_width, BIN_HEIGHT, "red", "#aaffaa");
top += BIN_HEIGHT;
}
svg_end();
}

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

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

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

@@ -0,0 +1,42 @@
#include <iostream>
#include <vector>
#include "text.h"
#include "histogram.h"
using namespace std;
void
show_histogram_text(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 i = 0; i < bin_count; i++){
if(bins[i]>max_count)
max_count = bins[i];
}
size_t height;
for (size_t i = 0; i < bin_count; i++)
{
if (bins[i] < 100)
cout << " ";
if (bins[i] < 10)
cout << " ";
cout<<bins[i]<<"|";
if(max_count>MAX_ASTERISK){
for (size_t j = 0; j <(height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count)) ; j++)
{
cout<<"*";
}
cout<<endl;
}
else
{
for (size_t j = 0; j <bins[i] ; j++)
{
cout<<"*";
}
cout<<endl;
}
}
return;
}

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

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

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

@@ -0,0 +1,38 @@
#define DOCTEST_CONFIG_NO_MULTITHREADING
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include "project\emptiness.h"
#include "project\histogram_internal.h"
TEST_CASE("difference check") {
double a = 20;
double b = 10;
double c = emptiness(a,b);
CHECK(c == 10);
CHECK(c > 0);
}
TEST_CASE("more then 0 check") {
double a = 20;
double b = 10;
double c = emptiness(a,b);
CHECK(c > 0);
}
TEST_CASE("less then 0 check") {
double a = 10;
double b = 20;
double c = emptiness(a,b);
CHECK(c < 0);
}
TEST_CASE("not 0 check") {
double a = 20;
double b = 10;
double c = emptiness(a,b);
CHECK(c != 0);
}
TEST_CASE("is massif full") {
double min = 0;
double max = 0;
std::vector<double>v{};
bool c = find_minmax(v,min,max);
CHECK(c == false);
}