Сравнить коммиты
18 Коммитов
9cc0a041e0
...
main
| Автор | SHA1 | Дата | |
|---|---|---|---|
| ca7de45c50 | |||
| d55b28de6f | |||
| fe30735e59 | |||
| cf63387552 | |||
| 860cb1779f | |||
| 0fcdf53cc3 | |||
| 2d21389932 | |||
| dfdfc3df83 | |||
| 5057f84975 | |||
| 2341494fc2 | |||
| f7cd78c7b9 | |||
| cd149ef988 | |||
| 29c09ca832 | |||
| 3827e12af2 | |||
| dbc5004f0f | |||
| 5ecec787a9 | |||
| 8a540766d0 | |||
| 2c0a8e0f70 |
6
.gitignore
поставляемый
Обычный файл
6
.gitignore
поставляемый
Обычный файл
@@ -0,0 +1,6 @@
|
||||
/bin
|
||||
/obj
|
||||
03-scaling.input.txt
|
||||
LAB1.layout
|
||||
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>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="LABA 1 IGOR" />
|
||||
<Option title="lab34" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Build>
|
||||
@@ -11,32 +11,58 @@
|
||||
<Option object_output="obj/Debug/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Option projectLinkerOptionsRelation="0" />
|
||||
<Compiler>
|
||||
<Add option="-g" />
|
||||
<Add directory="curl/include" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add library="libcurl.dll.a" />
|
||||
<Add directory="curl/lib" />
|
||||
</Linker>
|
||||
</Target>
|
||||
<Target title="Release">
|
||||
<Option output="bin/Release/LABA 1 IGOR" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Release/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Option projectLinkerOptionsRelation="0" />
|
||||
<Compiler>
|
||||
<Add option="-O2" />
|
||||
<Add directory="curl/include" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add option="-s" />
|
||||
<Add library="libcurl.dll.a" />
|
||||
<Add directory="curl/lib" />
|
||||
</Linker>
|
||||
</Target>
|
||||
</Build>
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
<Add option="-fexceptions" />
|
||||
<Add directory="curl/include" />
|
||||
</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="svg.cpp" />
|
||||
<Unit filename="svg.h">
|
||||
<Option link="1" />
|
||||
<Option target="<{~None~}>" />
|
||||
</Unit>
|
||||
<Unit filename="text.cpp" />
|
||||
<Unit filename="text.h" />
|
||||
<Extensions>
|
||||
<code_completion />
|
||||
<envvars />
|
||||
<debugger />
|
||||
<lib_finder disable_auto="1" />
|
||||
</Extensions>
|
||||
</Project>
|
||||
|
||||
129
main.cpp
129
main.cpp
@@ -1,76 +1,81 @@
|
||||
#include <curl/curl.h>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "histogram.h"
|
||||
#include "svg.h"
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
struct Input {
|
||||
vector<double> numbers;
|
||||
size_t bin_count{};
|
||||
};
|
||||
|
||||
size_t number_count, bin_count, i, bin;
|
||||
//cout << "Enter number count: " << endl;
|
||||
cin>>number_count;
|
||||
vector<double> numbers(number_count);
|
||||
//cout<< "Enter numbers: "<<endl;
|
||||
Input
|
||||
input_data(istream& in, bool prompt){
|
||||
size_t number_count;
|
||||
if (prompt == true) cerr << "Enter number_count: ";
|
||||
in >> number_count;
|
||||
|
||||
Input in1;
|
||||
in1.numbers.resize(number_count);
|
||||
if (prompt == true) cerr << "Enter numbers: ";
|
||||
for (size_t i = 0; i < number_count; i++){
|
||||
cin>>numbers[i];
|
||||
in >> in1.numbers[i];
|
||||
}
|
||||
double min = numbers[0];
|
||||
double max = numbers[0];
|
||||
for (double x : numbers) {
|
||||
if (x < min) {
|
||||
min = x;
|
||||
}
|
||||
else if (x > max) {
|
||||
max = x;
|
||||
|
||||
if (prompt == true) cerr << "Enter bin count: ";
|
||||
in >> in1.bin_count;
|
||||
|
||||
return in1;
|
||||
}
|
||||
|
||||
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) {
|
||||
stringstream buffer;
|
||||
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
CURL *curl = curl_easy_init();
|
||||
if(curl) {
|
||||
CURLcode res, res1;
|
||||
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);
|
||||
curl_easy_cleanup(curl);
|
||||
if (res != CURLE_OK){
|
||||
curl_off_t speed;
|
||||
res1 = curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD_T, &speed);
|
||||
if(!res1){
|
||||
cout << "Upload speed bytes/sec\n" << speed;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//cout<< "Enter bin count: "<<endl;
|
||||
cin>>bin_count;
|
||||
vector<size_t> bins(bin_count);
|
||||
double bin_size = (max - min) / bin_count;
|
||||
return input_data(buffer, false);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < number_count; 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]++;
|
||||
}
|
||||
}
|
||||
size_t height = 0;
|
||||
size_t max_count = 0;
|
||||
for (i = 0; i < bin_count; i++){
|
||||
if (bins[i] > max_count) max_count = bins[i];
|
||||
}
|
||||
/*for(size_t bin:bins){
|
||||
size_t height = bin;
|
||||
height = MAX_ASTERISK * (static_cast<double>(bin) / max_count);
|
||||
if(bin<100) cout<<" ";
|
||||
if(bin<10) cout<<" ";
|
||||
cout<<bin<<"|";
|
||||
for(size_t i = 0; i < height; i++){
|
||||
cout<<"*";
|
||||
}
|
||||
cout<<endl;
|
||||
}*/
|
||||
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;
|
||||
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);
|
||||
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