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

..

15 Коммитов

Автор SHA1 Сообщение Дата
ca7de45c50 code: fix2 2024-10-28 00:33:11 +03:00
d55b28de6f code: fix 2024-10-26 19:16:17 +03:00
fe30735e59 code: сделан индивидуальный вариант 2024-10-26 19:14:18 +03:00
cf63387552 code: Выполнен общий вариант 2024-10-26 16:59:03 +03:00
860cb1779f code: Добавлен вызов curl_easy_init() и обработка ошибок 2024-10-26 16:18:03 +03:00
0fcdf53cc3 code: добавлен вывод argc и argv 2024-10-26 15:47:19 +03:00
2d21389932 code: Добавлены параметры argc и argv в main 2024-10-26 15:40:29 +03:00
dfdfc3df83 build: Добавлена библиотека cURL 2024-10-20 17:09:21 +03:00
5057f84975 code: Добавлен параметр primpt в input_data() 2024-10-20 15:14:33 +03:00
2341494fc2 code: Добавлен параметр cin в input_data 2024-10-20 15:01:33 +03:00
f7cd78c7b9 code: сделан индивидуальный вариант 2024-10-20 02:45:50 +03:00
cd149ef988 build: Осуществлен вывод гистограммы в формате SVG 2024-10-20 02:14:20 +03:00
29c09ca832 build: Создан проект для модульных тестов и добавлена библиотека doctest.h 2024-10-20 00:33:28 +03:00
3827e12af2 build: выделение в отдельные файлы функции show_histogram_text() 2024-10-20 00:26:09 +03:00
dbc5004f0f build: Программа разделена на файлы 2024-10-20 00:23:44 +03:00
13 изменённых файлов: 7438 добавлений и 81 удалений

1
.gitignore поставляемый
Просмотреть файл

@@ -3,3 +3,4 @@
03-scaling.input.txt
LAB1.layout
main.exe
/curl

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

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

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 Обычный файл
Просмотреть файл

@@ -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 Обычный файл
Просмотреть файл

@@ -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

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

@@ -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="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="histogram_internal.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="main.cpp" />
<Unit filename="svg.cpp" />
<Unit filename="svg.h">
<Option link="1" />
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="text.cpp" />
<Unit filename="text.h" />
<Extensions>
<code_completion />
<envvars />
<debugger />
<lib_finder disable_auto="1" />
</Extensions>
</Project>

130
main.cpp
Просмотреть файл

@@ -1,107 +1,81 @@
#include <curl/curl.h>
#include <sstream>
#include <string>
#include <iostream>
#include <vector>
#include "histogram.h"
#include "svg.h"
using namespace std;
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
struct Input {
vector<double> numbers;
size_t bin_count{};
};
Input
input_data(){
input_data(istream& in, bool prompt){
size_t number_count;
cerr << "Enter number_count: ";
cin >> number_count;
if (prompt == true) cerr << "Enter number_count: ";
in >> number_count;
Input in;
in.numbers.resize(number_count);
cerr << "Enter numbers: ";
Input in1;
in1.numbers.resize(number_count);
if (prompt == true) cerr << "Enter numbers: ";
for (size_t i = 0; i < number_count; i++){
cin >> in.numbers[i];
in >> in1.numbers[i];
}
cerr << "Enter bin count: ";
cin >> in.bin_count;
if (prompt == true) cerr << "Enter bin count: ";
in >> in1.bin_count;
return in;
return in1;
}
void
find_minmax(vector<double>& numbers, double& min, double& max){
min = numbers[0];
max = numbers[0];
for (double x : numbers) {
if (x < min) {
min = x;
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;
}
else if (x > max) {
max = x;
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);
}
}
}
vector<size_t> make_histogram(vector<double>& numbers, size_t bin_count) {
double min, max;
find_minmax(numbers, min, max);
vector<size_t> bins(bin_count, 0);
double bin_size = (max - min) / bin_count;
for (double number : numbers) {
size_t bin_index = bin_count - 1; // default to last bin
if (number < max) {
bin_index = static_cast<size_t>((number - min) / bin_size);
}
bins[bin_index]++;
}
return bins;
}
void show_histogram_text(const vector<size_t>& bins){
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
main()
main(int argc, char* argv[])
{
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins);
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 Обычный файл
Просмотреть файл

@@ -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 Обычный файл
Просмотреть файл

@@ -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 Обычный файл
Просмотреть файл

@@ -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 Обычный файл
Просмотреть файл

@@ -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 Обычный файл
Просмотреть файл

@@ -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 Обычный файл
Просмотреть файл

@@ -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);
}