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

..

8 Коммитов

Автор 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
8 изменённых файлов: 151 добавлений и 103 удалений

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

@@ -32,7 +32,14 @@
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<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" />
<Unit filename="text.cpp" />
<Unit filename="text.h" />
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>

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

@@ -1,5 +1,9 @@
#include "histogram.h"
void find_minmax(vector<double> vec, double& min, double& max) {
#include "histogram_internal.h"
bool find_minmax(vector<double> vec, double& min, double& max) {
if(vec.size() == 0){
return false;
}
min = vec[0];
max = vec[0];
for (double x : vec) {
@@ -11,6 +15,7 @@ void find_minmax(vector<double> vec, double& min, double& max) {
max = x;
}
}
return true;
}
vector<size_t> make_histogram(size_t number, vector<double> vec) {
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 "text.h"
#include "svg.h"
struct Input {
vector<double> vec;
size_t korz{};
};
Input input_data() {
Input in;
Input input_data(istream& in, bool promt = false) {
Input lin;
size_t n, korz;
if(promt)
cerr << "Number of elem ";
cin >> n;
in.vec.resize(n);
in >> n;
lin.vec.resize(n);
for (size_t i = 0; i < n; i++)
cin >> in.vec[i];
in >> lin.vec[i];
if(promt)
cerr << "Enter bin count: ";
cin >> in.korz;
return in;
in >> lin.korz;
return lin;
}
int main() {
auto in = input_data();
auto bins = make_histogram(in.korz, in.vec);
show_histogram(bins);
size_t write_data(void* items, size_t item_size, size_t item_count, void* ctx){
stringstream* buffer = reinterpret_cast<stringstream*>(ctx);
size_t data_size = item_size * item_count;
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;
}

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

@@ -45,7 +45,7 @@ show_histogram_svg(const vector<size_t>& bins)
const auto BIN_HEIGHT = 30;
const auto BLOCK_WIDTH = 10;
const auto BLACK = "black";
const auto RED = "red";
const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH;
@@ -63,9 +63,10 @@ show_histogram_svg(const vector<size_t>& bins)
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);
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, BLACK,RED);
top += BIN_HEIGHT;
}

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

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

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

@@ -1,3 +1,3 @@
#include <iostream>
#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);
}