Сравнить коммиты
15 Коммитов
b5dbea59e9
...
main
| Автор | SHA1 | Дата | |
|---|---|---|---|
| 793740de35 | |||
| 10793dd568 | |||
| 6138cac446 | |||
| 06677ee183 | |||
| 518ab2b6b5 | |||
| dfe74bcb26 | |||
| d0b5fddd35 | |||
| 6210fae2ed | |||
| 40e200994c | |||
| 17511d3b9e | |||
| 238fcef658 | |||
| 9ac6132fb6 | |||
| 389e8322b6 | |||
| 14bd172c26 | |||
| 2760082054 |
5
.gitignore
поставляемый
5
.gitignore
поставляемый
@@ -2,5 +2,8 @@
|
||||
/obj
|
||||
/lab01.depend
|
||||
/lab01.layout
|
||||
/main.exe
|
||||
/main.o
|
||||
/main.exe
|
||||
/unittest.layout
|
||||
/curl
|
||||
libcurl-x64.dll
|
||||
|
||||
7106
doctest.h
Обычный файл
7106
doctest.h
Обычный файл
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
42
histogram.cpp
Обычный файл
42
histogram.cpp
Обычный файл
@@ -0,0 +1,42 @@
|
||||
#include "histogram.h"
|
||||
|
||||
bool find_minmax(vector<double> vec, double& min, double& max) {
|
||||
if (vec.size() == 0) {
|
||||
cerr << "Empty vec";
|
||||
return false;
|
||||
}
|
||||
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> bins(number);
|
||||
double mn, mx;
|
||||
find_minmax(vec, mn, mx);
|
||||
float bin_size = (mx - mn) / number;
|
||||
for (size_t i = 0; i < vec.size(); i++) {
|
||||
bool fl = false;
|
||||
for (size_t j = 0; (j < number - 1) && !fl; j++) {
|
||||
auto lo = mn + j * bin_size;
|
||||
auto hi = mn + (j + 1) * bin_size;
|
||||
if ((lo <= vec[i]) && (vec[i] < hi)) {
|
||||
bins[j]++;
|
||||
fl = true;
|
||||
}
|
||||
}
|
||||
if (!fl) {
|
||||
bins[number - 1]++;
|
||||
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
||||
5
histogram.h
Обычный файл
5
histogram.h
Обычный файл
@@ -0,0 +1,5 @@
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
vector<size_t> make_histogram(size_t number, vector<double> vec);
|
||||
2
histogram_internal.h
Обычный файл
2
histogram_internal.h
Обычный файл
@@ -0,0 +1,2 @@
|
||||
bool find_minmax(std::vector<double> vec, double& min, double& max);
|
||||
//
|
||||
@@ -32,7 +32,15 @@
|
||||
<Add option="-Wall" />
|
||||
<Add option="-fexceptions" />
|
||||
</Compiler>
|
||||
<Unit filename=".gitignore" />
|
||||
<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>
|
||||
|
||||
173
main.cpp
173
main.cpp
@@ -1,128 +1,63 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <curl/curl.h>
|
||||
|
||||
using namespace std;
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include "histogram.h"
|
||||
#include "text.h"
|
||||
#include "svg.h"
|
||||
|
||||
int main() {
|
||||
struct Input {
|
||||
vector<double> vec;
|
||||
size_t korz{};
|
||||
};
|
||||
Input input_data(istream& in, bool prompt = false) {
|
||||
Input lin;
|
||||
size_t n, korz;
|
||||
if(prompt)
|
||||
cerr << "Number of elements: ";
|
||||
in >> n;
|
||||
lin.vec.resize(n);
|
||||
for (size_t i = 0; i < n; i++)
|
||||
in >> lin.vec[i];
|
||||
if(prompt)
|
||||
cerr << "Enter bin count: ";
|
||||
in >> lin.korz;
|
||||
return lin;
|
||||
}
|
||||
|
||||
size_t number_count, bin_count;
|
||||
double min, max, bin_size;
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
cerr << "Enter the number of elements: ";
|
||||
cin >> number_count;
|
||||
|
||||
vector<double> numbers(number_count);
|
||||
|
||||
cerr << "\nEnter " << number_count << " elements:" << endl;
|
||||
for (size_t i = 0; i < number_count; i++) {
|
||||
cin >> numbers[i];
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
cerr << "Enter the number of bins: ";
|
||||
cin >> bin_count;
|
||||
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (double x : numbers) {
|
||||
if (x < min) {
|
||||
min = x;
|
||||
} else {
|
||||
if (x > max) {
|
||||
max = x;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bin_size = (max - min) / bin_count;
|
||||
|
||||
vector<size_t> bins(bin_count, 0);
|
||||
|
||||
for (size_t i = 0; i < number_count; 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 ((numbers[i] >= lo) && (numbers[i] <= hi)) {
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
}
|
||||
|
||||
size_t max_count = bins[0];
|
||||
for (size_t i = 1; i < bin_count; i++) {
|
||||
if (bins[i] > max_count) {
|
||||
max_count = bins[i];
|
||||
}
|
||||
}
|
||||
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
|
||||
|
||||
short space_count;
|
||||
|
||||
cerr << "\nHistogram:" << endl;
|
||||
|
||||
if (max_count <= MAX_ASTERISK) {
|
||||
for (size_t i = 0; i < bin_count; i++) {
|
||||
if (bins[i] < 10) {
|
||||
space_count = 2;
|
||||
} else if (bins[i] >= 10 && bins[i] < 100) {
|
||||
space_count = 1;
|
||||
} else if (bins[i] >= 100 && bins[i] < 1000) {
|
||||
space_count = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
for (size_t k = 0; k < space_count; k++) {
|
||||
cout << " ";
|
||||
}
|
||||
|
||||
cout << bins[i];
|
||||
|
||||
cout << "|";
|
||||
|
||||
|
||||
for (size_t j = 0; j < bins[i]; j++) {
|
||||
cout << "*";
|
||||
}
|
||||
|
||||
cout << "\n";
|
||||
}
|
||||
} else {
|
||||
for (size_t i = 0; i < bin_count; i++) {
|
||||
if (bins[i] < 10) {
|
||||
space_count = 2;
|
||||
} else if (bins[i] >= 10 && bins[i] < 100) {
|
||||
space_count = 1;
|
||||
} else if (bins[i] >= 100 && bins[i] < 1000) {
|
||||
space_count = 0;
|
||||
}
|
||||
|
||||
|
||||
for (size_t k = 0; k < space_count; k++) {
|
||||
cout << " ";
|
||||
}
|
||||
|
||||
cout << bins[i];
|
||||
|
||||
cout << "|";
|
||||
|
||||
size_t height = static_cast<size_t>(MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count));
|
||||
for (size_t j = 0; j < height; j++) {
|
||||
cout << "*";
|
||||
}
|
||||
|
||||
cout << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
auto bins = make_histogram(input.korz, input.vec);
|
||||
show_histogram_svg(bins);
|
||||
return 0;
|
||||
}
|
||||
|
||||
73
svg.cpp
Обычный файл
73
svg.cpp
Обычный файл
@@ -0,0 +1,73 @@
|
||||
#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 GREEN = "green";
|
||||
const auto RED = "red";
|
||||
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)
|
||||
{
|
||||
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, GREEN, RED);
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
|
||||
svg_end();
|
||||
}
|
||||
7
svg.h
Обычный файл
7
svg.h
Обычный файл
@@ -0,0 +1,7 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <math.h>
|
||||
|
||||
using namespace std;
|
||||
void show_histogram_svg(const vector<size_t>& bins);
|
||||
82
text.cpp
Обычный файл
82
text.cpp
Обычный файл
@@ -0,0 +1,82 @@
|
||||
#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;
|
||||
}
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
4
text.h
Обычный файл
4
text.h
Обычный файл
@@ -0,0 +1,4 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
void show_histogram(std::vector<size_t> bins);
|
||||
40
unittest.cbp
Обычный файл
40
unittest.cbp
Обычный файл
@@ -0,0 +1,40 @@
|
||||
<?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="doctest.h" />
|
||||
<Unit filename="histogram.cpp" />
|
||||
<Unit filename="histogram_internal.h" />
|
||||
<Unit filename="unittest.cpp" />
|
||||
<Extensions />
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
||||
49
unittest.cpp
Обычный файл
49
unittest.cpp
Обычный файл
@@ -0,0 +1,49 @@
|
||||
#define DOCTEST_CONFIG_NO_MULTITHREADING
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
#include "doctest.h"
|
||||
#include "histogram_internal.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
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;
|
||||
vector<double> empty;
|
||||
bool result = find_minmax(empty, min, max);
|
||||
CHECK(!result);
|
||||
}
|
||||
|
||||
TEST_CASE("single vector"){
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
vector<double> single = {3};
|
||||
find_minmax(single, min, max);
|
||||
CHECK(min == 3);
|
||||
CHECK(max == 3);
|
||||
}
|
||||
|
||||
TEST_CASE("negative vector"){
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
vector<double> negative = {-1, -2};
|
||||
find_minmax(negative, min, max);
|
||||
CHECK(min == -2);
|
||||
CHECK(max == -1);
|
||||
}
|
||||
|
||||
TEST_CASE("same vector"){
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
vector<double> identical = {1, 1};
|
||||
find_minmax(identical, min, max);
|
||||
CHECK(min == 1);
|
||||
CHECK(max == 1);
|
||||
}
|
||||
59
unittest.depend
Обычный файл
59
unittest.depend
Обычный файл
@@ -0,0 +1,59 @@
|
||||
# depslib dependency file v1.0
|
||||
1746175747 source:c:\users\german\desktop\Ïðîãè Ñ2\lab01\histogram.cpp
|
||||
"histogram.h"
|
||||
|
||||
1746114403 c:\users\german\desktop\Ïðîãè Ñ2\lab01\histogram.h
|
||||
<vector>
|
||||
<iostream>
|
||||
|
||||
1746175582 source:c:\users\german\desktop\Ïðîãè Ñ2\lab01\unittest.cpp
|
||||
"doctest.h"
|
||||
"histogram_internal.h"
|
||||
|
||||
1746115894 c:\users\german\desktop\Ïðîãè Ñ2\lab01\doctest.h
|
||||
<signal.h>
|
||||
<ciso646>
|
||||
<cstddef>
|
||||
<ostream>
|
||||
<istream>
|
||||
<type_traits>
|
||||
"doctest_fwd.h"
|
||||
<ctime>
|
||||
<cmath>
|
||||
<climits>
|
||||
<math.h>
|
||||
<new>
|
||||
<cstdio>
|
||||
<cstdlib>
|
||||
<cstring>
|
||||
<limits>
|
||||
<utility>
|
||||
<fstream>
|
||||
<sstream>
|
||||
<iostream>
|
||||
<algorithm>
|
||||
<iomanip>
|
||||
<vector>
|
||||
<atomic>
|
||||
<mutex>
|
||||
<set>
|
||||
<map>
|
||||
<unordered_set>
|
||||
<exception>
|
||||
<stdexcept>
|
||||
<csignal>
|
||||
<cfloat>
|
||||
<cctype>
|
||||
<cstdint>
|
||||
<string>
|
||||
<sys/types.h>
|
||||
<unistd.h>
|
||||
<sys/sysctl.h>
|
||||
<AfxWin.h>
|
||||
<windows.h>
|
||||
<io.h>
|
||||
<sys/time.h>
|
||||
<unistd.h>
|
||||
|
||||
1746175326 c:\users\german\desktop\Ïðîãè Ñ2\lab01\histogram_internal.h
|
||||
|
||||
Ссылка в новой задаче
Block a user