|
|
|
@ -3,80 +3,133 @@
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
struct Input
|
|
|
|
|
{
|
|
|
|
|
double min,max,lo,hi;
|
|
|
|
|
size_t number_count,bin_count,i,j,s=0;
|
|
|
|
|
const size_t SCREEN_WIDTH = 80;
|
|
|
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 4;
|
|
|
|
|
vector<double> numbers;
|
|
|
|
|
size_t bin_count{};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
cout << "Enter number_count: ";
|
|
|
|
|
cin >> number_count;
|
|
|
|
|
cout << "Enter numbers: ";
|
|
|
|
|
vector<double> numbers;
|
|
|
|
|
for(size_t i =0 ; i < number_count; ++i){
|
|
|
|
|
double num;
|
|
|
|
|
cin >> num;
|
|
|
|
|
numbers.push_back(num);
|
|
|
|
|
}
|
|
|
|
|
Input input_data()
|
|
|
|
|
{
|
|
|
|
|
size_t number_count;
|
|
|
|
|
cerr << "Enter number count: ";
|
|
|
|
|
cin >> number_count;
|
|
|
|
|
|
|
|
|
|
cout << "Enter bin_count: ";
|
|
|
|
|
cin >> bin_count;
|
|
|
|
|
min = numbers[0];
|
|
|
|
|
max = numbers[0];
|
|
|
|
|
for (i = 0; i < number_count; i++){
|
|
|
|
|
if (numbers[i] < min){
|
|
|
|
|
min = numbers[i];
|
|
|
|
|
}
|
|
|
|
|
else if (numbers[i] > max){
|
|
|
|
|
max = numbers[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Input in;
|
|
|
|
|
in.numbers.resize(number_count);
|
|
|
|
|
|
|
|
|
|
vector<size_t> bins(bin_count);
|
|
|
|
|
double bin_size = (max-min)/bin_count;
|
|
|
|
|
vector<double> numbers(number_count);
|
|
|
|
|
for (int i = 0; i < number_count; i++)
|
|
|
|
|
{
|
|
|
|
|
cerr << "Number[" << i << "]=";
|
|
|
|
|
cin >> in.numbers[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cerr << "Enter bin count: ";
|
|
|
|
|
cin >> in.bin_count;
|
|
|
|
|
return in;
|
|
|
|
|
}
|
|
|
|
|
void find_minmax(const vector <double> &numbers, double &min, double &max)
|
|
|
|
|
{
|
|
|
|
|
min = numbers[0];
|
|
|
|
|
max = 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)
|
|
|
|
|
{
|
|
|
|
|
double min, max;
|
|
|
|
|
find_minmax(numbers, min, max);
|
|
|
|
|
double bin_size = (max - min) / bin_count;
|
|
|
|
|
vector <size_t> bins (bin_count);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < number_count; i++){
|
|
|
|
|
bool found = false;
|
|
|
|
|
for (size_t j = 0;(j < bin_count - 1) && !found; j++){
|
|
|
|
|
lo = min + j * bin_size;
|
|
|
|
|
hi = min + (j + 1) * bin_size;
|
|
|
|
|
if ((lo <= numbers[i]) && (numbers[i]<hi)){
|
|
|
|
|
bins[j]++;
|
|
|
|
|
found = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!found){
|
|
|
|
|
bins[bin_count - 1]++;
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
void show_histogram_text(const vector<size_t>& bins, size_t& bin_count, double bin_size)
|
|
|
|
|
{
|
|
|
|
|
const size_t SCREEN_WIDTH = 80;
|
|
|
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
|
|
|
size_t max_bin_count = bins[0];
|
|
|
|
|
for(size_t i = 0; i < bin_count; i++)
|
|
|
|
|
{
|
|
|
|
|
if(bins[i] > max_bin_count)
|
|
|
|
|
{
|
|
|
|
|
max_bin_count = bins[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(size_t i = 0; i < bin_count; i++)
|
|
|
|
|
{
|
|
|
|
|
if(bins[i]<10)
|
|
|
|
|
{
|
|
|
|
|
cout<< " " << bins[i] << "|";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(bins[i]<100)
|
|
|
|
|
{
|
|
|
|
|
cout<< " " << bins[i] << "|";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(bins[i]<1000)
|
|
|
|
|
{
|
|
|
|
|
cout<< bins[i] << "|";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t max_count = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0 ;i < bin_count;i++){
|
|
|
|
|
if (bins[i] > MAX_ASTERISK){
|
|
|
|
|
max_count = bins[i];
|
|
|
|
|
}
|
|
|
|
|
size_t height;
|
|
|
|
|
if(max_bin_count <= MAX_ASTERISK)
|
|
|
|
|
{
|
|
|
|
|
height = bins[i];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_bin_count);
|
|
|
|
|
}
|
|
|
|
|
for(size_t j = 0; j < height; j ++)
|
|
|
|
|
{
|
|
|
|
|
cout<<"*";
|
|
|
|
|
}
|
|
|
|
|
cout<<endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0;i < bin_count; i++){
|
|
|
|
|
s += bins[i];
|
|
|
|
|
if (bins[i] < 100){
|
|
|
|
|
cout << " ";
|
|
|
|
|
}
|
|
|
|
|
if (bins[i]<10){
|
|
|
|
|
cout <<" ";
|
|
|
|
|
}
|
|
|
|
|
cout << s << "|";
|
|
|
|
|
if (max_count == 0){
|
|
|
|
|
for(j = 0;j < s; j++){
|
|
|
|
|
cout <<"*";
|
|
|
|
|
}}
|
|
|
|
|
else {
|
|
|
|
|
size_t height = MAX_ASTERISK*(static_cast<double>(s) / max_count);
|
|
|
|
|
for( j = 0; j < height; j++){
|
|
|
|
|
cout <<"*";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cout << endl;
|
|
|
|
|
}}
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
Input in = input_data();
|
|
|
|
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
|
|
|
|
double min, max;
|
|
|
|
|
double bin_size = (max - min) / in.bin_count;
|
|
|
|
|
show_histogram_text(bins,in.bin_count, bin_size);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|