Родитель
8e1147a2a1
Сommit
05006f1fe2
@ -0,0 +1,39 @@
|
||||
#include "histogram.h"
|
||||
#include <vector>
|
||||
#include "histogram_internal.h"
|
||||
using namespace std;
|
||||
|
||||
void
|
||||
find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (auto x : numbers) {
|
||||
if (x > max)
|
||||
max = x;
|
||||
if (x < min)
|
||||
min = x;
|
||||
}
|
||||
}
|
||||
|
||||
vector<size_t>
|
||||
make_histogram(const vector<double>& numbers, size_t bin_count) {
|
||||
double max, min;
|
||||
find_minmax(numbers, min, max);
|
||||
vector<size_t> bins(bin_count);
|
||||
double bin_size = ((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)) {
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
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,6 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
void
|
||||
find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
||||
|
@ -0,0 +1,52 @@
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#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;
|
||||
Input in;
|
||||
cerr << "Enter number_count ";
|
||||
cin >> number_count;
|
||||
|
||||
in.numbers.resize(number_count);
|
||||
cerr << "Enter numbers ";
|
||||
for (int i = 0; i < number_count;i++) {
|
||||
cin >> in.numbers[i];
|
||||
}
|
||||
|
||||
cerr << "Enter bin count \n";
|
||||
cin >> in.bin_count;
|
||||
|
||||
return in;
|
||||
}
|
||||
|
||||
void
|
||||
find_minmax(const vector<double>& numbers, double& min, double& max);
|
||||
|
||||
vector<size_t>
|
||||
make_histogram(const vector<double>& numbers, size_t bin_count);
|
||||
|
||||
void
|
||||
show_histogram_text(vector<size_t> bins);
|
||||
|
||||
void
|
||||
show_histogram_svg(const vector<size_t>& bins);
|
||||
|
||||
int main() {
|
||||
Input in = input_data();
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
show_histogram_svg(bins);
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
#include <iostream>
|
||||
#include "svg.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
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;
|
||||
|
||||
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>\n";
|
||||
}
|
||||
|
||||
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 << "' />\n";
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
show_histogram_svg(const vector<size_t>& bins) {
|
||||
svg_begin(2000, 2000);
|
||||
double top = 0;
|
||||
size_t maxb = 0;
|
||||
for (auto bin : bins) {
|
||||
if (maxb < bin) {
|
||||
maxb = bin;
|
||||
}
|
||||
}
|
||||
int k = (IMAGE_WIDTH - TEXT_WIDTH) / maxb * BLOCK_WIDTH;
|
||||
|
||||
if (k > 1) k = 1;
|
||||
for (size_t bin : bins) {
|
||||
const double bin_width = BLOCK_WIDTH * bin;
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", "#87CEEB");
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
svg_end();
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
void
|
||||
show_histogram_svg(const std::vector<size_t>& bins);
|
||||
|
@ -0,0 +1,43 @@
|
||||
#include "text.h"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
|
||||
void
|
||||
show_histogram_text(vector<size_t> bins) {
|
||||
size_t maxb = 0;
|
||||
for (int i = 0;i < bins.size();i++) {
|
||||
if (bins[i] > maxb) {
|
||||
maxb = bins[i];
|
||||
}
|
||||
}
|
||||
|
||||
double k = double(MAX_ASTERISK) / maxb;
|
||||
if (k > 1) {
|
||||
k = 1;
|
||||
|
||||
}
|
||||
vector<size_t> bin_graph(bins.size());
|
||||
for (size_t bin = 0; bin < bins.size(); bin++) {
|
||||
bin_graph[bin] = int(double(bins[bin] * k));
|
||||
|
||||
}
|
||||
for (size_t i = 0; i < bins.size(); i++) {
|
||||
if (bins[i] < 100) {
|
||||
cout << " ";
|
||||
}
|
||||
if (bins[i] < 10) {
|
||||
cout << " ";
|
||||
}
|
||||
cout << bins[i] << '|';
|
||||
for (size_t j = 0;j < bin_graph[i];j++) {
|
||||
cout << "*";
|
||||
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
void
|
||||
show_histogram_text(std::vector<size_t> bins);
|
Загрузка…
Ссылка в новой задаче