Сравнить коммиты
5 Коммитов
c5702a1392
...
main
| Автор | SHA1 | Дата | |
|---|---|---|---|
|
|
25a10722e9 | ||
|
|
5086c43a27 | ||
|
|
7c8fe812f0 | ||
|
|
8c17279d7f | ||
|
|
4d937bcb5d |
5
.gitignore
поставляемый
5
.gitignore
поставляемый
@@ -1,4 +1,7 @@
|
||||
/bin
|
||||
/obj
|
||||
/lab1.depend
|
||||
/lab1.layout
|
||||
/lab1.layout
|
||||
/unittest.layout
|
||||
/unittest.depend
|
||||
/curl
|
||||
7106
doctest.h
Обычный файл
7106
doctest.h
Обычный файл
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
@@ -2,23 +2,23 @@
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
|
||||
Input input_data() {
|
||||
Input in;
|
||||
Input input_data(std::istream& in, bool prompt) {
|
||||
Input data;
|
||||
size_t number_count;
|
||||
|
||||
std::cerr << "Enter number count: ";
|
||||
std::cin >> number_count;
|
||||
if (prompt) std::cerr << "Enter number count: ";
|
||||
in >> number_count;
|
||||
|
||||
in.numbers.resize(number_count);
|
||||
std::cerr << "Enter " << number_count << " numbers: ";
|
||||
data.numbers.resize(number_count);
|
||||
if (prompt) std::cerr << "Enter " << number_count << " numbers: ";
|
||||
for (size_t i = 0; i < number_count; i++) {
|
||||
std::cin >> in.numbers[i];
|
||||
in >> data.numbers[i];
|
||||
}
|
||||
|
||||
std::cerr << "Enter bin count: ";
|
||||
std::cin >> in.bin_count;
|
||||
if (prompt) std::cerr << "Enter bin count: ";
|
||||
in >> data.bin_count;
|
||||
|
||||
return in;
|
||||
return data;
|
||||
}
|
||||
|
||||
void find_minmax(const std::vector<double>& numbers, double& min, double& max) {
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
struct Input {
|
||||
std::vector<double> numbers;
|
||||
size_t bin_count{};
|
||||
};
|
||||
|
||||
Input input_data();
|
||||
Input input_data(std::istream& in, bool prompt);
|
||||
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count);
|
||||
void show_histogram_text(const std::vector<size_t>& bins);
|
||||
|
||||
15
lab1.cbp
15
lab1.cbp
@@ -31,8 +31,23 @@
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
<Add option="-fexceptions" />
|
||||
<Add directory="C:/Users/temka/OneDrive/Desktop/лаба 1/lab1/curl/include" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add option="-static-libstdc++" />
|
||||
<Add library="libcurl.dll.a" />
|
||||
<Add directory="C:/Users/temka/OneDrive/Desktop/лаба 1/lab1/curl/lib" />
|
||||
</Linker>
|
||||
<Unit filename="histogram.cpp" />
|
||||
<Unit filename="histogram.h">
|
||||
<Option target="Debug" />
|
||||
</Unit>
|
||||
<Unit filename="histogram_internal.h">
|
||||
<Option target="Debug" />
|
||||
</Unit>
|
||||
<Unit filename="main.cpp" />
|
||||
<Unit filename="svg.cpp" />
|
||||
<Unit filename="svg.h" />
|
||||
<Extensions>
|
||||
<lib_finder disable_auto="1" />
|
||||
</Extensions>
|
||||
|
||||
51
main.cpp
51
main.cpp
@@ -1,8 +1,51 @@
|
||||
#include "histogram.h"
|
||||
#include "svg.h"
|
||||
#include <curl/curl.h>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
int main() {
|
||||
auto in = input_data();
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
show_histogram_text(bins);
|
||||
size_t write_data(void* ptr, size_t size, size_t nmemb, std::stringstream* stream) {
|
||||
size_t data_size = size * nmemb;
|
||||
stream->write(static_cast<const char*>(ptr), data_size);
|
||||
return data_size;
|
||||
}
|
||||
|
||||
Input download(const std::string& url) {
|
||||
std::stringstream buffer;
|
||||
|
||||
CURL* curl = curl_easy_init();
|
||||
if (!curl) {
|
||||
std::cerr << "Failed to initialize cURL\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
||||
|
||||
CURLcode res = curl_easy_perform(curl);
|
||||
if (res != CURLE_OK) {
|
||||
std::cerr << "cURL error: " << curl_easy_strerror(res) << "\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
curl_easy_cleanup(curl);
|
||||
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(std::cin, true);
|
||||
}
|
||||
|
||||
auto bins = make_histogram(input.numbers, input.bin_count);
|
||||
show_histogram_svg(bins);
|
||||
|
||||
curl_global_cleanup();
|
||||
return 0;
|
||||
}
|
||||
|
||||
0
marks.svg
Обычный файл
0
marks.svg
Обычный файл
3
marks.txt
Обычный файл
3
marks.txt
Обычный файл
@@ -0,0 +1,3 @@
|
||||
10
|
||||
3 3 4 4 4 4 4 5 5 5
|
||||
3
|
||||
74
svg.cpp
Обычный файл
74
svg.cpp
Обычный файл
@@ -0,0 +1,74 @@
|
||||
#include "svg.h"
|
||||
#include <iostream>
|
||||
|
||||
void svg_begin(double width, double height) {
|
||||
std::cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
|
||||
std::cout << "<svg width='" << width << "' height='" << height << "' "
|
||||
<< "viewBox='0 0 " << width << " " << height << "' "
|
||||
<< "xmlns='http://www.w3.org/2000/svg'>\n";
|
||||
}
|
||||
|
||||
void svg_end() {
|
||||
std::cout << "</svg>\n";
|
||||
}
|
||||
|
||||
void svg_text(double left, double baseline, const std::string& text) {
|
||||
std::cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>\n";
|
||||
}
|
||||
|
||||
void svg_rect(double x, double y, double width, double height,
|
||||
const std::string& stroke, const std::string& fill) {
|
||||
std::cout << "<rect x='" << x << "' y='" << y << "' "
|
||||
<< "width='" << width << "' height='" << height << "' "
|
||||
<< "stroke='" << stroke << "' fill='" << fill << "' />\n";
|
||||
}
|
||||
|
||||
/*void show_histogram_svg(const std::vector<size_t>& bins) {
|
||||
const size_t IMAGE_WIDTH = 400;
|
||||
const size_t IMAGE_HEIGHT = 300;
|
||||
const size_t TEXT_LEFT = 20;
|
||||
const size_t TEXT_BASELINE = 20;
|
||||
const size_t TEXT_WIDTH = 50;
|
||||
const size_t BIN_HEIGHT = 30;
|
||||
const size_t BLOCK_WIDTH = 10;
|
||||
|
||||
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
||||
|
||||
double top = 0;
|
||||
size_t max_count = 0;
|
||||
for (size_t count : bins) {
|
||||
if (count > max_count) max_count = count;
|
||||
}
|
||||
|
||||
for (size_t bin : bins) {
|
||||
const double bin_width = static_cast<double>(bin) / max_count * (IMAGE_WIDTH - TEXT_WIDTH);
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin));
|
||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", "#aaffaa");
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
|
||||
svg_end();
|
||||
}*/
|
||||
void show_histogram_svg(const std::vector<size_t>& bins) {
|
||||
size_t max_count = 0;
|
||||
for (size_t count : bins) {
|
||||
if (count > max_count) max_count = count;
|
||||
}
|
||||
|
||||
const size_t IMAGE_HEIGHT = IMAGE_PADDING * 2 + max_count * 10 + TEXT_HEIGHT;
|
||||
|
||||
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
||||
|
||||
double x = IMAGE_PADDING;
|
||||
const double y_base = IMAGE_HEIGHT - IMAGE_PADDING - TEXT_HEIGHT;
|
||||
|
||||
for (size_t bin : bins) {
|
||||
const double height = bin * 10;
|
||||
svg_rect(x, y_base - height, BAR_WIDTH, height, "black", "#aaffaa");
|
||||
svg_text(x + BAR_WIDTH/2 - 5, y_base + TEXT_HEIGHT - 5, std::to_string(bin));
|
||||
|
||||
x += BAR_WIDTH + 10;
|
||||
}
|
||||
|
||||
svg_end();
|
||||
}
|
||||
14
svg.h
Обычный файл
14
svg.h
Обычный файл
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
const size_t IMAGE_WIDTH = 400;
|
||||
const size_t IMAGE_PADDING = 20;
|
||||
const size_t BAR_WIDTH = 30;
|
||||
const size_t TEXT_HEIGHT = 20;
|
||||
void svg_begin(double width, double height);
|
||||
void svg_end();
|
||||
void svg_text(double left, double baseline, const std::string& text);
|
||||
void svg_rect(double x, double y, double width, double height,
|
||||
const std::string& stroke = "black", const std::string& fill = "black");
|
||||
void show_histogram_svg(const std::vector<size_t>& bins);
|
||||
@@ -31,6 +31,10 @@
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
</Compiler>
|
||||
<Unit filename="doctest.h" />
|
||||
<Unit filename="histogram.cpp" />
|
||||
<Unit filename="histogram_internal.h" />
|
||||
<Unit filename="unittest.cpp" />
|
||||
<Extensions>
|
||||
<lib_finder disable_auto="1" />
|
||||
</Extensions>
|
||||
|
||||
45
unittest.cpp
Обычный файл
45
unittest.cpp
Обычный файл
@@ -0,0 +1,45 @@
|
||||
#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("empty vector") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({}, min, max);
|
||||
CHECK(min == std::numeric_limits<double>::max());
|
||||
CHECK(max == std::numeric_limits<double>::lowest());
|
||||
}
|
||||
|
||||
TEST_CASE("single element vector") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({42}, min, max);
|
||||
CHECK(min == 42);
|
||||
CHECK(max == 42);
|
||||
}
|
||||
|
||||
TEST_CASE("negative numbers") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({-5, -1, -3}, min, max);
|
||||
CHECK(min == -5);
|
||||
CHECK(max == -1);
|
||||
}
|
||||
|
||||
TEST_CASE("identical elements") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({7, 7, 7}, min, max);
|
||||
CHECK(min == 7);
|
||||
CHECK(max == 7);
|
||||
}
|
||||
//wewger
|
||||
Ссылка в новой задаче
Block a user