Сравнить коммиты
10 Коммитов
5905629649
...
06477600af
Автор | SHA1 | Дата |
---|---|---|
|
06477600af | 11 месяцев назад |
|
8cfd8f2d8b | 11 месяцев назад |
|
e45343aa23 | 11 месяцев назад |
|
aefdf1bd3b | 11 месяцев назад |
|
25c09087a8 | 11 месяцев назад |
|
db494506a5 | 12 месяцев назад |
|
08a8b250f5 | 12 месяцев назад |
|
cebba585f3 | 12 месяцев назад |
|
047504ce42 | 12 месяцев назад |
|
783c55c6db | 12 месяцев назад |
@ -1,3 +1,8 @@
|
|||||||
lab03.cbp
|
lab04.cbp
|
||||||
|
/curl
|
||||||
/bin
|
/bin
|
||||||
/obj
|
/obj
|
||||||
|
lab04.depend
|
||||||
|
lab04.layout
|
||||||
|
unittest.depend
|
||||||
|
unittest.cbp
|
||||||
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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
|
@ -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
|
@ -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();
|
||||||
|
}
|
@ -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
|
@ -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 <<"' />";
|
||||||
|
}
|
@ -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
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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
|
@ -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);
|
||||||
|
}
|
Загрузка…
Ссылка в новой задаче