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

...

11 Коммитов

4 изменённых файлов: 131 добавлений и 9 удалений

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

@@ -14,6 +14,11 @@
<Compiler> <Compiler>
<Add option="-g" /> <Add option="-g" />
</Compiler> </Compiler>
<Linker>
<Add option="-static-libstdc++" />
<Add option="-static-libgcc" />
<Add option="-static" />
</Linker>
</Target> </Target>
<Target title="Release"> <Target title="Release">
<Option output="bin/Release/fin_lab34" prefix_auto="1" extension_auto="1" /> <Option output="bin/Release/fin_lab34" prefix_auto="1" extension_auto="1" />
@@ -25,14 +30,34 @@
</Compiler> </Compiler>
<Linker> <Linker>
<Add option="-s" /> <Add option="-s" />
<Add option="-static-libstdc++" />
<Add option="-static-libgcc" />
<Add option="-static" />
</Linker> </Linker>
</Target> </Target>
</Build> </Build>
<Compiler> <Compiler>
<Add option="-Wall" /> <Add option="-Wall" />
<Add option="-fexceptions" /> <Add option="-fexceptions" />
<Add directory="D:/PROG/PROGRAMMING/fin_lab34/curl/include" />
</Compiler> </Compiler>
<Unit filename="main.cpp" /> <Linker>
<Add option="-static-libstdc++" />
<Add option="-static-libgcc" />
<Add option="-static" />
<Add option="-DCURL_STATICLIB" />
<Add library="D:/PROG/PROGRAMMING/fin_lab34/curl/lib/libcurl.dll.a" />
<Add directory="D:/PROG/PROGRAMMING/fin_lab34/curl/lib" />
</Linker>
<Unit filename=".gitignore" />
<Unit filename="fin_lab34.cpp" />
<Unit filename="histogram.cpp" />
<Unit filename="histogram.h" />
<Unit filename="histogram_internal.h" />
<Unit filename="svg.cpp" />
<Unit filename="svg.h" />
<Unit filename="text.cpp" />
<Unit filename="text.h" />
<Extensions> <Extensions>
<lib_finder disable_auto="1" /> <lib_finder disable_auto="1" />
</Extensions> </Extensions>

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

