Родитель
60818f1804
Сommit
487995c341
@ -0,0 +1,45 @@
|
|||||||
|
#include "histogram.h"
|
||||||
|
#include <vector>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
void
|
||||||
|
find_minmax(const std::vector<double>& numbers, double& min, double& max, bool& res){
|
||||||
|
if (numbers.size() == 0){
|
||||||
|
res = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
min = numbers[0];
|
||||||
|
max = numbers[0];
|
||||||
|
for (auto x : numbers) {
|
||||||
|
if (x < min) {
|
||||||
|
min = x;
|
||||||
|
}
|
||||||
|
else if (x > max) {
|
||||||
|
max = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::vector<size_t>
|
||||||
|
make_histogram(const std::vector<double>& numbers, size_t bin_count){
|
||||||
|
double min, max;
|
||||||
|
bool res = true;
|
||||||
|
find_minmax (numbers, min, max, res);
|
||||||
|
if (res == false){
|
||||||
|
std::cerr << "Number of elements cannot be equal to zero";
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
double bin_size = (max - min) / bin_count;
|
||||||
|
std::vector<size_t> bins(bin_count);
|
||||||
|
for (auto x : numbers) {
|
||||||
|
bool found = false;
|
||||||
|
for (size_t j = 0; (j < bin_count - 1) && !found ; j++) {
|
||||||
|
if ((min + j * bin_size <= x) && (x < min + (j + 1) * bin_size)) {
|
||||||
|
bins[j] += 1;
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found) bins[bin_count - 1]++;
|
||||||
|
}
|
||||||
|
return bins;
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
std::vector<size_t>
|
||||||
|
make_histogram(const std::vector<double>& numbers, size_t bin_count);
|
||||||
|
|
@ -0,0 +1,43 @@
|
|||||||
|
#include "text.h"
|
||||||
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
const size_t SCREEN_WIDTH = 80;
|
||||||
|
const size_t MAX_LENGTH = SCREEN_WIDTH - 3 - 1;
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_text(vector<size_t> bins, size_t bin_count ){
|
||||||
|
auto max_count = bins[0];
|
||||||
|
for (auto x : bins) {
|
||||||
|
if (x > max_count) {
|
||||||
|
max_count = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < bin_count; i++) {
|
||||||
|
size_t j = 100;
|
||||||
|
while (bins[i] < j) {
|
||||||
|
cout << " ";
|
||||||
|
j /= 10;
|
||||||
|
}
|
||||||
|
cout << bins[i] << "|";
|
||||||
|
if (max_count > MAX_LENGTH) {
|
||||||
|
auto count = bins[i];
|
||||||
|
size_t height = MAX_LENGTH * (static_cast<double>(count) / max_count);
|
||||||
|
j = 0;
|
||||||
|
while (j < height) {
|
||||||
|
cout << "*";
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
j = 0;
|
||||||
|
while (j < bins[i]) {
|
||||||
|
cout << "*";
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_text(std::vector<size_t> bins, size_t bin_count );
|
Загрузка…
Ссылка в новой задаче