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

...

2 Коммитов

Автор SHA1 Сообщение Дата
bich a19e2ec179 chup2
1 месяц назад
bich a8255653ea chup
1 месяц назад

@ -0,0 +1,44 @@
#include <iostream>
#include <vector>
#include "text.h"
using namespace std;
void show_histogram_text(vector<size_t> bins, size_t bin_count){
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
size_t max_bin = bins[0];
for(size_t i = 0; i < bin_count; i++)
{
if(bins[i] > max_bin)
{
max_bin = bins[i];
}
}
for (size_t bin: bins)
{
size_t height = bin;
if (max_bin > MAX_ASTERISK)
{
height = MAX_ASTERISK * (static_cast<double>(bin) / max_bin);
}
if (bin < 100)
{
cout << ' ';
}
if (bin < 10)
{
cout << ' ';
}
cout << bin << "|";
for(size_t i = 0; i < height; i++)
{
cout << "*";
}
cout << endl;
}
}

@ -0,0 +1,41 @@
#include "histogram.h"
#include "histogram_internal.h"
bool find_minmax(vector<double> vec, double& min, double& max) {
if(vec.size() == 0){
return false;
}
min = vec[0];
max = vec[0];
for (double x : vec) {
if (x < min) {
min = x;
}
else if (x > max)
{
max = x;
}
}
return true;
}
vector<size_t> make_histogram(size_t number, vector<double> vec) {
vector<size_t> bins(number);
double mn, mx;
find_minmax(vec, mn, mx);
float bin_size = (mx - mn) / number;
for (size_t i = 0; i < vec.size(); i++) {
bool fl = false;
for (size_t j = 0; (j < number - 1) && !fl; j++) {
auto lo = mn + j * bin_size;
auto hi = mn + (j + 1) * bin_size;
if ((lo <= vec[i]) && (vec[i] < hi)) {
bins[j]++;
fl = true;
}
}
if (!fl) {
bins[number - 1]++;
}
}
return bins;
}

@ -0,0 +1,4 @@
#include <vector>
#include <iostream>
using namespace std;
vector<size_t> make_histogram(size_t number, vector<double> vec);

@ -0,0 +1 @@
bool find_minmax(const std::vector<double> vec, double& min, double& max);

@ -1,9 +1,25 @@
#include <iostream>
#include "histogram.h"
#include "text.h"
#include "svg.h"
struct Input {
vector<double> vec;
size_t korz{};
};
Input input_data() {
Input in;
size_t n, korz;
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
cerr << "Number of elem ";
cin >> n;
in.vec.resize(n);
for (size_t i = 0; i < n; i++)
cin >> in.vec[i];
cerr << "Enter bin count: ";
cin >> in.korz;
return in;
}
int main() {
auto in = input_data();
auto bins = make_histogram(in.korz, in.vec);
show_histogram_svg(bins);
}

@ -0,0 +1,77 @@
#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 = "black", string fill = "black")
{
cout << "<rect x='"<<x<<"' y='"<<y<<"' width='"<<width<<"' height='"<<height<<"' stroke='"<<stroke<<"' fill='"<<fill<<"' />";
}
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;
const auto BLACK = "black";
const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH;
svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT);
double top = 0;
double max_count = bins[0];
for (size_t i = 0; i < bins.size(); i++)
{
if (max_count<bins[i])
{
max_count=bins[i];
}
}
for (size_t bin : bins)
{
const auto RED = "#" + std::to_string(int(ceil(10 - (bin * 9) / max_count))) + std::to_string(int(ceil(10 - (bin * 9) / max_count))) + std::to_string(int(ceil(10 - (bin * 9) / max_count)));
double bin_width = (MAX_WIDTH)*(bin/max_count);
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, BLACK,RED);
top += BIN_HEIGHT;
}
svg_end();
}

@ -0,0 +1,6 @@
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
using namespace std;
void show_histogram_svg(const vector<size_t>& bins);

@ -0,0 +1,44 @@
#include <iostream>
#include <vector>
#include "text.h"
using namespace std;
void show_histogram_text(vector<size_t> bins, size_t bin_count){
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
size_t max_bin = bins[0];
for(size_t i = 0; i < bin_count; i++)
{
if(bins[i] > max_bin)
{
max_bin = bins[i];
}
}
for (size_t bin: bins)
{
size_t height = bin;
if (max_bin > MAX_ASTERISK)
{
height = MAX_ASTERISK * (static_cast<double>(bin) / max_bin);
}
if (bin < 100)
{
cout << ' ';
}
if (bin < 10)
{
cout << ' ';
}
cout << bin << "|";
for(size_t i = 0; i < height; i++)
{
cout << "*";
}
cout << endl;
}
}

@ -0,0 +1,3 @@
#include <iostream>
#include <vector>
void show_histogram_text(std::vector<size_t> bins);

@ -0,0 +1,37 @@
#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({1, 2}, min, max);
CHECK(min == 1);
CHECK(max == 2);
}
TEST_CASE("distinct negative numbers"){
double min = 0;
double max = 0;
find_minmax({-1, -2}, min, max);
CHECK(min == -2);
CHECK(max == -1);
}
TEST_CASE("vector of the same elements"){
double min = 0;
double max = 0;
find_minmax({3,3,3}, min, max);
CHECK(min == 3);
CHECK(max == 3);
}
TEST_CASE("empty vector"){
double min = 0;
double max = 0;
CHECK(!find_minmax({}, min, max));
}
TEST_CASE("vector of one elements"){
double min = 0;
double max = 0;
find_minmax({3}, min, max);
CHECK(min == max);
}
Загрузка…
Отмена
Сохранить