code: Два задания 9 и 11

main
FilippovDY 11 месяцев назад
Родитель 62efa8240e
Сommit 8f1c0dc2ae

@ -13,6 +13,7 @@
<Option compiler="gcc" /> <Option compiler="gcc" />
<Compiler> <Compiler>
<Add option="-g" /> <Add option="-g" />
<Add directory="curl-8.7.1_9-win64-mingw/include" />
</Compiler> </Compiler>
</Target> </Target>
<Target title="Release"> <Target title="Release">
@ -37,7 +38,6 @@
<Add library="libcurl.dll.a" /> <Add library="libcurl.dll.a" />
<Add directory="curl-8.7.1_9-win64-mingw/lib" /> <Add directory="curl-8.7.1_9-win64-mingw/lib" />
</Linker> </Linker>
<Unit filename="doctest.h" />
<Unit filename="histogram.cpp" /> <Unit filename="histogram.cpp" />
<Unit filename="histogram.h" /> <Unit filename="histogram.h" />
<Unit filename="histogram_internal.h" /> <Unit filename="histogram_internal.h" />

@ -1,46 +1,43 @@
#include <iostream>
#include <vector> #include <vector>
#include "histogram.h" #include "histogram.h"
void find_minmax(const std::vector<double>& numbers, double& min, double& max) { #include "histogram_internal.h"
using namespace std;
void find_minmax(const vector<double>& numbers, double& min, double& max) {
max = 0;
if (numbers.size() != 0) {
min = numbers[0]; min = numbers[0];
max = numbers[0]; max = numbers[0];
for (double x : numbers) for (auto el : numbers) {
{ if (el > max) {
if (x < min){ max = el;
min = x;
} }
else if (x > max){ if (el < min) {
max = x; min = el;
} }
} }
} else {min = 0;}
} }
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) {
std::vector <size_t> make_histogram(const std::vector<double>&numbers, size_t &bin_count, size_t &number_count, size_t &max_count) { vector<size_t> bins(bin_count);
double min, max; double max, min;
find_minmax(numbers, min, max); find_minmax(numbers, min, max);
double bin_size = (max - min) / bin_count; double binSize = (max - min) / bin_count;
std::vector<size_t> bins(bin_count); for (size_t i = 0; i < numbers.size(); i++) {
max_count = bins[0];
for (size_t i = 0; i < number_count; i++){
bool found = false; bool found = false;
for (size_t j = 0; (j < bin_count - 1) && !found; j++) { for (size_t j = 0; (j < bin_count - 1) && !found; j++) {
auto lo = min + j * bin_size; auto lo = min + j * binSize;
auto hi = min + (j + 1) * bin_size; auto hi = min + (j + 1) * binSize;
if ((lo <= numbers[i]) && (numbers[i] < hi)) { if ((lo <= numbers[i]) && (numbers[i] < hi)) {
bins[j]++; bins[j]++;
found = true; found = true;
} }
if (bins[j] > max_count){
max_count = bins[j];
}
} }
if (!found) { if (!found) {
bins[bin_count - 1]++; bins[bin_count - 1]++;
} }
if (bins[bin_count - 1] > max_count){
max_count = bins[bin_count - 1];
}
} }
return bins; return bins;
} }

@ -1,5 +1,4 @@
#pragma once #pragma once
#include <vector> #include <vector>
std::vector<size_t> std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count);
make_histogram(const std::vector<double>&numbers, size_t & bin_count, size_t & number_count, size_t & max_count);

