KhnytchenkovAM 2 лет назад
Родитель 4ec4a7cd73
Сommit f67263f063

@ -1,5 +1,7 @@
#include <iostream>
#include <vector>
#include "histogram.h"
#include "text.h"
using namespace std;
struct Input {
@ -23,88 +25,6 @@ for (size_t i = 0; i < number_count; i++) {
return in;
}
void find_minmax( const vector<double>& numbers, double& minN, double& maxN) {
minN = numbers[0];
maxN = numbers[0];
for (double x: numbers){
if (minN > x){
minN = x;
}
if (maxN < x){
maxN = x;
}
}
}
auto make_histogram(vector<double> numbers, size_t bin_count){
double minN, maxN;
find_minmax( numbers, minN, maxN);
vector <size_t> bins(bin_count);
double diff = (maxN - minN) / bin_count;
size_t max_count = 0;
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 = minN + j * diff;
auto hi = minN + (j + 1) * diff;
if ((lo <= numbers[i]) && (hi > numbers[i])){
bins[j]++;
if (bins[j] > max_count){
max_count = bins[j];
}
found = true;
}
}
if(!found){
bins[bin_count - 1]++;
if (bins[bin_count - 1] > max_count){
max_count = bins[bin_count - 1];
}
}
}
return bins;
}
void show_histogram_text(vector <size_t> bins,size_t bin_count, size_t max_count){
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 6 - 1;
bool scaling = false;
if (max_count > MAX_ASTERISK){
scaling = true;
}
for (size_t i = 0; i < bin_count; i++){
cout << " ";
if (bins[i] < 100){
cout << ' ';
}
if (bins[i] < 10){
cout << ' ';
}
cout << bins[i] << '|';
size_t number_of_stars = bins[i];
if (scaling){
if (bins[i] == max_count){
number_of_stars = MAX_ASTERISK * 1.0;
}
else {
number_of_stars = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
}
}
for (size_t j = 0; j < number_of_stars; j++){
cout << '*';
}
cout << endl;
}
}
int main(){
size_t max_count;
auto in = input_data();

Загрузка…
Отмена
Сохранить