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

...

10 Коммитов

Автор SHA1 Сообщение Дата
961b531303 finish 2025-05-21 21:13:43 +03:00
43e4142deb update 2025-05-21 18:40:22 +03:00
de1785831e http 2025-05-21 18:33:47 +03:00
77874d3b68 с божьей помощью подключили curl 2025-05-19 12:18:46 +03:00
aa518e1fed promt 2025-05-16 11:59:17 +03:00
dcb3401ade cin меняем на in 2025-05-16 11:51:45 +03:00
6eb46fd053 unittest 2025-04-25 14:02:29 +03:00
e93efef297 zhachita 2025-04-25 13:56:47 +03:00
0d3fb9ea6a svg 2025-04-22 21:34:21 +03:00
42849f2538 doctest 2025-04-22 21:07:49 +03:00
10 изменённых файлов: 7337 добавлений и 101 удалений

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

@@ -32,7 +32,14 @@
<Add option="-Wall" /> <Add option="-Wall" />
<Add option="-fexceptions" /> <Add option="-fexceptions" />
</Compiler> </Compiler>
<Unit filename="histogram.cpp" />
<Unit filename="histogram.h" />
<Unit filename="histogram_internal.h" />
<Unit filename="main.cpp" /> <Unit filename="main.cpp" />
<Unit filename="svg.cpp" />
<Unit filename="svg.h" />
<Unit filename="text.cpp" />
<Unit filename="text.h" />
<Extensions> <Extensions>
<lib_finder disable_auto="1" /> <lib_finder disable_auto="1" />
</Extensions> </Extensions>

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

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

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

@@ -1,16 +1,21 @@
#include "histogram.h" #include "histogram.h"
void find_minmax(vector<double> vec, double& min, double& max) { #include "histogram_internal.h"
min = vec[0]; bool find_minmax(vector<double> vec, double& min, double& max) {
max = vec[0]; if(vec.size() == 0){
for (double x : vec) { return false;
if (x < min) {
min = x;
}
else if (x > max)
{
max = x;
}
} }
min = vec[0];
max = vec[0];
for (double x : vec) {
if (x < min) {
min = x;
}
else if (x > max)
{
max = x;
}
}
return true;
} }
vector<size_t> make_histogram(size_t number, vector<double> vec) { vector<size_t> make_histogram(size_t number, vector<double> vec) {
vector<size_t> bins(number); vector<size_t> bins(number);

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

@@ -1 +1 @@
void find_minmax(std::vector<double> vec, double& min, double& max); bool find_minmax(const std::vector<double> vec, double& min, double& max);

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

@@ -1,24 +1,61 @@
#include <curl/curl.h>
#include <sstream>
#include <string>
#include "histogram.h" #include "histogram.h"
#include "text.h" #include "text.h"
#include "svg.h"
struct Input { struct Input {
vector<double> vec; vector<double> vec;
size_t korz{}; size_t korz{};
}; };
Input input_data() { Input input_data(istream& in, bool promt = false) {
Input in; Input lin;
size_t n, korz; size_t n, korz;
if(promt)
cerr << "Number of elem "; cerr << "Number of elem ";
cin >> n; in >> n;
in.vec.resize(n); lin.vec.resize(n);
for (size_t i = 0; i < n; i++) for (size_t i = 0; i < n; i++)
cin >> in.vec[i]; in >> lin.vec[i];
cerr << "Enter bin count: "; if(promt)
cin >> in.korz; cerr << "Enter bin count: ";
return in; in >> lin.korz;
return lin;
} }
int main() { size_t write_data(void* items, size_t item_size, size_t item_count, void* ctx){
auto in = input_data(); stringstream* buffer = reinterpret_cast<stringstream*>(ctx);
auto bins = make_histogram(in.korz, in.vec); size_t data_size = item_size * item_count;
show_histogram(bins); buffer->write(reinterpret_cast<const char*>(items), data_size);
return data_size;
}
Input download(const string& address){
stringstream buffer;
CURL* curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, address.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res != CURLE_OK) {
cerr << "cURL error: " << curl_easy_strerror(res) << endl;
exit(1);
}
return input_data(buffer, false);
}
}
int main(int argc, char* argv[]) {
curl_global_init(CURL_GLOBAL_ALL);
Input input;
if (argc > 1) {
input = download(argv[1]);
}else{
input = input_data(cin, true);
}
auto bins = make_histogram(input.korz, input.vec);
show_histogram_svg(bins);
return 0;
} }

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