@ -3,93 +3,70 @@
#include "histogram.h" #include "histogram.h"
#include "text.h" #include "text.h"
#include "svg.h" #include "svg.h"
#include <curl/curl.h>
#include <sstream> #include <sstream>
#include <string> #include <curl/curl.h>
using namespace std;
struct Input using namespace std;
{
struct Input {
vector<double> numbers; vector<double> numbers;
size_t bin_count{}; size_t bin_count{};
size_t number_count{};
size_t max_count{};
}; };
Input Input input_data(istream& stream) {
input_data(istream& stream, bool prompt) Input in;
{
size_t number_count; size_t number_count;
if (prompt==true)
{
cerr << "Enter number count: ";
}
stream >> number_count; stream >> number_count;
Input in; in.numbers.resize(number_count);
if (prompt==true) for (size_t i = 0; i < number_count; i++) {
{
cerr << "Enter bin count: ";
}
stream >> in.bin_count;
vector<double> numbers(in.number_count);
in.numbers.resize(in.number_count);
for (size_t i = 0; i < in.number_count; i++)
{
stream >> in.numbers[i]; stream >> in.numbers[i];
} }
size_t bin_count; stream >> in.bin_count;
size_t max_count;
in.max_count = 0;
return in; return in;
} }
size_t write_data(void* items, size_t item_size, size_t item_count, void* ctx)
{ size_t write_data(void* items, size_t item_size, size_t item_count, void* ctx) {
size_t data_size = item_size * item_count; size_t data_size = item_size * item_count;
stringstream* buffer = reinterpret_cast<stringstream*>(ctx); stringstream* buffer = reinterpret_cast<stringstream*>(ctx);
auto text = reinterpret_cast<const char*>(items); buffer->write(reinterpret_cast<const char*>(items), data_size);
buffer->write(text, data_size);
return data_size; return data_size;
} }
Input download(const string& address)
{
Input download(const string& address) {
stringstream buffer; stringstream buffer;
curl_global_init(CURL_GLOBAL_ALL);
CURL* curl = curl_easy_init(); CURL* curl = curl_easy_init();
if(curl) if (curl) {
{
CURLcode res; CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, address.c_str()); curl_easy_setopt(curl, CURLOPT_URL, address.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
curl_easy_cleanup(curl); if (res != CURLE_OK) {
if (res != 0) cout << curl_easy_strerror(res);
{
cout << res;
exit(1); exit(1);
} }
curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW); auto data = curl_version_info(CURLVERSION_NOW)->protocols;
for (size_t i = 0; curl_info->protocols != NULL; i++){ for (auto protocol = data; *protocol; ++protocol) {
cerr << curl_info->protocols[i] << "\n"; cerr << *protocol << endl;
} }
curl_global_init(CURL_GLOBAL_ALL); curl_easy_cleanup(curl);
return input_data(buffer, false);
} }
return input_data(buffer);
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
Input in; Input in;
if (argc > 1) { if (argc > 1) {
in = download(argv[1]); in = download(argv[1]);
} else { } else {
in = input_data(cin, true); in = input_data(cin);
} }
auto bins = make_histogram(in.numbers, in.bin_count, in.number_count, in.max_count); auto bins = make_histogram(in.numbers, in.bin_count);
vector<double> test; show_histogram_svg(bins);
show_histogram_svg(bins, in.max_count, in.bin_count, test); cerr << "cURL version " << curl_version_info(CURLVERSION_NOW)->version << endl;
auto ssl_ver = curl_version_info(CURLVERSION_NOW)->ssl_version;
cerr << "SSL version " << ssl_ver << "\n";
return 0; return 0;
} }

