Сравнить коммиты
18 Коммитов
8d01744e6d
...
main
| Автор | SHA1 | Дата | |
|---|---|---|---|
| 3a0651cc1a | |||
| 3db4b095a9 | |||
| 1c7fc9a454 | |||
| c69b174056 | |||
| 0b72e913b2 | |||
| 191fd262a0 | |||
| 5636df9530 | |||
| 6373e22a80 | |||
| 3dae9289ff | |||
| 8c94d31a34 | |||
| 6ef0b93ced | |||
| ece8c021cc | |||
| 397562b360 | |||
| fce6d4daeb | |||
| be7b291133 | |||
| cd46e043f3 | |||
| 07cf1172cd | |||
| 444b499d7d |
5
.gitignore
поставляемый
Обычный файл
5
.gitignore
поставляемый
Обычный файл
@@ -0,0 +1,5 @@
|
||||
bin/
|
||||
obj/
|
||||
curl/
|
||||
Lab1.depend
|
||||
unittest.depend
|
||||
21
Lab1.cbp
21
Lab1.cbp
@@ -30,10 +30,29 @@
|
||||
</Build>
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
<Add option="-fexceptions" />
|
||||
<Add option="-fexceptions -lcurl" />
|
||||
<Add option="-DCURL_STATICLIB" />
|
||||
<Add option='-DCURL_STATICLIB "curl-config --cflags --static-libs"' />
|
||||
<Add option="-DCURL_STATICLIB `curl-config --cflags --static-libs`" />
|
||||
<Add directory="C:/Users/Gamer/GameMaker 8.1/Desktop/учебный кал/lab03/Lab1/curlic/include" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add option="-static-libstdc++" />
|
||||
<Add option="-static-libgcc" />
|
||||
<Add option="-static" />
|
||||
<Add directory="C:/Users/Gamer/GameMaker 8.1/Desktop/учебный кал/lab03/Lab1/curlic/lib" />
|
||||
</Linker>
|
||||
<Unit filename=".gitignore" />
|
||||
<Unit filename="histogram.cpp" />
|
||||
<Unit filename="histogram.h" />
|
||||
<Unit filename="histogram_internal.h" />
|
||||
<Unit filename="main.cpp" />
|
||||
<Unit filename="main.h" />
|
||||
<Unit filename="svg.cpp" />
|
||||
<Unit filename="svg.h" />
|
||||
<Unit filename="svg_internal.h" />
|
||||
<Unit filename="text.cpp" />
|
||||
<Unit filename="text.h" />
|
||||
<Extensions />
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
||||
|
||||
7106
doctest.h
Обычный файл
7106
doctest.h
Обычный файл
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
85
histogram.cpp
Обычный файл
85
histogram.cpp
Обычный файл
@@ -0,0 +1,85 @@
|
||||
#include "histogram.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
void
|
||||
find_minmax(const vector<double>& numbers, double& min, double& max, bool& empty_vector)
|
||||
{
|
||||
if (numbers.empty())
|
||||
{
|
||||
empty_vector = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (double x : numbers)
|
||||
{
|
||||
if (x > max )
|
||||
{
|
||||
max = x;
|
||||
}
|
||||
if (x < min)
|
||||
{
|
||||
min = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vector<size_t>
|
||||
make_histogram(const vector<double>& numbers, size_t& bin_count)
|
||||
{
|
||||
double min, max;
|
||||
bool empty_vector = false;
|
||||
find_minmax(numbers, min, max, empty_vector);
|
||||
|
||||
|
||||
double bin_size = (max - min) / bin_count;
|
||||
vector<size_t> bins(bin_count);
|
||||
|
||||
if (empty_vector)
|
||||
{
|
||||
for (size_t y : bins)
|
||||
{
|
||||
y = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (size_t i = 0; i < numbers.size(); i++)
|
||||
{
|
||||
for (size_t j = i + 1; j < numbers.size(); j++)
|
||||
{
|
||||
if (numbers[i] == numbers[j])
|
||||
{
|
||||
//numbers.erase(numbers.begin() + j);
|
||||
//number_count--;
|
||||
//i--;
|
||||
//cout << "Removed number " << numbers[j] << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < numbers.size(); i++)
|
||||
{
|
||||
bool found = false;
|
||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++)
|
||||
{
|
||||
auto low_bound = min + j * bin_size;
|
||||
auto high_bound = min + (j + 1) * bin_size;
|
||||
if ((low_bound <= numbers[i]) && (numbers[i] < high_bound))
|
||||
{
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found)
|
||||
{
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
}
|
||||
|
||||
return bins;
|
||||
}
|
||||
9
histogram.h
Обычный файл
9
histogram.h
Обычный файл
@@ -0,0 +1,9 @@
|
||||
#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
|
||||
9
histogram_internal.h
Обычный файл
9
histogram_internal.h
Обычный файл
@@ -0,0 +1,9 @@
|
||||
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
|
||||
#define HISTOGRAM_INTERNAL_H_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
|
||||
void
|
||||
find_minmax(const std::vector<double>& numbers, double& min, double& max, bool& empty_vector);
|
||||
|
||||
#endif // HISTOGRAM_INTERNAL_H_INCLUDED
|
||||
148
main.cpp
148
main.cpp
@@ -1,87 +1,109 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include "histogram.h"
|
||||
//#include "text.h"
|
||||
#include "svg.h"
|
||||
#include <curl/curl.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;
|
||||
vector<string> colors;
|
||||
size_t bin_count{};
|
||||
};
|
||||
|
||||
Input
|
||||
input_data(istream& in_stream,bool prompt) {
|
||||
size_t number_count;
|
||||
size_t bin_count;
|
||||
Input in;
|
||||
|
||||
cerr << "Enter number count: ";
|
||||
cin >> number_count;
|
||||
if (prompt) cerr << "Enter number count: ";
|
||||
in_stream >> number_count;
|
||||
vector<double> numbers(number_count);
|
||||
|
||||
for (size_t i = 0; i < number_count; i++)
|
||||
cin >> numbers[i];
|
||||
in.numbers.resize(number_count);
|
||||
|
||||
double min = numbers[0];
|
||||
double max = numbers[0];
|
||||
double max_count = 0;
|
||||
|
||||
for (double value : numbers)
|
||||
{
|
||||
if (value < min) {min = value;}
|
||||
else if (value > max) {max = value;}
|
||||
for (size_t i = 0; i < number_count; i++) {
|
||||
in_stream >> in.numbers[i];
|
||||
}
|
||||
|
||||
cerr << "Enter bins count: ";
|
||||
cin >> bin_count;
|
||||
vector<size_t> bins(bin_count);
|
||||
if (prompt) cerr << "Enter bins count and colors: ";
|
||||
in_stream >> in.bin_count;
|
||||
vector<string> colors(in.bin_count);
|
||||
in.colors.resize(in.bin_count);
|
||||
|
||||
double bin_size = (max - min) / bin_count;
|
||||
|
||||
for (size_t i = 0; i < number_count; i++)
|
||||
{
|
||||
for (size_t j = i + 1; j < number_count; j++)
|
||||
{
|
||||
if (numbers[i] == numbers[j])
|
||||
{
|
||||
//numbers.erase(numbers.begin() + j);
|
||||
//number_count--;
|
||||
//i--;
|
||||
//cout << "Removed number " << numbers[j] << endl;
|
||||
for (size_t i = 0; i < in.bin_count; i++) {
|
||||
//in_stream >> in.colors[i];
|
||||
in.colors[i]="red";
|
||||
bool check = check_color(in.colors[i]);
|
||||
while (!check) {
|
||||
if (prompt) cerr << "Incorrect input. Color doesn't contain # or has spaces." << endl;
|
||||
in_stream >> in.colors[i];
|
||||
//in.colors[i]="red";
|
||||
check = check_color(in.colors[i]);
|
||||
}
|
||||
}
|
||||
return in;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
size_t write_header(char* buffer, size_t size, size_t nitems, void* userdata)
|
||||
{
|
||||
return size * nitems;
|
||||
}
|
||||
|
||||
Input download(const string& address) {
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
stringstream buffer;
|
||||
|
||||
CURL* curl = curl_easy_init();
|
||||
if (curl) {
|
||||
CURLcode res;
|
||||
curl_easy_setopt(curl, CURLOPT_URL, address.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
||||
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_header);
|
||||
curl_easy_setopt(curl, CURLOPT_HEADERDATA, NULL);
|
||||
res = curl_easy_perform(curl);
|
||||
curl_easy_cleanup(curl);
|
||||
|
||||
if (res != CURLE_OK) {
|
||||
cout << curl_easy_strerror(res);
|
||||
exit(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
double *ct;
|
||||
res = curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME, &ct);
|
||||
if((CURLE_OK == res) && ct)
|
||||
printf("Time: %f\n",ct);
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < number_count; i++)
|
||||
{
|
||||
bool found = false;
|
||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++)
|
||||
{
|
||||
auto low_bound = min + j * bin_size;
|
||||
auto high_bound = min + (j + 1) * bin_size;
|
||||
if ((low_bound <= numbers[i]) && (numbers[i] < high_bound))
|
||||
{
|
||||
bins[j]++;
|
||||
found = true;
|
||||
if (bins[j] > max_count)
|
||||
max_count = bins[j];
|
||||
}
|
||||
return input_data(buffer, false);
|
||||
}
|
||||
|
||||
if (!found)
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
bins[bin_count - 1]++;
|
||||
if (bins[bin_count - 1] > max_count)
|
||||
max_count = bins[bin_count - 1];
|
||||
}
|
||||
}
|
||||
for (size_t bin : bins)
|
||||
Input in;
|
||||
if (argc > 1)
|
||||
{
|
||||
if (bin < 100) cout << " ";
|
||||
if (bin < 10) cout << " ";
|
||||
cout << bin;
|
||||
cout << "|";
|
||||
size_t height = MAX_ASTERISK * (static_cast<double>(bin) / max_count);
|
||||
if (max_count <= MAX_ASTERISK) height = bin;
|
||||
for (size_t i = 0; i < height; i++) cout << "*";
|
||||
cout << endl;
|
||||
in = download(argv[1]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
else
|
||||
{
|
||||
in = input_data(cin,true);
|
||||
}
|
||||
vector<size_t> bins = make_histogram(in.numbers, in.bin_count);
|
||||
//show_histogram_text(bins, in.bin_count);
|
||||
show_histogram_svg(bins, in.colors);
|
||||
}
|
||||
|
||||
83
svg.cpp
Обычный файл
83
svg.cpp
Обычный файл
@@ -0,0 +1,83 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "svg.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool
|
||||
check_color(string color) {
|
||||
|
||||
if (color[0]=='#'||color.find(' ')==(-1))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
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 = "black", string fill = "black") {
|
||||
cout << "<rect x='" << x
|
||||
<< "' y='" << y
|
||||
<< "' width='" << width
|
||||
<< "' height='" << height
|
||||
<< "' stroke='" << stroke
|
||||
<< "' fill='" << fill
|
||||
<< "' />\n";
|
||||
}
|
||||
|
||||
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
|
||||
show_histogram_svg(const vector<size_t>& bins, const vector<string>& colors) {
|
||||
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 auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
|
||||
size_t max_count = 0;
|
||||
for (size_t x : bins) {
|
||||
if (x > max_count) {
|
||||
max_count = x;
|
||||
}
|
||||
}
|
||||
if (max_count == 0) {
|
||||
max_count = 1;
|
||||
}
|
||||
auto scale_factor = static_cast<double>(MAX_WIDTH) / (max_count * BLOCK_WIDTH);
|
||||
if (scale_factor > 1) {
|
||||
scale_factor = 1;
|
||||
}
|
||||
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
||||
double top = 0;
|
||||
for (size_t bin : bins) {
|
||||
const double bin_width = BLOCK_WIDTH * bin;
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black",colors[top/BIN_HEIGHT]);
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
svg_end();
|
||||
}
|
||||
13
svg.h
Обычный файл
13
svg.h
Обычный файл
@@ -0,0 +1,13 @@
|
||||
#ifndef SVG_H_INCLUDED
|
||||
#define SVG_H_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
void
|
||||
show_histogram_svg(const std::vector<size_t>& bins, const std::vector<std::string>& colors);
|
||||
|
||||
bool
|
||||
check_color(std::string color);
|
||||
|
||||
#endif // SVG_H_INCLUDED
|
||||
9
svg_internal.h
Обычный файл
9
svg_internal.h
Обычный файл
@@ -0,0 +1,9 @@
|
||||
#ifndef SVG_INTERNAL_H_INCLUDED
|
||||
#define SVG_INTERNAL_H_INCLUDED
|
||||
|
||||
#include <string>
|
||||
|
||||
bool
|
||||
check_color(std::string color);
|
||||
|
||||
#endif // SVG_INTERNAL_H_INCLUDED
|
||||
36
text.cpp
Обычный файл
36
text.cpp
Обычный файл
@@ -0,0 +1,36 @@
|
||||
#include <iostream>
|
||||
#include "text.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
void
|
||||
show_histogram_text(const 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 s = 0; s < bin_count; s++)
|
||||
{
|
||||
if (bins[s] > max_count)
|
||||
{
|
||||
max_count = bins[s];
|
||||
}
|
||||
}
|
||||
|
||||
if (max_count == 0)
|
||||
{
|
||||
max_count = 1;
|
||||
}
|
||||
|
||||
for (size_t bin : bins)
|
||||
{
|
||||
if (bin < 100) cout << " ";
|
||||
if (bin < 10) cout << " ";
|
||||
cout << bin;
|
||||
cout << "|";
|
||||
size_t height = MAX_ASTERISK * (static_cast<double>(bin) / max_count);
|
||||
if (max_count <= MAX_ASTERISK) height = bin;
|
||||
for (size_t i = 0; i < height; i++) cout << "*";
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
9
text.h
Обычный файл
9
text.h
Обычный файл
@@ -0,0 +1,9 @@
|
||||
#ifndef TEXT_H_INCLUDED
|
||||
#define TEXT_H_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
|
||||
void
|
||||
show_histogram_text(const std::vector<size_t>& bins, size_t& bin_count);
|
||||
|
||||
#endif // TEXT_H_INCLUDED
|
||||
39
unittest.cbp
Обычный файл
39
unittest.cbp
Обычный файл
@@ -0,0 +1,39 @@
|
||||
<?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 />
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
||||
70
unittest.cpp
Обычный файл
70
unittest.cpp
Обычный файл
@@ -0,0 +1,70 @@
|
||||
#define DOCTEST_CONFIG_NO_MULTITHREADING
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
#include "doctest.h"
|
||||
#include "histogram_internal.h"
|
||||
#include "svg_internal.h"
|
||||
#include <string>
|
||||
|
||||
TEST_CASE("distinct positive numbers") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
bool empty_vector = false;
|
||||
find_minmax({1, 2}, min, max, empty_vector);
|
||||
CHECK(empty_vector == false);
|
||||
CHECK(min == 1);
|
||||
CHECK(max == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("single number") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
bool empty_vector = false;
|
||||
find_minmax({1}, min, max, empty_vector);
|
||||
CHECK(empty_vector == false);
|
||||
CHECK(min == 1);
|
||||
CHECK(max == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("negative numbers") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
bool empty_vector = false;
|
||||
find_minmax({-1, -2}, min, max, empty_vector);
|
||||
CHECK(empty_vector == false);
|
||||
CHECK(min == -2);
|
||||
CHECK(max == -1);
|
||||
}
|
||||
|
||||
TEST_CASE("identical numbers") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
bool empty_vector = false;
|
||||
find_minmax({2, 2}, min, max, empty_vector);
|
||||
CHECK(empty_vector == false);
|
||||
CHECK(min == 2);
|
||||
CHECK(max == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("empty vector") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
bool empty_vector = false;
|
||||
find_minmax({}, min, max, empty_vector);
|
||||
CHECK(empty_vector == true);
|
||||
CHECK(min == 0);
|
||||
CHECK(max == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("correct color") {
|
||||
bool check = check_color("red");
|
||||
CHECK(check == true);
|
||||
check = check_color("#FF FFF");
|
||||
CHECK(check == true);
|
||||
}
|
||||
|
||||
TEST_CASE("incorrect color") {
|
||||
bool check = check_color("re d");
|
||||
CHECK(check == false);
|
||||
check = check_color("red green blue");
|
||||
CHECK(check == false);
|
||||
}
|
||||
Ссылка в новой задаче
Block a user