Сравнить коммиты
10 Коммитов
5905629649
...
06477600af
| Автор | SHA1 | Дата | |
|---|---|---|---|
| 06477600af | |||
| 8cfd8f2d8b | |||
| e45343aa23 | |||
| aefdf1bd3b | |||
| 25c09087a8 | |||
| db494506a5 | |||
| 08a8b250f5 | |||
| cebba585f3 | |||
| 047504ce42 | |||
| 783c55c6db |
7
.gitignore
поставляемый
7
.gitignore
поставляемый
@@ -1,3 +1,8 @@
|
|||||||
lab03.cbp
|
lab04.cbp
|
||||||
|
/curl
|
||||||
/bin
|
/bin
|
||||||
/obj
|
/obj
|
||||||
|
lab04.depend
|
||||||
|
lab04.layout
|
||||||
|
unittest.depend
|
||||||
|
unittest.cbp
|
||||||
|
|||||||
0
README.md
Обычный файл
0
README.md
Обычный файл
7106
doctest.h
Обычный файл
7106
doctest.h
Обычный файл
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
45
histogram.cpp
Обычный файл
45
histogram.cpp
Обычный файл
@@ -0,0 +1,45 @@
|
|||||||
|
#include "histogram.h"
|
||||||
|
|
||||||
|
vector<size_t> make_histogram(const vector<double> &numbers, size_t bin_count) {
|
||||||
|
double min, max;
|
||||||
|
bool res = false;
|
||||||
|
find_minmax(numbers, min, max, res);
|
||||||
|
double bin_size = ( max - min ) / bin_count;
|
||||||
|
vector<size_t> bins ( 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 )){
|
||||||
|
bins[j]++;
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ( !found ){
|
||||||
|
bins[bin_count-1]++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bins;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
find_minmax(const vector <double> &numbers, double &min, double &max, bool &res) {
|
||||||
|
|
||||||
|
if (numbers.size()==0){
|
||||||
|
res = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
min = numbers[0];
|
||||||
|
max = numbers[0];
|
||||||
|
for ( double x : numbers ){
|
||||||
|
if ( x < min ){
|
||||||
|
min = x;
|
||||||
|
} else if ( x > max ){
|
||||||
|
max = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
14
histogram.h
Обычный файл
14
histogram.h
Обычный файл
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef HISTOGRAM_H_INCLUDED
|
||||||
|
#define HISTOGRAM_H_INCLUDED
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
vector<size_t>
|
||||||
|
make_histogram(const std::vector<double>& numbers, size_t bin_count);
|
||||||
|
|
||||||
|
void
|
||||||
|
find_minmax(const vector <double> &numbers, double &min, double &max, bool &res);
|
||||||
|
|
||||||
|
#endif // HISTOGRAM_H_INCLUDED
|
||||||
8
histogram_internal.h
Обычный файл
8
histogram_internal.h
Обычный файл
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
|
#define HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void
|
||||||
|
find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
||||||
|
|
||||||
|
#endif // HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
166
main.cpp
166
main.cpp
@@ -1,5 +1,13 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "histogram.h"
|
||||||
|
#include "text.h"
|
||||||
|
#include "svg.h"
|
||||||
|
#include "show_svg.h"
|
||||||
|
#include <curl/curl.h>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
struct Input {
|
struct Input {
|
||||||
@@ -7,134 +15,84 @@ struct Input {
|
|||||||
size_t bin_count{};
|
size_t bin_count{};
|
||||||
};
|
};
|
||||||
|
|
||||||
Input
|
Input input_data(istream& stream, bool prompt) {
|
||||||
input_data() {
|
|
||||||
Input in;
|
|
||||||
|
|
||||||
size_t number_count;
|
size_t number_count;
|
||||||
cerr << "Enter quantity of numbers: ";
|
if (prompt) {
|
||||||
cin >> number_count;
|
cerr << "Enter number count: ";
|
||||||
|
}
|
||||||
|
stream >> number_count;
|
||||||
|
|
||||||
|
Input in;
|
||||||
|
|
||||||
vector<double> numbers(number_count);
|
|
||||||
in.numbers.resize(number_count);
|
in.numbers.resize(number_count);
|
||||||
|
|
||||||
|
if (prompt){
|
||||||
cerr << "Enter numbers: ";
|
cerr << "Enter numbers: ";
|
||||||
for (size_t i = 0; i < number_count; i++) {
|
}
|
||||||
cin >> in.numbers[i];
|
for (size_t i = 0; i < number_count; i++)
|
||||||
|
{
|
||||||
|
stream >> in.numbers[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
cerr << "Enter bin count: ";
|
if (prompt){
|
||||||
cin >> in.bin_count;
|
cerr << "Enter number of bins: ";
|
||||||
|
}
|
||||||
|
stream >> in.bin_count;
|
||||||
|
|
||||||
return in;
|
return in;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
size_t write_data(void* items, size_t item_size, size_t item_count, void* ctx) {
|
||||||
find_minmax(const vector<double> numbers, double& min, double& max) {
|
size_t data_size = item_size * item_count;
|
||||||
min = numbers[0];
|
stringstream* buffer = reinterpret_cast<stringstream*>(ctx);
|
||||||
max = numbers[0];
|
auto text = reinterpret_cast<const char*>(items);
|
||||||
for (double x : numbers) {
|
buffer->write(text, data_size);
|
||||||
if (x < min) {
|
return data_size;
|
||||||
min = x;
|
|
||||||
}
|
|
||||||
else if (x > max) {
|
|
||||||
max = x;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
vector <size_t>
|
Input download(const string& address) {
|
||||||
make_histogram(const vector<double> numbers, size_t bin_count) {
|
|
||||||
double min;
|
|
||||||
double max;
|
|
||||||
|
|
||||||
find_minmax(numbers, min, max);
|
stringstream buffer;
|
||||||
|
|
||||||
vector<size_t> bins(bin_count);
|
curl_global_init(CURL_GLOBAL_ALL);
|
||||||
double bin_size = (max - min) / bin_count;
|
|
||||||
for (size_t i = 0; i < numbers.size(); i++) {
|
CURL* curl = curl_easy_init();
|
||||||
bool found = false;
|
|
||||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++) {
|
if(curl) {
|
||||||
auto lo = min + j * bin_size;
|
|
||||||
auto hi = min + (j + 1) * bin_size;
|
CURLcode res;
|
||||||
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
|
curl_easy_setopt(curl, CURLOPT_URL, address.c_str());
|
||||||
bins[j]++;
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
|
||||||
found = true;
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
||||||
|
res = curl_easy_perform(curl);
|
||||||
|
|
||||||
|
if (res != CURLE_OK) {
|
||||||
|
curl_easy_strerror(res);
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
|
curl_easy_cleanup(curl);
|
||||||
}
|
}
|
||||||
if (!found) {
|
return input_data(buffer, false);
|
||||||
bins[bin_count - 1]++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return bins;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void show_histogram_text(const vector<size_t> bins, size_t bin_count) {
|
|
||||||
const size_t SCREEN_WIDTH = 80;
|
|
||||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
||||||
|
|
||||||
int max_count = 0;
|
|
||||||
for (int i =0; i<bin_count;i++){
|
|
||||||
if (bins[i]>max_count){
|
|
||||||
max_count=bins[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (max_count>MAX_ASTERISK){
|
|
||||||
for (int i=0; i<bin_count; i++){
|
|
||||||
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
|
|
||||||
if (bins[i]<10){
|
|
||||||
cout<<" "<<bins[i]<<"|";
|
|
||||||
for (int j=0;j<height;j++){
|
|
||||||
cout<<"*";
|
|
||||||
}
|
|
||||||
cout<<endl;
|
|
||||||
}
|
|
||||||
else if(bins[i]<100){
|
|
||||||
cout<<" "<<bins[i]<<"|";
|
|
||||||
for (int j=0;j<height;j++){
|
|
||||||
cout<<"*";
|
|
||||||
}
|
|
||||||
cout<<endl;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
cout<<bins[i]<<"|";
|
|
||||||
for (int j=0;j<height;j++){
|
|
||||||
cout<<"*";
|
|
||||||
}
|
|
||||||
cout<<endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
int main(int argc, char* argv[]){
|
||||||
|
|
||||||
|
Input in;
|
||||||
|
if (argc > 1) {
|
||||||
|
in = download(argv[1]);
|
||||||
} else {
|
} else {
|
||||||
for (int i=0; i<bin_count; i++){
|
in = input_data(cin, true);
|
||||||
if (bins[i]<10){
|
|
||||||
cout<<" "<<bins[i]<<"|";
|
|
||||||
for (int j=0;j<bins[i];j++){
|
|
||||||
cout<<"*";
|
|
||||||
}
|
|
||||||
cout<<endl;
|
|
||||||
}
|
|
||||||
else if(bins[i]<100){
|
|
||||||
cout<<" "<<bins[i]<<"|";
|
|
||||||
for (int j=0;j<bins[i];j++){
|
|
||||||
cout<<"*";
|
|
||||||
}
|
|
||||||
cout<<endl;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
cout<<bins[i]<<"|";
|
|
||||||
for (int j=0;j<bins[i];j++){
|
|
||||||
cout<<"*";
|
|
||||||
}
|
|
||||||
cout<<endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
main(){
|
|
||||||
auto in = input_data();
|
|
||||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||||
show_histogram_text(bins, in.bin_count);
|
|
||||||
|
show_histogram_svg(bins);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
38
show_histogram.cpp
Обычный файл
38
show_histogram.cpp
Обычный файл
@@ -0,0 +1,38 @@
|
|||||||
|
#include "svg.h"
|
||||||
|
|
||||||
|
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 BLOCK_WIDTH = 10;
|
||||||
|
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
||||||
|
double top = 0;
|
||||||
|
size_t longest;
|
||||||
|
if (!bins.empty()) {
|
||||||
|
longest = bins[0];
|
||||||
|
} else {
|
||||||
|
longest = 0;
|
||||||
|
}
|
||||||
|
for (size_t bin : bins) {
|
||||||
|
if (bin > longest) {
|
||||||
|
longest = bin;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (size_t bin : bins) {
|
||||||
|
const double bin_width = BLOCK_WIDTH * bin;
|
||||||
|
if (bin == longest){
|
||||||
|
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
||||||
|
svg_rect(TEXT_WIDTH, top, IMAGE_WIDTH - TEXT_WIDTH, BIN_HEIGHT, "#000000", "#ff00a2");
|
||||||
|
top += BIN_HEIGHT;
|
||||||
|
} else{
|
||||||
|
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
||||||
|
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "#000000", "#ff00a2");
|
||||||
|
top += BIN_HEIGHT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
svg_end();
|
||||||
|
}
|
||||||
10
show_svg.h
Обычный файл
10
show_svg.h
Обычный файл
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef SHOW_SVG_H_INCLUDED
|
||||||
|
#define SHOW_SVG_H_INCLUDED
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_svg(const vector<size_t>& bins);
|
||||||
|
|
||||||
|
#endif // SHOW_SVG_H_INCLUDED
|
||||||
30
svg.cpp
Обычный файл
30
svg.cpp
Обычный файл
@@ -0,0 +1,30 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include "svg.h"
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
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";
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
svg_end() {
|
||||||
|
cout << "</svg>\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
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){
|
||||||
|
cout << "<rect x='"<< x <<"' y='"<< y <<"' width='"<< width <<"' height='"<< height <<"' stroke='"<< stroke <<"' fill='"<< fill <<"' />";
|
||||||
|
}
|
||||||
13
svg.h
Обычный файл
13
svg.h
Обычный файл
@@ -0,0 +1,13 @@
|
|||||||
|
#ifndef SVG_H_INCLUDED
|
||||||
|
#define SVG_H_INCLUDED
|
||||||
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
void svg_begin(double width, double height);
|
||||||
|
|
||||||
|
void svg_end();
|
||||||
|
|
||||||
|
void svg_text(double left, double baseline, string text);
|
||||||
|
|
||||||
|
void svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black");;
|
||||||
|
#endif // SVG_H_INCLUDED
|
||||||
35
text.cpp
Обычный файл
35
text.cpp
Обычный файл
@@ -0,0 +1,35 @@
|
|||||||
|
#include "text.h"
|
||||||
|
#include <cstdio>
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
void
|
||||||
|
show_histogram_text(vector<size_t>& bins, size_t & max_count,size_t & bin_count) {
|
||||||
|
const size_t SCREEN_WIDTH = 80;
|
||||||
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||||
|
if (max_count > MAX_ASTERISK){
|
||||||
|
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++){
|
||||||
|
cout<<"*";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
for (size_t i = 0; i < bin_count; i++)
|
||||||
|
{
|
||||||
|
printf("%3d|", bins[i]);
|
||||||
|
for (size_t j = 0; j < bins[i]; j++){
|
||||||
|
cout<<"*";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
11
text.h
Обычный файл
11
text.h
Обычный файл
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef SHOW-SVG_H_INCLUDED
|
||||||
|
#define SHOW-SVG_H_INCLUDED
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_text(vector<size_t>& bins, size_t & max_count,size_t & bin_count);
|
||||||
|
|
||||||
|
#endif // SHOW-SVG_H_INCLUDED
|
||||||
40
unittest.cpp
Обычный файл
40
unittest.cpp
Обычный файл
@@ -0,0 +1,40 @@
|
|||||||
|
#define DOCTEST_CONFIG_NO_MULTITHREADING
|
||||||
|
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||||
|
#include "doctest.h"
|
||||||
|
#include "histogram_internal.h"
|
||||||
|
|
||||||
|
TEST_CASE("distinct positive numbers") {
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({}, min, max);
|
||||||
|
CHECK(min == 0);
|
||||||
|
CHECK(max == 0);
|
||||||
|
}
|
||||||
|
TEST_CASE("distinct positive numbers") {
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({1, 2}, min, max);
|
||||||
|
CHECK(min == 1);
|
||||||
|
CHECK(max == 2);
|
||||||
|
}
|
||||||
|
TEST_CASE("distinct positive numbers") {
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({1}, min, max);
|
||||||
|
CHECK(min == 1);
|
||||||
|
CHECK(max == 1);
|
||||||
|
}
|
||||||
|
TEST_CASE("distinct positive numbers") {
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({1,1,1,1,1,1,1,1,1}, min, max);
|
||||||
|
CHECK(min == 1);
|
||||||
|
CHECK(max == 1);
|
||||||
|
}
|
||||||
|
TEST_CASE("distinct positive numbers") {
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({-1.5,-2,-3,-4}, min, max);
|
||||||
|
CHECK(min == -4);
|
||||||
|
CHECK(max == -1.5);
|
||||||
|
}
|
||||||
Ссылка в новой задаче
Block a user