code: Два задания 9 и 11
Этот коммит содержится в:
@@ -13,6 +13,7 @@
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-g" />
|
||||
<Add directory="curl-8.7.1_9-win64-mingw/include" />
|
||||
</Compiler>
|
||||
</Target>
|
||||
<Target title="Release">
|
||||
@@ -37,7 +38,6 @@
|
||||
<Add library="libcurl.dll.a" />
|
||||
<Add directory="curl-8.7.1_9-win64-mingw/lib" />
|
||||
</Linker>
|
||||
<Unit filename="doctest.h" />
|
||||
<Unit filename="histogram.cpp" />
|
||||
<Unit filename="histogram.h" />
|
||||
<Unit filename="histogram_internal.h" />
|
||||
|
||||
@@ -1,46 +1,43 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "histogram.h"
|
||||
void find_minmax(const std::vector<double>& numbers, double& min, double& max) {
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (double x : numbers)
|
||||
{
|
||||
if (x < min){
|
||||
min = x;
|
||||
#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];
|
||||
max = numbers[0];
|
||||
for (auto el : numbers) {
|
||||
if (el > max) {
|
||||
max = el;
|
||||
}
|
||||
if (el < min) {
|
||||
min = el;
|
||||
}
|
||||
}
|
||||
else if (x > max){
|
||||
max = x;
|
||||
}
|
||||
}
|
||||
} else {min = 0;}
|
||||
}
|
||||
|
||||
|
||||
std::vector <size_t> make_histogram(const std::vector<double>&numbers, size_t &bin_count, size_t &number_count, size_t &max_count) {
|
||||
double min, max;
|
||||
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) {
|
||||
vector<size_t> bins(bin_count);
|
||||
double max, min;
|
||||
find_minmax(numbers, min, max);
|
||||
double bin_size = (max - min) / bin_count;
|
||||
std::vector<size_t> bins(bin_count);
|
||||
max_count = bins[0];
|
||||
for (size_t i = 0; i < number_count; i++){
|
||||
double binSize = (max - min) / bin_count;
|
||||
for (size_t i = 0; i < numbers.size(); 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 ((lo <= numbers[i]) && (numbers[i] < hi)){
|
||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++) {
|
||||
auto lo = min + j * binSize;
|
||||
auto hi = min + (j + 1) * binSize;
|
||||
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
if (bins[j] > max_count){
|
||||
max_count = bins[j];
|
||||
}
|
||||
}
|
||||
if (!found){
|
||||
if (!found) {
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
if (bins[bin_count - 1] > max_count){
|
||||
max_count = bins[bin_count - 1];
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
std::vector<size_t>
|
||||
make_histogram(const std::vector<double>&numbers, size_t & bin_count, size_t & number_count, size_t & max_count);
|
||||
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count);
|
||||
|
||||
87
main.cpp
87
main.cpp
@@ -3,93 +3,70 @@
|
||||
#include "histogram.h"
|
||||
#include "text.h"
|
||||
#include "svg.h"
|
||||
#include <curl/curl.h>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <curl/curl.h>
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
struct Input
|
||||
{
|
||||
struct Input {
|
||||
vector<double> numbers;
|
||||
size_t bin_count{};
|
||||
size_t number_count{};
|
||||
size_t max_count{};
|
||||
};
|
||||
|
||||
Input
|
||||
input_data(istream& stream, bool prompt)
|
||||
{
|
||||
size_t number_count;
|
||||
if (prompt==true)
|
||||
{
|
||||
cerr << "Enter number count: ";
|
||||
}
|
||||
stream >> number_count;
|
||||
Input input_data(istream& stream) {
|
||||
Input in;
|
||||
if (prompt==true)
|
||||
{
|
||||
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++)
|
||||
{
|
||||
size_t number_count;
|
||||
stream >> number_count;
|
||||
in.numbers.resize(number_count);
|
||||
for (size_t i = 0; i < number_count; i++) {
|
||||
stream >> in.numbers[i];
|
||||
}
|
||||
size_t bin_count;
|
||||
size_t max_count;
|
||||
in.max_count = 0;
|
||||
stream >> in.bin_count;
|
||||
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;
|
||||
stringstream* buffer = reinterpret_cast<stringstream*>(ctx);
|
||||
auto text = reinterpret_cast<const char*>(items);
|
||||
buffer->write(text, data_size);
|
||||
buffer->write(reinterpret_cast<const char*>(items), data_size);
|
||||
return data_size;
|
||||
}
|
||||
Input download(const string& address)
|
||||
{
|
||||
|
||||
|
||||
Input download(const string& address) {
|
||||
stringstream buffer;
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
CURL* curl = curl_easy_init();
|
||||
if(curl)
|
||||
{
|
||||
if (curl) {
|
||||
CURLcode res;
|
||||
curl_easy_setopt(curl, CURLOPT_URL, address.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
||||
res = curl_easy_perform(curl);
|
||||
curl_easy_cleanup(curl);
|
||||
if (res != 0)
|
||||
{
|
||||
cout << res;
|
||||
if (res != CURLE_OK) {
|
||||
cout << curl_easy_strerror(res);
|
||||
exit(1);
|
||||
}
|
||||
curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW);
|
||||
for (size_t i = 0; curl_info->protocols != NULL; i++){
|
||||
cerr << curl_info->protocols[i] << "\n";
|
||||
auto data = curl_version_info(CURLVERSION_NOW)->protocols;
|
||||
for (auto protocol = data; *protocol; ++protocol) {
|
||||
cerr << *protocol << endl;
|
||||
}
|
||||
curl_easy_cleanup(curl);
|
||||
}
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
return input_data(buffer, false);
|
||||
}
|
||||
return input_data(buffer);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]){
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
Input in;
|
||||
if (argc > 1) {
|
||||
in = download(argv[1]);
|
||||
} 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);
|
||||
vector<double> test;
|
||||
show_histogram_svg(bins, in.max_count, in.bin_count, test);
|
||||
return 0;
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
show_histogram_svg(bins);
|
||||
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;
|
||||
}
|
||||
|
||||
92
svg.cpp
92
svg.cpp
@@ -1,77 +1,61 @@
|
||||
#include "iostream"
|
||||
#include <iostream>
|
||||
#include "svg.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
const auto IMAGE_WIDTH = 400;
|
||||
const auto IMAGE_HEIGHT = 300;
|
||||
const auto IMAGE_HEIGHT = 310;
|
||||
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;
|
||||
|
||||
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";
|
||||
cout << "<line x1='10' y1='2' x2='" << width + 10 <<"' y2='2' stroke-dasharray = '10 10' stroke='black'/>\n";
|
||||
|
||||
void svg_begin(double width, double height, std::vector <double>& test)
|
||||
{
|
||||
test.push_back(width);
|
||||
test.push_back(height);
|
||||
std::cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
|
||||
std::cout << "<svg ";
|
||||
std::cout << "width='" << width << "' ";
|
||||
std::cout << "height='" << height << "' ";
|
||||
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_end(double top, ostream& stream) {
|
||||
stream << "<line x1='10' y1='" << top << "' x2='" << IMAGE_WIDTH <<"' y2='" << top << "' stroke-dasharray = '10 10' stroke='black'/>\n";
|
||||
stream << "</svg>\n";
|
||||
}
|
||||
|
||||
void svg_rect(double x, double y, double width, double height, std::string stroke = "black", std::string fill = "red")
|
||||
{
|
||||
std::cout<< "<rect x='"<< x <<"' y='"<< y <<"' width='"<< width <<"' height='"<< height<<"' stroke='black' fill='red' ></rect>";
|
||||
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";
|
||||
cout << "<text x='" << left << "' y='" << baseline << "'>" << text <<"</text>\n";
|
||||
}
|
||||
|
||||
void svg_line(double x, double y, double x2, double y2, std::vector <double>& test)
|
||||
{
|
||||
test.push_back(x);
|
||||
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);
|
||||
svg_line(TEXT_LEFT-10, top, TEXT_LEFT-10, top+3*BIN_HEIGHT, test);
|
||||
svg_line(IMAGE_WIDTH+5, top, IMAGE_WIDTH+5, 2*top+3*BIN_HEIGHT, test);
|
||||
svg_line(TEXT_LEFT, 2*top+3*BIN_HEIGHT, IMAGE_WIDTH+5, 2*top+3*BIN_HEIGHT, test);
|
||||
for (double t : test)
|
||||
{
|
||||
std:: cout << t<< '\n';
|
||||
void show_histogram_svg(const vector<size_t>& bins) {
|
||||
double max = 0, scale = 1;
|
||||
int maxlen = IMAGE_WIDTH - TEXT_WIDTH;
|
||||
for (auto el: bins) {
|
||||
if (max < el) {
|
||||
max = el;
|
||||
}
|
||||
}
|
||||
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;
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin));
|
||||
svg_rect(TEXT_WIDTH+2, top+2, bin_width, BIN_HEIGHT);
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
||||
svg_rect(TEXT_WIDTH, top, bin_width / scale, BIN_HEIGHT);
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
svg_end();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void
|
||||
svg_end()
|
||||
{
|
||||
std::cout << "</svg>\n";
|
||||
svg_end(top, cout);
|
||||
}
|
||||
|
||||
9
svg.h
9
svg.h
@@ -1,10 +1,3 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#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 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();
|
||||
void show_histogram_svg(const std::vector<size_t>& bins);
|
||||
|
||||
40
text.cpp
40
text.cpp
@@ -1,34 +1,30 @@
|
||||
#include <iostream>
|
||||
#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 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++){
|
||||
printf("%3d|", bins[i]);
|
||||
for (size_t j = 0; j < hights[i]; j++){
|
||||
std::cout<<"*";
|
||||
}
|
||||
std::cout<<std::endl;
|
||||
void show_histogram_text(const vector<size_t>& bins) {
|
||||
size_t maxCount = bins[0];
|
||||
for (auto bin : bins) {
|
||||
if (maxCount < bin) {
|
||||
maxCount = bin;
|
||||
}
|
||||
}
|
||||
else{
|
||||
for (size_t i = 0; i < bin_count; i++)
|
||||
for (auto bin : bins) {
|
||||
if (bin < 100)
|
||||
{
|
||||
printf("%3d|", bins[i]);
|
||||
for (size_t j = 0; j < bins[i]; j++){
|
||||
std::cout<<"*";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
cout << " ";
|
||||
}
|
||||
if (bin < 10)
|
||||
{
|
||||
cout << " ";
|
||||
}
|
||||
size_t height = maxCount < MAX_ASTERISK ? bin : MAX_ASTERISK * (static_cast<double>(bin) / maxCount);
|
||||
cout << bin << "|" << string(height, '*');
|
||||
cout << "\n";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
3
text.h
3
text.h
@@ -1 +1,4 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
void show_histogram_text(const std::vector<size_t>& bins);
|
||||
|
||||
Ссылка в новой задаче
Block a user