Сравнить коммиты
15 Коммитов
5ecec787a9
...
main
| Автор | SHA1 | Дата | |
|---|---|---|---|
| ca7de45c50 | |||
| d55b28de6f | |||
| fe30735e59 | |||
| cf63387552 | |||
| 860cb1779f | |||
| 0fcdf53cc3 | |||
| 2d21389932 | |||
| dfdfc3df83 | |||
| 5057f84975 | |||
| 2341494fc2 | |||
| f7cd78c7b9 | |||
| cd149ef988 | |||
| 29c09ca832 | |||
| 3827e12af2 | |||
| dbc5004f0f |
1
.gitignore
поставляемый
1
.gitignore
поставляемый
@@ -3,3 +3,4 @@
|
|||||||
03-scaling.input.txt
|
03-scaling.input.txt
|
||||||
LAB1.layout
|
LAB1.layout
|
||||||
main.exe
|
main.exe
|
||||||
|
/curl
|
||||||
|
|||||||
7106
doctest.h
Обычный файл
7106
doctest.h
Обычный файл
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
44
histogram.cpp
Обычный файл
44
histogram.cpp
Обычный файл
@@ -0,0 +1,44 @@
|
|||||||
|
#include "histogram.h"
|
||||||
|
|
||||||
|
void
|
||||||
|
find_minmax(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<size_t>
|
||||||
|
make_histogram(std::vector<double>& numbers, size_t bin_count){
|
||||||
|
|
||||||
|
std::vector<size_t> bins(bin_count);
|
||||||
|
|
||||||
|
double min, max;
|
||||||
|
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;
|
||||||
|
}
|
||||||
11
histogram.h
Обычный файл
11
histogram.h
Обычный файл
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef HISTOGRAM_H_INCLUDED
|
||||||
|
#define HISTOGRAM_H_INCLUDED
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
std::vector<size_t>
|
||||||
|
make_histogram(std::vector<double>& numbers, size_t bin_count);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif // HISTOGRAM_H_INCLUDED
|
||||||
10
histogram_internal.h
Обычный файл
10
histogram_internal.h
Обычный файл
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
|
#define HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void
|
||||||
|
find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
||||||
|
|
||||||
|
|
||||||
|
#endif // HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
34
lab1.cbp
34
lab1.cbp
@@ -2,7 +2,7 @@
|
|||||||
<CodeBlocks_project_file>
|
<CodeBlocks_project_file>
|
||||||
<FileVersion major="1" minor="6" />
|
<FileVersion major="1" minor="6" />
|
||||||
<Project>
|
<Project>
|
||||||
<Option title="LABA 1 IGOR" />
|
<Option title="lab34" />
|
||||||
<Option pch_mode="2" />
|
<Option pch_mode="2" />
|
||||||
<Option compiler="gcc" />
|
<Option compiler="gcc" />
|
||||||
<Build>
|
<Build>
|
||||||
@@ -11,32 +11,58 @@
|
|||||||
<Option object_output="obj/Debug/" />
|
<Option object_output="obj/Debug/" />
|
||||||
<Option type="1" />
|
<Option type="1" />
|
||||||
<Option compiler="gcc" />
|
<Option compiler="gcc" />
|
||||||
|
<Option projectLinkerOptionsRelation="0" />
|
||||||
<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/LABA 1 IGOR" prefix_auto="1" extension_auto="1" />
|
<Option output="bin/Release/LABA 1 IGOR" prefix_auto="1" extension_auto="1" />
|
||||||
<Option object_output="obj/Release/" />
|
<Option object_output="obj/Release/" />
|
||||||
<Option type="1" />
|
<Option type="1" />
|
||||||
<Option compiler="gcc" />
|
<Option compiler="gcc" />
|
||||||
|
<Option projectLinkerOptionsRelation="0" />
|
||||||
<Compiler>
|
<Compiler>
|
||||||
<Add option="-O2" />
|
<Add option="-O2" />
|
||||||
|
<Add directory="curl/include" />
|
||||||
</Compiler>
|
</Compiler>
|
||||||
<Linker>
|
<Linker>
|
||||||
<Add option="-s" />
|
<Add option="-s" />
|
||||||
|
<Add library="libcurl.dll.a" />
|
||||||
|
<Add directory="curl/lib" />
|
||||||
</Linker>
|
</Linker>
|
||||||
</Target>
|
</Target>
|
||||||
</Build>
|
</Build>
|
||||||
<Compiler>
|
<Compiler>
|
||||||
<Add option="-Wall" />
|
<Add option="-Wall" />
|
||||||
<Add option="-fexceptions" />
|
<Add option="-fexceptions" />
|
||||||
|
<Add directory="curl/include" />
|
||||||
</Compiler>
|
</Compiler>
|
||||||
|
<Linker>
|
||||||
|
<Add library="libcurl.dll.a" />
|
||||||
|
<Add directory="curl/lib" />
|
||||||
|
</Linker>
|
||||||
|
<Unit filename="histogram.cpp" />
|
||||||
|
<Unit filename="histogram.h">
|
||||||
|
<Option target="<{~None~}>" />
|
||||||
|
</Unit>
|
||||||
|
<Unit filename="histogram_internal.h">
|
||||||
|
<Option target="<{~None~}>" />
|
||||||
|
</Unit>
|
||||||
<Unit filename="main.cpp" />
|
<Unit filename="main.cpp" />
|
||||||
|
<Unit filename="svg.cpp" />
|
||||||
|
<Unit filename="svg.h">
|
||||||
|
<Option link="1" />
|
||||||
|
<Option target="<{~None~}>" />
|
||||||
|
</Unit>
|
||||||
|
<Unit filename="text.cpp" />
|
||||||
|
<Unit filename="text.h" />
|
||||||
<Extensions>
|
<Extensions>
|
||||||
<code_completion />
|
|
||||||
<envvars />
|
|
||||||
<debugger />
|
|
||||||
<lib_finder disable_auto="1" />
|
<lib_finder disable_auto="1" />
|
||||||
</Extensions>
|
</Extensions>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
128
main.cpp
128
main.cpp
@@ -1,107 +1,81 @@
|
|||||||
|
#include <curl/curl.h>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "histogram.h"
|
||||||
|
#include "svg.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
const size_t SCREEN_WIDTH = 80;
|
|
||||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
||||||
|
|
||||||
struct Input {
|
struct Input {
|
||||||
vector<double> numbers;
|
vector<double> numbers;
|
||||||
size_t bin_count{};
|
size_t bin_count{};
|
||||||
};
|
};
|
||||||
|
|
||||||
Input
|
Input
|
||||||
input_data(){
|
input_data(istream& in, bool prompt){
|
||||||
|
|
||||||
size_t number_count;
|
size_t number_count;
|
||||||
cerr << "Enter number_count: ";
|
if (prompt == true) cerr << "Enter number_count: ";
|
||||||
cin >> number_count;
|
in >> number_count;
|
||||||
|
|
||||||
Input in;
|
Input in1;
|
||||||
in.numbers.resize(number_count);
|
in1.numbers.resize(number_count);
|
||||||
cerr << "Enter numbers: ";
|
if (prompt == true) cerr << "Enter numbers: ";
|
||||||
for (size_t i = 0; i < number_count; i++){
|
for (size_t i = 0; i < number_count; i++){
|
||||||
cin >> in.numbers[i];
|
in >> in1.numbers[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
cerr << "Enter bin count: ";
|
if (prompt == true) cerr << "Enter bin count: ";
|
||||||
cin >> in.bin_count;
|
in >> in1.bin_count;
|
||||||
|
|
||||||
return in;
|
return in1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
static size_t
|
||||||
find_minmax(vector<double>& numbers, double& min, double& max){
|
write_data(void* items, size_t item_size, size_t item_count, void* ctx){
|
||||||
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> make_histogram(vector<double>& numbers, size_t bin_count) {
|
Input
|
||||||
double min, max;
|
download(const string& address) {
|
||||||
find_minmax(numbers, min, max);
|
stringstream buffer;
|
||||||
|
|
||||||
vector<size_t> bins(bin_count, 0);
|
curl_global_init(CURL_GLOBAL_ALL);
|
||||||
double bin_size = (max - min) / bin_count;
|
CURL *curl = curl_easy_init();
|
||||||
|
if(curl) {
|
||||||
for (double number : numbers) {
|
CURLcode res, res1;
|
||||||
size_t bin_index = bin_count - 1; // default to last bin
|
curl_easy_setopt(curl, CURLOPT_URL, address.c_str());
|
||||||
if (number < max) {
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
|
||||||
bin_index = static_cast<size_t>((number - min) / bin_size);
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
||||||
}
|
res = curl_easy_perform(curl);
|
||||||
bins[bin_index]++;
|
curl_easy_cleanup(curl);
|
||||||
}
|
if (res != CURLE_OK){
|
||||||
|
curl_off_t speed;
|
||||||
return bins;
|
res1 = curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD_T, &speed);
|
||||||
}
|
if(!res1){
|
||||||
|
cout << "Upload speed bytes/sec\n" << speed;
|
||||||
void show_histogram_text(const vector<size_t>& bins){
|
exit(1);
|
||||||
size_t i;
|
|
||||||
size_t height = 0;
|
|
||||||
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 bin:bins){
|
|
||||||
size_t height = bin;
|
|
||||||
height = MAX_ASTERISK * (static_cast<double>(bin) / max_count);
|
|
||||||
for(size_t i = 0; i < height; i++){
|
|
||||||
cout<<"*";
|
|
||||||
}
|
}
|
||||||
cout<<"|";
|
|
||||||
if(bin<100) cout<<" ";
|
|
||||||
if(bin<10) cout<<" ";
|
|
||||||
cout<<bin<<endl;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for(size_t bin:bins){
|
|
||||||
size_t height = bin;
|
|
||||||
for(size_t i = 0; i < height; i++){
|
|
||||||
cout<<"*";
|
|
||||||
}
|
|
||||||
cout<<"|";
|
|
||||||
if(bin<100) cout<<" ";
|
|
||||||
if(bin<10) cout<<" ";
|
|
||||||
cout<<bin<<endl;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return input_data(buffer, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main()
|
main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
auto in = input_data();
|
Input input;
|
||||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
if (argc > 1) {
|
||||||
show_histogram_text(bins);
|
input = download(argv[1]);
|
||||||
|
} else {
|
||||||
|
input = input_data(cin, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto bins = make_histogram(input.numbers, input.bin_count);
|
||||||
|
show_histogram_svg(bins);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
61
svg.cpp
Обычный файл
61
svg.cpp
Обычный файл
@@ -0,0 +1,61 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#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, string stroke, string fil){
|
||||||
|
cout << "<rect x = '" << x << "' y = '" << y << "' width = '" << width << "' height = '" << height << "' stroke = '" << stroke << "' fill = '" << fil << "' />";
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_svg(const vector<size_t>& bins) {
|
||||||
|
const auto IMAGE_WIDTH = 400;
|
||||||
|
const auto IMAGE_HEIGHT = 300;
|
||||||
|
const auto IMAGE_LEFT = 20;
|
||||||
|
const auto TEXT_LEFT = 30;
|
||||||
|
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);
|
||||||
|
|
||||||
|
size_t max_count = 0;
|
||||||
|
for (size_t x: bins) {
|
||||||
|
if (x > max_count) {
|
||||||
|
max_count = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
double top = 0;
|
||||||
|
for (size_t bin : bins) {
|
||||||
|
const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH) * bin / max_count;
|
||||||
|
const double text_left = bin_width + TEXT_LEFT;
|
||||||
|
svg_rect(IMAGE_LEFT, top, bin_width, BIN_HEIGHT,"red","#ffeeee");
|
||||||
|
svg_text(text_left, top + TEXT_BASELINE, to_string(bin));
|
||||||
|
top += BIN_HEIGHT;
|
||||||
|
}
|
||||||
|
svg_end();
|
||||||
|
}
|
||||||
|
|
||||||
23
svg.h
Обычный файл
23
svg.h
Обычный файл
@@ -0,0 +1,23 @@
|
|||||||
|
#ifndef SVG_H_INCLUDED
|
||||||
|
#define SVG_H_INCLUDED
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void
|
||||||
|
svg_begin(double width, double height);
|
||||||
|
|
||||||
|
void
|
||||||
|
svg_end();
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_svg(const std::vector<size_t>& bins);
|
||||||
|
|
||||||
|
void
|
||||||
|
svg_text(double left, double baseline, std::string text);
|
||||||
|
|
||||||
|
void
|
||||||
|
svg_rect(double x, double y, double width, double height, std::string stroke = "black", std::string fil = "black");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif // SVG_H_INCLUDED
|
||||||
36
text.cpp
Обычный файл
36
text.cpp
Обычный файл
@@ -0,0 +1,36 @@
|
|||||||
|
#include "text.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_text(std::vector<size_t>& bins){
|
||||||
|
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 bin:bins){
|
||||||
|
size_t height = bin;
|
||||||
|
height = MAX_ASTERISK * (static_cast<double>(bin) / max_count);
|
||||||
|
for(size_t i = 0; i < height; i++){
|
||||||
|
std::cout<<"*";
|
||||||
|
}
|
||||||
|
std::cout<<"|";
|
||||||
|
if(bin<100) std::cout<<" ";
|
||||||
|
if(bin<10) std::cout<<" ";
|
||||||
|
std::cout<<bin<<std::endl;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for(size_t bin:bins){
|
||||||
|
size_t height = bin;
|
||||||
|
for(size_t i = 0; i < height; i++){
|
||||||
|
std::cout<<"*";
|
||||||
|
}
|
||||||
|
std::cout<<"|";
|
||||||
|
if(bin<100) std::cout<<" ";
|
||||||
|
if(bin<10) std::cout<<" ";
|
||||||
|
std::cout<<bin<<std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
12
text.h
Обычный файл
12
text.h
Обычный файл
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef TEXT_H_INCLUDED
|
||||||
|
#define TEXT_H_INCLUDED
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
const size_t SCREEN_WIDTH = 80;
|
||||||
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_text(std::vector<size_t>& bins);
|
||||||
|
|
||||||
|
#endif // TEXT_H_INCLUDED
|
||||||
41
unittest.cbp
Обычный файл
41
unittest.cbp
Обычный файл
@@ -0,0 +1,41 @@
|
|||||||
|
<?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>
|
||||||
|
<lib_finder disable_auto="1" />
|
||||||
|
</Extensions>
|
||||||
|
</Project>
|
||||||
|
</CodeBlocks_project_file>
|
||||||
12
unittest.cpp
Обычный файл
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({-2, -1}, min, max);
|
||||||
|
CHECK(min == -2);
|
||||||
|
CHECK(max == -1);
|
||||||
|
}
|
||||||
Ссылка в новой задаче
Block a user