@@ -0,0 +1,77 @@
#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 = "black", string fill = "black")
{
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 = 20;
const auto TEXT_BASELINE = 20;
const auto TEXT_WIDTH = 50;
const auto BIN_HEIGHT = 30;
const auto BLOCK_WIDTH = 10;
const auto BLACK = "black";
const auto 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 (max_count<bins[i])
{
max_count=bins[i];
}
}
for (size_t bin : bins)
{
const auto RED = "#" + std::to_string(int(ceil(10 - (bin * 9) / max_count))) + std::to_string(int(ceil(10 - (bin * 9) / max_count))) + std::to_string(int(ceil(10 - (bin * 9) / max_count)));
double bin_width = (MAX_WIDTH)*(bin/max_count);
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, BLACK,RED);
top += BIN_HEIGHT;
}
svg_end();
}

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

@@ -0,0 +1,6 @@
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
using namespace std;
void show_histogram_svg(const vector<size_t>& bins);

109
text.cpp
Просмотреть файл

@@ -1,83 +1,44 @@
#include <iostream>
#include <vector>
#include "text.h" #include "text.h"
void show_histogram(std::vector<size_t> bins) { using namespace std;
bool gigant = false;
auto spaces = 0; void show_histogram_text(vector<size_t> bins, size_t bin_count){
size_t mx_count = 0; const size_t SCREEN_WIDTH = 80;
for (auto x : bins) { const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
if (x > 76) {
gigant = true; size_t max_bin = bins[0];
for(size_t i = 0; i < bin_count; i++)
{
if(bins[i] > max_bin)
{
max_bin = bins[i];
} }
if (x > mx_count) { }
mx_count = x;
} for (size_t bin: bins)
auto len = 0; {
while (x > 0) { size_t height = bin;
x /= 10;
len++; if (max_bin > MAX_ASTERISK)
} {
if (len > spaces) { height = MAX_ASTERISK * (static_cast<double>(bin) / max_bin);
spaces = len;
} }
} if (bin < 100)
if (spaces == 1) { {
for (size_t i = 0; i < bins.size(); i++) { cout << ' ';
std::cout << " " << bins[i] << "|";
if (gigant) {
if (bins[i] == mx_count) {
for (size_t j = 0; j < 76; j++) {
std::cout << "*";
}
}
else
{
for (size_t j = 0; j < 76 * static_cast<double>(bins[i]) / mx_count; j++) {
std::cout << "*";
}
}
}
else
{
for (size_t j = 0; j < bins[i]; j++) {
std::cout << "*";
}
std::cout << std::endl;
}
} }
} if (bin < 10)
else {
{ cout << ' ';
for (size_t i = 0; i < bins.size(); i++) {
int len = 1;
int k = bins[i];
for (; k /= 10; ++len);
while (len < spaces) {
std::cout << " ";
len++;
}
std::cout << bins[i];
std::cout << "|";
if (gigant) {
if (bins[i] == mx_count) {
for (size_t j = 0; j < 76; j++) {
std::cout << "*";
}
}
else
{
for (size_t j = 0; j < (76 * static_cast<double>(bins[i]) / mx_count - 1); j++) {
std::cout << "*";
}
}
}
else
{
for (size_t j = 0; j < bins[i]; j++) {
std::cout << "*";
}
}
std::cout << std::endl;
} }
cout << bin << "|";
for(size_t i = 0; i < height; i++)
{
cout << "*";
}
cout << endl;
} }
} }

2
text.h
Просмотреть файл

@@ -1,3 +1,3 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
void show_histogram(std::vector<size_t> bins); void show_histogram_text(std::vector<size_t> bins);

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

@@ -0,0 +1,37 @@
#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({1, 2}, min, max);
CHECK(min == 1);
CHECK(max == 2);
}
TEST_CASE("distinct negative numbers"){
double min = 0;
double max = 0;
find_minmax({-1, -2}, min, max);
CHECK(min == -2);
CHECK(max == -1);
}
TEST_CASE("vector of the same elements"){
double min = 0;
double max = 0;
find_minmax({3,3,3}, min, max);
CHECK(min == 3);
CHECK(max == 3);
}
TEST_CASE("empty vector"){
double min = 0;
double max = 0;
CHECK(!find_minmax({}, min, max));
}
TEST_CASE("vector of one elements"){
double min = 0;
double max = 0;
find_minmax({3}, min, max);
CHECK(min == max);
}