Родитель
ebd84a6053
Сommit
d8f414ff60
@ -0,0 +1,18 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "hist_proc.h"
|
||||
using namespace std;
|
||||
vector<size_t> make_histogram_proc(const vector<double> numbers, size_t bin_count, vector<size_t> bins){
|
||||
vector<size_t> procent(bin_count);
|
||||
double sum = 0;
|
||||
for (size_t i = 0; i < bin_count; i++){
|
||||
sum = sum + bins[i];
|
||||
}
|
||||
double ost = 100;
|
||||
for (size_t i = 0; i< bin_count-1; i++){
|
||||
procent[i]=(bins[i]/sum)*100;
|
||||
ost = ost - procent[i];
|
||||
}
|
||||
procent[bin_count-1]=ost;
|
||||
return procent;
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include "histogram.h"
|
||||
using namespace std;
|
||||
|
||||
static void
|
||||
find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||
min = numbers[0];
|
||||
for (double x : numbers) {
|
||||
if (x < min) {
|
||||
min = x;
|
||||
}
|
||||
else if (x > max) {
|
||||
max = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vector<size_t> make_histogram(const vector<double> numbers, size_t bin_count){
|
||||
vector<size_t> bins(bin_count);
|
||||
double max, min = 0;
|
||||
find_minmax(numbers, min, max);
|
||||
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,60 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <text.h>
|
||||
using namespace std;
|
||||
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
|
||||
void
|
||||
show_histogram(size_t bin_count, std::vector<size_t> bins, std::vector<size_t> procent){
|
||||
|
||||
size_t max_count = 0;
|
||||
for (size_t x: bins) {
|
||||
if (x > max_count) {
|
||||
max_count = x;
|
||||
}
|
||||
}
|
||||
|
||||
if (max_count > MAX_ASTERISK) {
|
||||
for (size_t i = 0; i < bin_count ; i++) {
|
||||
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
|
||||
if (bins[i] < 10) {
|
||||
cout << " " << procent[i] << "%|";
|
||||
}
|
||||
else if (bins[i] >= 100) {
|
||||
cout << procent[i] << "%|";
|
||||
}
|
||||
|
||||
else {
|
||||
cout << " " << procent[i] << "%|";
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < height; i++) {
|
||||
cout << "*";
|
||||
}
|
||||
cout << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
for (size_t i = 0; i < bin_count; i++) {
|
||||
|
||||
if (procent[i] < 10) {
|
||||
cout << " " << procent[i] << "%|";
|
||||
}
|
||||
else if (procent[i] < 100){
|
||||
cout << " " << procent[i] << "%|";
|
||||
}
|
||||
else if (procent[i] = 100){
|
||||
cout << procent[i] << "%|";
|
||||
}
|
||||
|
||||
for (size_t r = 0; r < bins[i]; r++)
|
||||
cout << "*";
|
||||
cout << "\n";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Загрузка…
Ссылка в новой задаче