main
TekotovaVA 2 лет назад
Родитель 260247b455
Сommit 3fe4023a61

@ -1,65 +1,29 @@
#include "text.h"
#include "histogram.h"
#include "svg.h"
#include "histogram_internal.h"
#include <iostream>
#include <vector>
using namespace std;
int main()
struct Input {
vector<double> numbers;
size_t bin_count{};
};
Input input_data()
{
size_t num, po, i;
double max, min, m = 0, le, ri;
cout << "num=";
cin >> num;
cout << "po=";
cin >> po;
vector <double> mas(num);
vector<int> ch(po);
for (i = 0; i < num; i++)
{
cout << "mas [" << i << "]=";
cin >> mas[i];
}
cout << endl;
max = mas[0];
min = mas[0];
for (i = 1; i < num; i++)
{
if (max < mas[i])
max = mas[i];
if (min > mas[i])
min = mas[i];
}
le = min;
ri = le + (max - min) / po;
int kol = 0;
while (kol < po - 1)
{
for (i = 0; i < num; i++)
{
if (le <= mas[i] && ri > mas[i])
ch[kol]++;
}
if (ch[kol] > m)
m = ch[kol];
kol++;
le = ri;
ri = le + (max - min) / po;
}
for (i = 0; i < num; i++)
{
if ((ri <= mas[i] && le < mas[i]) || (le <= mas[i] && ri > mas[i]))
ch[kol] ++;
if (ch[kol] > m)
m = ch[kol];
size_t number_count;
cin >> number_count;
Input in;
in.numbers.resize(number_count);
for (size_t i = 0; i < number_count; i++) {
cin >> in.numbers[i];
}
for (i = 0; i < po; i++)
{
int d = m - ch[i];
if (d != 0)
for (int j = 0; j < d; j++)
cout << " ";
for (size_t j = 0; j < ch[i]; j++)
cout << "*";
cout << "|" << ch[i];
cout << endl;
}
return 0;
cin >> in.bin_count;
return in;
}
int main()
{
Input in = input_data();
auto ch = make_histogram(in.numbers, in.bin_count);
show_histogram_svg(ch);
}

@ -0,0 +1,43 @@
#include "histogram.h"
#include "histogram_internal.h"
#include <iostream>
#include <vector>
using namespace std;
void find_minmax(const vector <double>& mas, double& min, double& max)
{
for (size_t i = 0; i < mas.size(); i++)
{
if (max < mas[i])
max = mas[i];
if (min > mas[i])
min = mas[i];
}
}
vector<int> make_histogram(const vector<double>& mas, size_t bin_count)
{
size_t i;
vector<int> ch(bin_count);
double max = mas[0], min = mas[0], le, ri;
find_minmax(mas, min, max);
le = min;
ri = le + (max - min) / bin_count;
int kol = 0;
while (kol < bin_count - 1)
{
for (i = 0; i < mas.size(); i++)
{
if (le <= mas[i] && ri > mas[i])
ch[kol]++;
}
kol++;
le = ri;
ri = le + (max - min) / bin_count;
}
for (i = 0; i < mas.size(); i++)
{
if ((ri <= mas[i] && le < mas[i]) || (le <= mas[i] && ri > mas[i]))
ch[kol] ++;
}
return ch;
}

@ -0,0 +1,7 @@
#ifndef histogram
#define histogram
#include <vector>
std::vector<int> make_histogram(const std::vector<double>& mas, size_t bin_count);
#endif //histogram

@ -0,0 +1,7 @@
#ifndef histogram_internal
#define histogram_internal
#include <vector>
void find_minmax(const std::vector <double>& mas, double& min, double& max);
#endif //histogram_internal

@ -0,0 +1,84 @@
#include "svg.h"
#include <iostream>
#include <vector>
#include <string>
#include <math.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 pers(int bin, int sum, int& x) {
x = round(bin * 100 / sum);
}
void
show_histogram_svg(const vector<int>& 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;
const size_t MAX_ASTERISK = IMAGE_WIDTH - TEXT_WIDTH;
int maxb = bins[0], sum= bins[0];
for (size_t j = 1; j < bins.size(); j++)
{
if (bins[j] > maxb) maxb = bins[j];
sum += 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;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "red", "#ffeeee");
int x = 0;
pers(bin, sum, x);
svg_text((TEXT_LEFT+ TEXT_WIDTH+ bin_width), top + TEXT_BASELINE, (to_string(x)+ " % "));
top += BIN_HEIGHT;
}
svg_end();
}

@ -0,0 +1,8 @@
#ifndef svg
#define svg
#include <vector>
void pers(int bin, int sum, int& x);
void show_histogram_svg(const std::vector <int>& bins);
#endif //svg

@ -0,0 +1,26 @@
#include "text.h"
#include <iostream>
#include <vector>
using namespace std;
void show_histogram_text(vector<int>& ch, size_t bin_count)
{
int m = 0, i;
for (i = 0; i < bin_count; i++)
if (ch[i] > m)
m = ch[i];
for (i = 0; i < bin_count; i++)
{
if (ch[i] < 10)
cout << " ";
else if (ch[i] < 100)
cout << " ";
cout << ch[i] << "|";
size_t k = ch[i];
if (m > 76)
k = 76 * (static_cast <double> (ch[i]) / m);
for (size_t j = 0; j < k; j++)
cout << "*";
cout << endl;
}
}

@ -0,0 +1,7 @@
#ifndef text
#define text
#include <vector>
void show_histogram_text(std::vector<int>& ch);
#endif //text
Загрузка…
Отмена
Сохранить