Сommit
f5b2b93109
@ -0,0 +1,60 @@
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
#include <vector>
|
||||
#include "histogram.h"
|
||||
using namespace std;
|
||||
|
||||
bool
|
||||
find_minmax(const vector<double>& numbers, double& min, double& max)
|
||||
{
|
||||
if (numbers.size() == 0) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
max = numbers[0];
|
||||
min = numbers[0];
|
||||
}
|
||||
for(size_t i = 0; i < numbers.size(); i++)
|
||||
{
|
||||
if (numbers[i] > max) max = numbers[i];
|
||||
if (numbers[i] < min) min = numbers[i];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
vector<size_t> make_histogram (const vector<double>& numbers, size_t &bin_count)
|
||||
{
|
||||
vector <size_t> bins(bin_count);
|
||||
|
||||
double min, max;
|
||||
float low, hi;
|
||||
|
||||
if (!find_minmax(numbers, min, max))
|
||||
cout << "error in find_minmax: empty vector";
|
||||
|
||||
|
||||
double bin_size = (max - min) / bin_count;
|
||||
low = min;
|
||||
hi = low + bin_size;
|
||||
|
||||
for (size_t i = 0; i < numbers.size(); i++)
|
||||
{
|
||||
bool flag = false;
|
||||
|
||||
for (size_t j = 0; (j < bin_count - 1) && !flag; j++)
|
||||
{
|
||||
low = min + j * bin_size;
|
||||
hi = min + (j + 1) * bin_size;
|
||||
if ((low <= numbers[i]) && (numbers[i] < hi))
|
||||
{
|
||||
bins[j]++;
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
if (flag==false)
|
||||
{
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
;std::vector<size_t>
|
||||
make_histogram(const std::vector<double> &numbers, size_t &bin_count);
|
@ -0,0 +1,47 @@
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include "histogram.h"
|
||||
#include "text.h"
|
||||
#include "svg.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Input
|
||||
{
|
||||
vector<double> numbers;
|
||||
size_t bin_count{};
|
||||
};
|
||||
|
||||
Input
|
||||
input_data()
|
||||
{
|
||||
|
||||
size_t number_count;
|
||||
cerr << "count=";
|
||||
cin >> number_count;
|
||||
|
||||
Input in;
|
||||
|
||||
in.numbers.resize(number_count);
|
||||
|
||||
for (size_t i = 0; i < number_count; i++)
|
||||
{
|
||||
cin >> in.numbers[i];
|
||||
}
|
||||
|
||||
cerr << "bin_count= ";
|
||||
cin >> in.bin_count;
|
||||
|
||||
return in;
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
auto in = input_data();
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
show_histogram_svg(bins);
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <math.h>
|
||||
#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<<"' />";
|
||||
|
||||
}
|
||||
|
||||
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(400, 300);
|
||||
|
||||
double top = 0;
|
||||
const size_t MAX_ASTERISK = IMAGE_WIDTH - TEXT_WIDTH;
|
||||
int maxb = bins[0];
|
||||
int color_value;
|
||||
|
||||
for (size_t j = 1; j < bins.size(); j++)
|
||||
{
|
||||
if (bins[j] > maxb) maxb = bins[j];
|
||||
}
|
||||
|
||||
int maxb1 = maxb * BLOCK_WIDTH;
|
||||
|
||||
for (size_t bin : bins) {
|
||||
|
||||
double bin_width;
|
||||
|
||||
|
||||
if (maxb1 > MAX_ASTERISK)
|
||||
{
|
||||
bin_width = BLOCK_WIDTH * MAX_ASTERISK * (bin / maxb1);
|
||||
}
|
||||
else bin_width = BLOCK_WIDTH * bin;
|
||||
|
||||
color_value = (10 - (bin * 9) / maxb);
|
||||
string color = "#" + to_string(color_value) + to_string(color_value) + to_string(color_value);
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "violet", color);
|
||||
top += BIN_HEIGHT;
|
||||
|
||||
}
|
||||
|
||||
svg_end();
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
void
|
||||
color_find(vector<size_t>& bins, int maxb, int& color);
|
||||
|
||||
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, string fill);
|
||||
|
||||
void
|
||||
show_histogram_svg(const vector<size_t>& bins);
|
@ -0,0 +1,34 @@
|
||||
#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 bin : bins)
|
||||
if (bin > max_bin)
|
||||
max_bin = bin;
|
||||
for (size_t bin : bins)
|
||||
{
|
||||
int 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 (int i = 0; i < height; i++)
|
||||
cout << "*";
|
||||
cout << endl;
|
||||
}
|
||||
return;
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
void
|
||||
show_histogram_text(std::vector<size_t>& bins, size_t &bin_count);
|
Загрузка…
Ссылка в новой задаче