@@ -1,9 +1,14 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <cmath> #include <cmath>
#include <sstream>
#include <string>
#include <fstream>
#include <curl/curl.h>
#include "histogram.h" #include "histogram.h"
#include "text.h" #include "text.h"
#include "svg.h" #include "svg.h"
const size_t WINDOW_WIDTH = 80; const size_t WINDOW_WIDTH = 80;
const size_t MAX_VALUE = WINDOW_WIDTH - 3 - 1; const size_t MAX_VALUE = WINDOW_WIDTH - 3 - 1;
using namespace std; using namespace std;
@@ -14,24 +19,72 @@ struct Input{
}; };
Input Input
input_data(){ input_data(istream &thread, bool prompt){
//ïðîèçîøëà íàêëàäêà ñ èìåíàìè ïåðåìåííûõ: ïðè íàçâàíèè ïîòîêà in ðóãàåòñÿ íà ñ22, â êîòîðîé ÿêîáû îáúÿâëÿåòñÿ àðãóìåíò
prompt = false;
size_t number_count; size_t number_count;
if (prompt) {cerr << "Enter the amount of numbers: " << endl;}
cin >> number_count; cin >> number_count;
Input in; Input in;
in.numbers.resize(number_count); in.numbers.resize(number_count);
if (prompt) {cerr << "Enter the numbers: " << endl;}
for (size_t i = 0; i < number_count; i++) { for (size_t i = 0; i < number_count; i++) {
cin >> in.numbers[i]; cin >> in.numbers[i];
} }
if (prompt) {cerr << "Enter the amount of bins: " << endl;}
cin >> in.bin_count; cin >> in.bin_count;
return in; return in;
} }
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;
}
Input
download(const string& address){
stringstream buffer;
CURLcode res;
CURL *curl = curl_easy_init();
if (curl){
curl_easy_setopt(curl, CURLOPT_URL, address.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
res = curl_easy_perform(curl);
cerr << curl_easy_getinfo(curl, CURLINFO_PRIMARY_IP, write_data); //var 8
if (res != CURLE_OK){
cerr << curl_easy_strerror(res);
exit(1);
}
return input_data(buffer, false);
}
curl_easy_cleanup(curl);
}
int int
main(){ main(int argc, char* argv[]){
double min = 0, max = 0; curl_global_init(CURL_GLOBAL_ALL);
Input in = input_data(); Input input, in;
if (argc > 1) {
input = download(argv[1]);
} else {
input = input_data(cin, true);
}
curl_global_init(CURL_GLOBAL_ALL);
//CURLcode curl_global_init(CURL_GLOBAL_ALL);
size_t font = 0;
//auto in = input_data(cin, prompt);
auto bins = make_histogram(in.numbers, in.bin_count); auto bins = make_histogram(in.numbers, in.bin_count);
//for (auto now: bins) {cout << now << endl;} //for (auto now: bins) {cout << now << endl;}
show_histogram_svg(bins); cin >> font;
if (font < 8) {(cerr << font << "Font value is too small. Please, edit the input file.");}
else if (font > 32) {(cerr << "Font value is too large. Please, edit the input file.");}
else {show_histogram_svg(bins, font);}
return 0; return 0;
} }
//ñ êëàâèàòóðû â marks.txt, ñ marks.txt â marks.svg

38
svg.cpp
Просмотреть файл

@@ -1,5 +1,6 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <string>
#include "svg.h" #include "svg.h"
using namespace std; using namespace std;
void void
@@ -16,9 +17,42 @@ void
svg_end() { svg_end() {
cout << "</svg>\n"; cout << "</svg>\n";
} }
void
svg_text(double left, double baseline, size_t font, string text){
cout << "<text x='" << left << "' y='" << baseline << "' font-size='" << font << "' >" << text << " </text>" << endl;
return;
}
void
svg_rect(double x, double y, double width, double height, string stroke = "#ed53a8", string fill = "#9df3a1"){
cout << "<rect x='" << x << "' y='" << y << "' width= '" << width << "' height= '" << height << "' stroke= '" << stroke << "' fill= '" << fill << "'/>" << endl;
return;
}
void void
show_histogram_svg(const vector <size_t> &bins) { show_histogram_svg(const vector <size_t> &bins, size_t &font) {
svg_begin(400, 300);
const auto IMAGE_WIDTH = 800;
const auto IMAGE_HEIGHT = 600;
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 MAX_VALUE = IMAGE_WIDTH - TEXT_WIDTH;
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
double top = 0;
size_t max_count = 0;
for (auto now: bins)
{if (now > max_count) {max_count = now;}}
for (size_t bin : bins){
double bin_width = BLOCK_WIDTH * bin;
if (bin == max_count) {bin_width = MAX_VALUE * 1.0;}
else {bin_width = MAX_VALUE * (static_cast <double> (bin) / max_count);}
svg_text(TEXT_LEFT, top + TEXT_BASELINE, font, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
top += BIN_HEIGHT;
}
svg_end(); svg_end();
return;
} }

12
svg.h
Просмотреть файл

@@ -1,6 +1,7 @@
#ifndef SVG_H_INCLUDED #ifndef SVG_H_INCLUDED
#define SVG_H_INCLUDED #define SVG_H_INCLUDED
#include <vector> #include <vector>
#include <string>
using namespace std; using namespace std;
void void
svg_begin(double width, double height); svg_begin(double width, double height);
@@ -9,5 +10,14 @@ void
svg_end(); svg_end();
void void
show_histogram_svg(const vector <size_t> &bins); svg_text(double left, double baseline, size_t font, string text);
size_t
enter_font_size(size_t &font);
void
svg_rect(double x, double y, double width, double height, string stroke, string fill);
void
show_histogram_svg(const vector <size_t> &bins, size_t &font);
#endif // SVG_H_INCLUDED #endif // SVG_H_INCLUDED