Родитель
58a0eba938
Сommit
214a060068
@ -1,109 +1,48 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include "histogram.h"
|
||||
#include "text.h"
|
||||
using namespace std;
|
||||
|
||||
int main(){
|
||||
size_t bin_count, numbers_count;
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
cerr << "Enter number count and bin count:";
|
||||
cin >> numbers_count >> bin_count;
|
||||
vector<double> numbers ( numbers_count );
|
||||
for (size_t i=0; i < numbers_count; i++){
|
||||
cin >> numbers[i];
|
||||
}
|
||||
vector<size_t> bins ( bin_count );
|
||||
double minx = numbers[0];
|
||||
double maxx = numbers[0];
|
||||
for ( double x : numbers ){
|
||||
if ( x < minx ){
|
||||
minx = x;
|
||||
} else if ( x > maxx ){
|
||||
maxx = x;
|
||||
}
|
||||
}
|
||||
double bin_size = ( maxx - minx ) / bin_count;
|
||||
for (size_t i=0; i < numbers_count; i++ ){
|
||||
bool found = false;
|
||||
for (size_t j = 0; ( j < bin_count - 1 ) && !found; j++ ){
|
||||
auto lo = minx + j * bin_size;
|
||||
auto hi = minx + ( j + 1 ) * bin_size;
|
||||
if (lo <= numbers[i] && ( numbers[i] < hi )){
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if ( !found ){
|
||||
bins[bin_count-1]++;
|
||||
}
|
||||
}
|
||||
size_t maxbin = bins[0];
|
||||
for (size_t i=1; i < bin_count; i++){
|
||||
if (maxbin < bins[i]){
|
||||
maxbin = bins[i];
|
||||
}
|
||||
}
|
||||
if (maxbin <=MAX_ASTERISK){
|
||||
for ( size_t i=0; i < bin_count; i++ ){
|
||||
if ((bins[i] < 1000)&&(bins[i] > 99)){
|
||||
cout << bins[i] << "|";
|
||||
for ( size_t j=0; j < bins[i]; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
} else if ((bins[i] < 100)&&(bins[i]>9)) {
|
||||
cout << " " << bins[i] << "|";
|
||||
for ( size_t j=0; j < bins[i]; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
} else if ( bins[i] < 10 ){
|
||||
cout << " " << bins[i]<< "|";
|
||||
for ( size_t j=0; j < bins[i]; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (size_t i=0; i < bin_count; i++){
|
||||
if (bins[i] == maxbin){
|
||||
if ((bins[i] < 1000)&&(bins[i] > 99)){
|
||||
cout << bins[i] << "|";
|
||||
for ( size_t j=0; j < MAX_ASTERISK; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
} else if ((bins[i] < 100)&&(bins[i]>=MAX_ASTERISK)) {
|
||||
cout << " " << bins[i] << "|";
|
||||
for ( size_t j=0; j < bins[i]; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
} else {
|
||||
size_t heightG= MAX_ASTERISK * (static_cast<double>(bins[i]) / maxbin);
|
||||
if ((bins[i] < 1000)&&(bins[i] > 99)){
|
||||
cout << bins[i] << "|";
|
||||
for ( size_t j=0; j < heightG; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
} else if ((bins[i] < 100)&&(bins[i]>9)) {
|
||||
cout << " " << bins[i] << "|";
|
||||
for ( size_t j=0; j < heightG; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
} else if ( bins[i] < 10 ){
|
||||
cout << " " << bins[i]<< "|";
|
||||
for ( size_t j=0; j < heightG; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Input {
|
||||
vector<double> numbers;
|
||||
size_t bin_count{};
|
||||
};
|
||||
|
||||
Input input_data() {
|
||||
|
||||
size_t number_count;
|
||||
|
||||
cerr << "Enter number count: ";
|
||||
cin >> number_count;
|
||||
|
||||
Input in;
|
||||
|
||||
cerr << "Enter number of bins: ";
|
||||
cin >> in.bin_count;
|
||||
|
||||
in.numbers.resize(number_count);
|
||||
|
||||
cerr << "Enter numbers: ";
|
||||
for (size_t i = 0; i < number_count; i++)
|
||||
{
|
||||
cin >> in.numbers[i];
|
||||
}
|
||||
|
||||
return in;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
Input in = input_data();
|
||||
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
|
||||
show_histogram_text(bins);
|
||||
|
||||
|
||||
}
|
||||
|
Загрузка…
Ссылка в новой задаче