Вы не можете выбрать более 25 тем
Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
77 строки
1.5 KiB
C++
77 строки
1.5 KiB
C++
#include <iostream>
|
|
#include <vector>
|
|
#include "histogram.h"
|
|
using namespace std;
|
|
|
|
struct Input {
|
|
vector<double> numbers;
|
|
size_t bin_count{};
|
|
};
|
|
Input
|
|
input_data(){
|
|
size_t number_count, bin_count;
|
|
cerr << "Enter number count: ";
|
|
cin >> number_count;
|
|
Input in;
|
|
in.numbers.resize(number_count);
|
|
for (size_t i = 0; i < number_count; i++)
|
|
{
|
|
cerr << "Enter Num[" << i << "]: ";
|
|
cin >> in.numbers[i];
|
|
}
|
|
cerr << "Enter bin count: "; cin >> bin_count;
|
|
cerr << "Enter bin count: ";
|
|
cin >> in.bin_count;
|
|
return in;
|
|
}
|
|
|
|
|
|
|
|
void show_histogram_text(vector<size_t> bins, size_t bin_count){
|
|
const size_t SCREEN_WIDTH = 80;
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
|
|
size_t max_bin = bins[0];
|
|
for(size_t i = 0; i < bin_count; i++)
|
|
{
|
|
if(bins[i] > max_bin)
|
|
{
|
|
max_bin = bins[i];
|
|
}
|
|
}
|
|
|
|
for (size_t bin: bins)
|
|
{
|
|
size_t height = bin;
|
|
|
|
if (max_bin > MAX_ASTERISK)
|
|
{
|
|
height = MAX_ASTERISK * (static_cast<double>(bin) / max_bin);
|
|
}
|
|
|
|
if (bin < 100)
|
|
{
|
|
cout << ' ';
|
|
}
|
|
if (bin < 10)
|
|
{
|
|
cout << ' ';
|
|
}
|
|
cout << bin << "|";
|
|
for(size_t i = 0; i < height; i++)
|
|
{
|
|
cout << "*";
|
|
}
|
|
cout << endl;
|
|
}
|
|
}
|
|
int main()
|
|
{
|
|
auto in = input_data();
|
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
|
show_histogram_text(bins, in.bin_count);
|
|
|
|
return 0;
|
|
|
|
}
|