MachulinaDV 2 лет назад
Родитель 20778a68c1
Сommit 2be18e6286

@ -0,0 +1,68 @@
#include <iostream>
#include <vector>
#include <math.h>
#include "histogram.h"
using namespace std;
bool find_minmax(const vector<double>& A, double& min, double& max)
{
bool empt;
if (A.size() != 0)
{
min = A[0];
for (auto i = 0; i < A.size(); i++)
{
if (A[i] < min)
{
min = A[i];
}
}
max = A[0];
for (auto i = 0; i < A.size(); i++)
{
if (A[i] > max)
{
max = A[i];
}
}
empt =true;
}
else
empt = false;
return empt;
}
vector <size_t> make_histogram(const vector<double>& A, size_t bin)
{
vector<size_t>B(bin);
size_t max_count;
double max, min;
bool empt;
find_minmax(A, min, max);
double step = (max - min) / (bin);
for (size_t i = 0; i < A.size(); i++)
{
for (size_t j = 0; j < bin; j++)
{
if ((A[i] >= (min + j * step)) && (A[i] < (min + (j + 1)*step)))
{
B[j]++;
break;
}
}
}
for (size_t i = 0; i < A.size(); i++)
{
if (A[i] == max)
B[bin - 1]++;
}
return B;
}

@ -0,0 +1,9 @@
#ifndef HISTOGRAM_H_INCLUDED
#define HISTOGRAM_H_INCLUDED
#include <vector>
std::vector<size_t>
make_histogram(const std::vector<double>& A, size_t bin);
#endif // HISTOGRAM_H_INCLUDED

@ -0,0 +1,42 @@
#include <iostream>
#include <vector>
#include <math.h>
#include "histogram.h"
#include "text.h"
#include "svg.h"
using namespace std;
struct Input {
vector<double>A;
size_t bin{};
};
Input
input_data()
{
size_t n;
cerr<<"Marks: ";
cin>>n;
Input in;
in.A.resize(n);
for (size_t i=0; i<n; i++)
{
cerr<<"A["<<i<<"]=";
cin>>in.A[i];
}
cerr<<"Rows: ";
cin>>in.bin;
return in;
}
int main()
{
Input in = input_data();
auto B = make_histogram(in.A, in.bin);
show_histogram_svg (B);
return 0;
}

@ -0,0 +1,64 @@
#include <math.h>
#include <iostream>
#include <conio.h>
#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 opacity , string stroke, string fill) {
cout << "<rect x='"<<x<<"' y='"<<y<<"' width='"<<width<<"' height='"<<height<<"' fill-opacity= '"<< opacity<< "' stroke='"<<stroke<<"' fill='"<<fill<<"' />";
}
void
show_histogram_svg(const vector<size_t>& B) {
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 MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH;
svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT);
double top = 0;
double max_count = B[0];
for (size_t i = 0; i < B.size(); i++) {
if (B[i] > max_count)
max_count = B[i];
}
for (size_t bin : B) {
double opacity = (bin / max_count);
const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH)*(bin/max_count);
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, to_string(opacity), "blue", "#9BB0D2");
top += BIN_HEIGHT;
}
svg_end();
}

@ -0,0 +1,8 @@
#ifndef SVG_H_INCLUDED
#define SVG_H_INCLUDED
#include <vector>
void
show_histogram_svg(const std::vector<size_t>& B);
#endif // SVG_H_INCLUDED

@ -0,0 +1,29 @@
#include <iostream>
#include <vector>
#include <math.h>
#include "text.h"
using namespace std;
void show_histogram(vector<size_t>B, size_t bin)
{
for (size_t i = 0; i < bin; i++)
{
if (B[i] < 10)
{
cout << " ";
}
else if (B[i] < 100)
{
cout << " ";
}
cout << B[i] << "|";
for (size_t j = 0; j < B[i]; j++)
{
cout << "*";
}
cout << endl;
}
return;
}

@ -0,0 +1,6 @@
#pragma once
#include <vector>
void show_histogram(std::vector<size_t>B, size_t bin);
Загрузка…
Отмена
Сохранить