Сравнить коммиты
7 Коммитов
ca853cef18
...
main
| Автор | SHA1 | Дата | |
|---|---|---|---|
|
|
21c78ba11b | ||
|
|
2f9243eb49 | ||
|
|
15ce772a8f | ||
|
|
07f4e26326 | ||
|
|
5c80c670c0 | ||
|
|
4eb9fb549b | ||
|
|
5f4d64fd36 |
2
.gitignore
поставляемый
2
.gitignore
поставляемый
@@ -4,3 +4,5 @@
|
|||||||
/cs-lab34.layout
|
/cs-lab34.layout
|
||||||
/main.exe
|
/main.exe
|
||||||
/main.o
|
/main.o
|
||||||
|
/unittest.layout
|
||||||
|
/curl
|
||||||
|
|||||||
@@ -31,7 +31,12 @@
|
|||||||
<Compiler>
|
<Compiler>
|
||||||
<Add option="-Wall" />
|
<Add option="-Wall" />
|
||||||
<Add option="-fexceptions" />
|
<Add option="-fexceptions" />
|
||||||
|
<Add directory="curl/include" />
|
||||||
</Compiler>
|
</Compiler>
|
||||||
|
<Linker>
|
||||||
|
<Add library="curl/lib/libcurl.dll.a" />
|
||||||
|
<Add directory="curl/lib" />
|
||||||
|
</Linker>
|
||||||
<Unit filename=".gitignore" />
|
<Unit filename=".gitignore" />
|
||||||
<Unit filename="histogram.cpp" />
|
<Unit filename="histogram.cpp" />
|
||||||
<Unit filename="histogram.h" />
|
<Unit filename="histogram.h" />
|
||||||
|
|||||||
@@ -3,4 +3,3 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
vector<size_t> make_histogram(size_t number, vector<double> vec);
|
vector<size_t> make_histogram(size_t number, vector<double> vec);
|
||||||
|
|
||||||
|
|||||||
@@ -1 +1,2 @@
|
|||||||
bool find_minmax(std::vector<double> vec, double& min, double& max);
|
bool find_minmax(std::vector<double> vec, double& min, double& max);
|
||||||
|
//
|
||||||
|
|||||||
90
main.cpp
90
main.cpp
@@ -1,3 +1,7 @@
|
|||||||
|
#include <curl/curl.h>
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
#include "histogram.h"
|
#include "histogram.h"
|
||||||
#include "text.h"
|
#include "text.h"
|
||||||
#include "svg.h"
|
#include "svg.h"
|
||||||
@@ -5,40 +9,78 @@
|
|||||||
struct Input {
|
struct Input {
|
||||||
vector<double> vec;
|
vector<double> vec;
|
||||||
size_t korz{};
|
size_t korz{};
|
||||||
size_t block_width{};
|
|
||||||
};
|
};
|
||||||
|
Input input_data(istream& in, bool prompt = false) {
|
||||||
Input input_data() {
|
Input lin;
|
||||||
Input in;
|
|
||||||
size_t n, korz;
|
size_t n, korz;
|
||||||
|
if(prompt)
|
||||||
cerr << "Number of elements: ";
|
cerr << "Number of elements: ";
|
||||||
cin >> n;
|
in >> n;
|
||||||
in.vec.resize(n);
|
lin.vec.resize(n);
|
||||||
cerr << "Elements: ";
|
|
||||||
for (size_t i = 0; i < n; i++)
|
for (size_t i = 0; i < n; i++)
|
||||||
cin >> in.vec[i];
|
in >> lin.vec[i];
|
||||||
|
if(prompt)
|
||||||
cerr << "Enter bin count: ";
|
cerr << "Enter bin count: ";
|
||||||
cin >> in.korz;
|
in >> lin.korz;
|
||||||
|
return lin;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 block width in px (3-30): ";
|
Input download(const string& address){
|
||||||
cin >> in.block_width;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
while (in.block_width < 3 || in.block_width > 30) {
|
int main(int argc, char* argv[]) {
|
||||||
if (in.block_width < 3) {
|
curl_global_init(CURL_GLOBAL_ALL);
|
||||||
cerr << "Block width too small! Minimum is 3px. Please enter again: ";
|
Input input;
|
||||||
|
if (argc > 1) {
|
||||||
|
input = download(argv[1]);
|
||||||
}else{
|
}else{
|
||||||
cerr << "Block width too large! Maximum is 30px. Please enter again: ";
|
input = input_data(cin, true);
|
||||||
}
|
}
|
||||||
cin >> in.block_width;
|
auto bins = make_histogram(input.korz, input.vec);
|
||||||
|
show_histogram_svg(bins);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return in;
|
//Ïîëó÷åíèå àðãóìåíòîâ êîìàíäû. Ñóòü â òîì, ÷òî êàæäûé àðãóìåíò (ïîòîê ñèìâîëîâ) èìååò ñâîé ïîðÿäêîâûé íîìåð.
|
||||||
}
|
//×òîáû ïðîãðàììà ìîëãà âîñïðèíèìàòü àðãóìåíòû è êîìàíäû òàê, êàê íàäî, àðãóìåíòû, êàê è ñëîâà â ëþáîì ÿçûêå, ðàçäåëÿþòñÿ ïðîáåëàìè.
|
||||||
|
// íàøåì ñëó÷àå ïðîãðàììà ïîêàçûâàåò, êàêîé ïîðÿäêîâûé íîìåð èìååò êàæäîå ñëîâî ñ ïîìîùüþ ïðîáåëîâ.
|
||||||
|
//Äëÿ NUL íè÷åãî íå âûâåäåòñÿ òàê êàê ìû âûâåëè èòîãîâûé ïîòîê â íèêóäà.
|
||||||
|
|
||||||
int main() {
|
/*
|
||||||
auto in = input_data();
|
C:\Users\Natasha\Desktop\Ïðîãè Ñ2\cs-lab34\bin\Debug>cs-lab34.exe -x --y /z w
|
||||||
auto bins = make_histogram(in.korz, in.vec);
|
argc = 5
|
||||||
show_histogram_svg(bins, in.block_width);
|
argv[0] = cs-lab34.exe
|
||||||
}
|
argv[1] = -x
|
||||||
|
argv[2] = --y
|
||||||
|
argv[3] = /z
|
||||||
|
argv[4] = w
|
||||||
|
|
||||||
|
C:\Users\Natasha\Desktop\Ïðîãè Ñ2\cs-lab34\bin\Debug>cs-lab34.exe param "with spaces"
|
||||||
|
argc = 3
|
||||||
|
argv[0] = cs-lab34.exe
|
||||||
|
argv[1] = param
|
||||||
|
argv[2] = with spaces
|
||||||
|
|
||||||
|
C:\Users\Natasha\Desktop\Ïðîãè Ñ2\cs-lab34\bin\Debug>cs-lab34.exe param <marks.txt >NUL
|
||||||
|
*/
|
||||||
|
|||||||
45
svg.cpp
45
svg.cpp
@@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void svg_begin(double width, double height) {
|
void
|
||||||
|
svg_begin(double width, double height)
|
||||||
|
{
|
||||||
cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
|
cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
|
||||||
cout << "<svg ";
|
cout << "<svg ";
|
||||||
cout << "width='" << width << "' ";
|
cout << "width='" << width << "' ";
|
||||||
@@ -11,46 +13,59 @@ void svg_begin(double width, double height) {
|
|||||||
cout << "xmlns='http://www.w3.org/2000/svg'>\n";
|
cout << "xmlns='http://www.w3.org/2000/svg'>\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void svg_end() {
|
void
|
||||||
|
svg_end()
|
||||||
|
{
|
||||||
cout << "</svg>\n";
|
cout << "</svg>\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void svg_text(double left, double baseline, string text) {
|
void
|
||||||
|
svg_text(double left, double baseline, string text)
|
||||||
|
{
|
||||||
cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>";
|
cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>";
|
||||||
}
|
}
|
||||||
|
|
||||||
void svg_rect(double x, double y, double width, double height, string stroke, string fill) {
|
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 << "' />";
|
cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << "' />";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void show_histogram_svg(const vector<size_t>& bins, size_t block_width) {
|
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_svg(const vector<size_t>& bins)
|
||||||
|
{
|
||||||
const auto IMAGE_WIDTH = 400;
|
const auto IMAGE_WIDTH = 400;
|
||||||
const auto IMAGE_HEIGHT = 300;
|
const auto IMAGE_HEIGHT = 300;
|
||||||
const auto TEXT_LEFT = 20;
|
const auto TEXT_LEFT = 20;
|
||||||
const auto TEXT_BASELINE = 20;
|
const auto TEXT_BASELINE = 20;
|
||||||
const auto TEXT_WIDTH = 50;
|
const auto TEXT_WIDTH = 50;
|
||||||
const auto BIN_HEIGHT = 30;
|
const auto BIN_HEIGHT = 30;
|
||||||
const auto BLACK = "black";
|
const auto BLOCK_WIDTH = 10;
|
||||||
|
const auto GREEN = "green";
|
||||||
const auto RED = "red";
|
const auto RED = "red";
|
||||||
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
|
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
|
||||||
|
|
||||||
|
|
||||||
svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT);
|
svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT);
|
||||||
|
|
||||||
double top = 0;
|
double top = 0;
|
||||||
double max_count = bins.empty() ? 0 : bins[0];
|
double max_count = bins[0];
|
||||||
for (size_t i = 1; i < bins.size(); i++) {
|
for (size_t i = 0; i < bins.size(); i++)
|
||||||
if (bins[i] > max_count) {
|
{
|
||||||
|
if (max_count < bins[i])
|
||||||
|
{
|
||||||
max_count = bins[i];
|
max_count = bins[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t bin : bins) {
|
for (size_t bin : bins)
|
||||||
double bin_width = static_cast<double>(block_width) * bin;
|
{
|
||||||
if (bin_width > MAX_WIDTH) {
|
double bin_width = (MAX_WIDTH) * (bin/max_count);
|
||||||
bin_width = MAX_WIDTH;
|
|
||||||
}
|
|
||||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
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, GREEN, RED);
|
||||||
top += BIN_HEIGHT;
|
top += BIN_HEIGHT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
4
svg.h
4
svg.h
@@ -2,6 +2,6 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
void show_histogram_svg(const vector<size_t>& bins, size_t block_width);
|
using namespace std;
|
||||||
|
void show_histogram_svg(const vector<size_t>& bins);
|
||||||
|
|||||||
1
text.h
1
text.h
@@ -2,4 +2,3 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
void show_histogram(std::vector<size_t> bins);
|
void show_histogram(std::vector<size_t> bins);
|
||||||
|
|
||||||
|
|||||||
17
unittest.cpp
17
unittest.cpp
@@ -21,6 +21,9 @@ TEST_CASE("empty vector"){
|
|||||||
CHECK(!result);
|
CHECK(!result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
TEST_CASE("single vector"){
|
TEST_CASE("single vector"){
|
||||||
double min = 0;
|
double min = 0;
|
||||||
double max = 0;
|
double max = 0;
|
||||||
@@ -47,3 +50,17 @@ TEST_CASE("same vector"){
|
|||||||
CHECK(min == 1);
|
CHECK(min == 1);
|
||||||
CHECK(max == 1);
|
CHECK(max == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("block"){
|
||||||
|
size_t block_width = 15;
|
||||||
|
bool result = width( block_width);
|
||||||
|
CHECK(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST_CASE("block"){
|
||||||
|
size_t block_width = 0;
|
||||||
|
bool result = width( block_width);
|
||||||
|
CHECK(!result);
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
# depslib dependency file v1.0
|
# depslib dependency file v1.0
|
||||||
1746274971 source:c:\users\natasha\desktop\Ïðîãè Ñ2\cs-lab34\histogram.cpp
|
1746447381 source:c:\users\natasha\desktop\Ïðîãè Ñ2\cs-lab34\histogram.cpp
|
||||||
"histogram.h"
|
"histogram.h"
|
||||||
|
|
||||||
1746274971 c:\users\natasha\desktop\Ïðîãè Ñ2\cs-lab34\histogram.h
|
1746446253 c:\users\natasha\desktop\Ïðîãè Ñ2\cs-lab34\histogram.h
|
||||||
<vector>
|
<vector>
|
||||||
<iostream>
|
<iostream>
|
||||||
|
|
||||||
1746276122 source:c:\users\natasha\desktop\Ïðîãè Ñ2\cs-lab34\unittest.cpp
|
1746447498 source:c:\users\natasha\desktop\Ïðîãè Ñ2\cs-lab34\unittest.cpp
|
||||||
"doctest.h"
|
"doctest.h"
|
||||||
"histogram_internal.h"
|
"histogram_internal.h"
|
||||||
|
|
||||||
@@ -55,5 +55,5 @@
|
|||||||
<sys/time.h>
|
<sys/time.h>
|
||||||
<unistd.h>
|
<unistd.h>
|
||||||
|
|
||||||
1746276025 c:\users\natasha\desktop\Ïðîãè Ñ2\cs-lab34\histogram_internal.h
|
1746447065 c:\users\natasha\desktop\Ïðîãè Ñ2\cs-lab34\histogram_internal.h
|
||||||
|
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user