@ -1,77 +1,61 @@
#include "iostream" #include <iostream>
#include "svg.h" #include "svg.h"
using namespace std;
const auto IMAGE_WIDTH = 400; const auto IMAGE_WIDTH = 400;
const auto IMAGE_HEIGHT = 300; const auto IMAGE_HEIGHT = 310;
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 BLOCK_WIDTH = 10;
void svg_begin(double width, double height, std::vector <double>& test) void svg_begin(double width, double height) {
{ cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
test.push_back(width); cout << "<svg ";
test.push_back(height); cout << "width='" << width << "' ";
std::cout << "<?xml version='1.0' encoding='UTF-8'?>\n"; cout << "height='" << height << "' ";
std::cout << "<svg "; cout << "viewBox='0 0 " << width << " " << height << "' ";
std::cout << "width='" << width << "' "; cout << "xmlns='http://www.w3.org/2000/svg'>\n";
std::cout << "height='" << height << "' "; cout << "<line x1='10' y1='2' x2='" << width + 10 <<"' y2='2' stroke-dasharray = '10 10' stroke='black'/>\n";
std::cout << "viewBox='0 0 " << width << " " << height << "' ";
std::cout << "xmlns='http://www.w3.org/2000/svg'>\n";
}
void svg_text(double left, double baseline, std::string text)
{
std::cout << "<text x='"<< left <<"' y='"<< baseline <<"'>"<< text <<"</text>";
} }
void svg_rect(double x, double y, double width, double height, std::string stroke = "black", std::string fill = "red") void svg_end(double top, ostream& stream) {
{ stream << "<line x1='10' y1='" << top << "' x2='" << IMAGE_WIDTH <<"' y2='" << top << "' stroke-dasharray = '10 10' stroke='black'/>\n";
std::cout<< "<rect x='"<< x <<"' y='"<< y <<"' width='"<< width <<"' height='"<< height<<"' stroke='black' fill='red' ></rect>"; stream << "</svg>\n";
} }
void svg_line(double x, double y, double x2, double y2, std::vector <double>& test) void svg_text(double left, double baseline, string text) {
{ cout << "<line x1='10' y1='" << baseline - TEXT_BASELINE << "' x2='10' y2='" << baseline <<"' stroke-dasharray = '10 10' stroke='black'/>\n";
test.push_back(x); cout << "<text x='" << left << "' y='" << baseline << "'>" << text <<"</text>\n";
test.push_back(y);
test.push_back(x2);
test.push_back(y2);
std::cout<<"<line x1='"<< x <<"' y1='"<< y <<"' x2='"<< x2 <<"' y2='"<< y2 <<"' stroke-dasharray = '10 10' stroke='black';stroke-width:4' />";
} }
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 << "' stroke='" << stroke << "' height='" << height << "' fill='" << fill << "' />\n";
cout << "<line x1='" << IMAGE_WIDTH <<"' y1='" << y << "' x2='" << IMAGE_WIDTH <<"' y2='" << y + height <<"' stroke-dasharray = '10 10' stroke='black'/>\n";
}
void
show_histogram_svg(const std::vector<size_t>& bins, size_t & max_count, size_t & bin_count, std::vector <double>& test)
{
const auto BLOCK_WIDTH = (IMAGE_WIDTH - TEXT_WIDTH)/max_count;
svg_begin(IMAGE_WIDTH+4, IMAGE_HEIGHT, test);
double top = 4;
svg_line(TEXT_LEFT, top, IMAGE_WIDTH+5, top, test); void show_histogram_svg(const vector<size_t>& bins) {
svg_line(TEXT_LEFT-10, top, TEXT_LEFT-10, top+3*BIN_HEIGHT, test); double max = 0, scale = 1;
svg_line(IMAGE_WIDTH+5, top, IMAGE_WIDTH+5, 2*top+3*BIN_HEIGHT, test); int maxlen = IMAGE_WIDTH - TEXT_WIDTH;
svg_line(TEXT_LEFT, 2*top+3*BIN_HEIGHT, IMAGE_WIDTH+5, 2*top+3*BIN_HEIGHT, test); for (auto el: bins) {
for (double t : test) if (max < el) {
{ max = el;
std:: cout << t<< '\n';
} }
for (size_t bin : bins) }
{ svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
double top = 5;
scale = max * BLOCK_WIDTH / maxlen;
for (size_t bin : bins) {
const double bin_width = BLOCK_WIDTH * bin; const double bin_width = BLOCK_WIDTH * bin;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin)); svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH+2, top+2, bin_width, BIN_HEIGHT); svg_rect(TEXT_WIDTH, top, bin_width / scale, BIN_HEIGHT);
top += BIN_HEIGHT; top += BIN_HEIGHT;
} }
svg_end(); svg_end(top, cout);
}
void
svg_end()
{
std::cout << "</svg>\n";
} }

@ -1,10 +1,3 @@
#pragma once
#include <iostream>
#include <vector> #include <vector>
void show_histogram_svg(const std::vector<size_t>& bins, size_t & max_count, size_t & bin_count, std::vector <double>& test); void show_histogram_svg(const std::vector<size_t>& bins);
void svg_begin(double width, double height, std::vector <double>& test);
void svg_text(double left, double baseline, std::string text);
void svg_rect(double x, double y, double width, double height, std::string stroke, std::string fill);
void svg_line(double x, double y, double width, double height, std::vector <double>& test);
void svg_end();

@ -1,34 +1,30 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include "text.h"
void show_histogram_text(std::vector<size_t>& bins, size_t & max_count,size_t & bin_count){ using namespace std;
const size_t SCREEN_WIDTH = 80; const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
if (max_count > MAX_ASTERISK){
std::vector<size_t> hights(bin_count);
for (size_t i = 0; i < bin_count; i++){
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
hights[i] = height;
}
for (size_t i = 0; i < bin_count; i++){ void show_histogram_text(const vector<size_t>& bins) {
printf("%3d|", bins[i]); size_t maxCount = bins[0];
for (size_t j = 0; j < hights[i]; j++){ for (auto bin : bins) {
std::cout<<"*"; if (maxCount < bin) {
maxCount = bin;
} }
std::cout<<std::endl;
} }
} for (auto bin : bins) {
else{ if (bin < 100)
for (size_t i = 0; i < bin_count; i++)
{ {
printf("%3d|", bins[i]); cout << " ";
for (size_t j = 0; j < bins[i]; j++){
std::cout<<"*";
} }
std::cout << std::endl; if (bin < 10)
{
cout << " ";
} }
size_t height = maxCount < MAX_ASTERISK ? bin : MAX_ASTERISK * (static_cast<double>(bin) / maxCount);
cout << bin << "|" << string(height, '*');
cout << "\n";
} }
} }

@ -1 +1,4 @@
#pragma once
#include <vector>
void show_histogram_text(const std::vector<size_t>& bins);

Загрузка…
Отмена
Сохранить