Сравнить коммиты
11 Коммитов
6f1662469b
...
master
| Автор | SHA1 | Дата | |
|---|---|---|---|
| 7449ef860f | |||
| 1b1311833a | |||
| 5135699b21 | |||
| 2eaf794295 | |||
| bcc61042de | |||
| d5a9d78924 | |||
| 7bd4bee170 | |||
| c37b1fed88 | |||
| ab7728268d | |||
| 15fb696925 | |||
| 74edc12b97 |
3
.gitignore
поставляемый
Обычный файл
3
.gitignore
поставляемый
Обычный файл
@@ -0,0 +1,3 @@
|
||||
/bin
|
||||
/obj
|
||||
/curl
|
||||
@@ -1,6 +1,7 @@
|
||||
#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);
|
||||
|
||||
7
hist_proc.h
Обычный файл
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
Обычный файл
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
|
||||
@@ -4,8 +4,14 @@
|
||||
using namespace std;
|
||||
|
||||
|
||||
static void
|
||||
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) {
|
||||
@@ -18,6 +24,9 @@ find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
9
histogram.h
Обычный файл
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
|
||||
12
lab003.cbp
12
lab003.cbp
@@ -13,7 +13,11 @@
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-g" />
|
||||
<Add directory="curl/include" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add library="curl/include" />
|
||||
</Linker>
|
||||
</Target>
|
||||
<Target title="Release">
|
||||
<Option output="bin/Release/lab003" prefix_auto="1" extension_auto="1" />
|
||||
@@ -31,13 +35,21 @@
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
<Add option="-fexceptions" />
|
||||
<Add directory="curl/include" />
|
||||
</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="svg.cpp" />
|
||||
<Unit filename="svg.h">
|
||||
<Option target="<{~None~}>" />
|
||||
</Unit>
|
||||
<Unit filename="text.cpp" />
|
||||
<Unit filename="text.h" />
|
||||
<Extensions>
|
||||
|
||||
94
main.cpp
94
main.cpp
@@ -1,9 +1,12 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <hist_proc.h>
|
||||
#include "histogram.h"
|
||||
#include <text.h>
|
||||
|
||||
#include <svg.h>
|
||||
#include <curl/curl.h>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
|
||||
@@ -19,28 +22,95 @@ struct Input {
|
||||
|
||||
|
||||
Input
|
||||
input_data() {
|
||||
input_data(istream& in, bool prompt) {
|
||||
if (prompt == true){
|
||||
size_t number_count;
|
||||
cin >> number_count;
|
||||
Input in;
|
||||
in.numbers.resize(number_count);
|
||||
cerr << "input number count";
|
||||
in >> number_count;
|
||||
Input inn;
|
||||
inn.numbers.resize(number_count);
|
||||
vector<double> numbers(number_count);
|
||||
cerr << "input numbers";
|
||||
for (size_t i = 0; i < number_count; i++) {
|
||||
cin >> in.numbers[i];
|
||||
in >> inn.numbers[i];
|
||||
}
|
||||
|
||||
size_t bin_count;
|
||||
cin >> in.bin_count;
|
||||
return in;
|
||||
cerr << "input amount of bins";
|
||||
in >> inn.bin_count;
|
||||
return inn;
|
||||
}
|
||||
else {
|
||||
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];
|
||||
}
|
||||
|
||||
int main()
|
||||
size_t bin_count;
|
||||
cout << "input amount of bins";
|
||||
in >> inn.bin_count;
|
||||
return inn;
|
||||
}
|
||||
}
|
||||
|
||||
static 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;
|
||||
}
|
||||
|
||||
Input
|
||||
download(const string& address, bool proverka_skorosti) {
|
||||
stringstream buffer;
|
||||
CURL* curl = curl_easy_init();
|
||||
if(curl)
|
||||
{
|
||||
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 procent = make_histogram_proc(in.numbers, in.bin_count, bins);
|
||||
show_histogram(in.bin_count, bins, procent);
|
||||
|
||||
show_histogram_svg(bins, procent);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
88
svg.cpp
Обычный файл
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
Обычный файл
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
|
||||
7
text.h
Обычный файл
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
|
||||
12
unittest4var-2.cpp
Обычный файл
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
Обычный файл
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);
|
||||
}
|
||||
Ссылка в новой задаче
Block a user