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