|
|
|
@ -4,82 +4,96 @@
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
const size_t SCREEN_WIDTH = 80;
|
|
|
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
|
|
|
struct Input {
|
|
|
|
|
vector<double> numbers;
|
|
|
|
|
size_t bin_count{};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Input input_data() {
|
|
|
|
|
Input in;
|
|
|
|
|
int number_count;
|
|
|
|
|
|
|
|
|
|
int number_count, bucket;
|
|
|
|
|
do {
|
|
|
|
|
cout << "Enter number count: ";
|
|
|
|
|
cin >> number_count;
|
|
|
|
|
} while (number_count < 1);
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
cout << "Enter number count: "; cin >> number_count;
|
|
|
|
|
}
|
|
|
|
|
while (number_count < 1);
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
cout << "Enter bucket: "; cin >> bucket;
|
|
|
|
|
}
|
|
|
|
|
while (bucket < 1);
|
|
|
|
|
do {
|
|
|
|
|
cout << "Enter bucket: ";
|
|
|
|
|
cin >> in.bin_count;
|
|
|
|
|
} while (in.bin_count < 1);
|
|
|
|
|
|
|
|
|
|
cout << "\n";
|
|
|
|
|
|
|
|
|
|
vector <double> numbers(number_count);
|
|
|
|
|
for (int i = 0; i < number_count; i++) cin >> numbers[i];
|
|
|
|
|
in.numbers.resize(number_count);
|
|
|
|
|
for (int i = 0; i < number_count; i++) {
|
|
|
|
|
cin >> in.numbers[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float min = numbers[0];
|
|
|
|
|
float max = numbers[0];
|
|
|
|
|
return in;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (float x : numbers)
|
|
|
|
|
{
|
|
|
|
|
void find_minmax(const vector<double>& numbers, double& min, double& max) {
|
|
|
|
|
min = numbers[0];
|
|
|
|
|
max = numbers[0];
|
|
|
|
|
for (float x : numbers) {
|
|
|
|
|
if (x < min) min = x;
|
|
|
|
|
else if (x > max) max = x;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float k = (max-min)/bucket;
|
|
|
|
|
|
|
|
|
|
vector <int> stolb(bucket);
|
|
|
|
|
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) {
|
|
|
|
|
double min, max;
|
|
|
|
|
find_minmax(numbers, min, max);
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < bucket; j++) stolb[j] = 0;
|
|
|
|
|
float k = (max - min) / bin_count;
|
|
|
|
|
vector<size_t> bins(bin_count, 0);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < number_count; i++)
|
|
|
|
|
{
|
|
|
|
|
for (double number : numbers) {
|
|
|
|
|
bool flag = false;
|
|
|
|
|
for (int j = 0; (j < bucket && !flag); j++)
|
|
|
|
|
{
|
|
|
|
|
if (numbers[i] >= (min+k*j) && numbers[i] < (min+k*(1+j)))
|
|
|
|
|
{
|
|
|
|
|
stolb[j]++;
|
|
|
|
|
for (size_t j = 0; (j < bin_count && !flag); j++) {
|
|
|
|
|
if (number >= (min + k * j) && number < (min + k * (j + 1))) {
|
|
|
|
|
bins[j]++;
|
|
|
|
|
flag = true;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!flag) stolb[bucket-1]++;
|
|
|
|
|
if (!flag) bins[bin_count - 1]++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int maxlen = 0;
|
|
|
|
|
return bins;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void show_histogram_text(const vector<size_t>& bins) {
|
|
|
|
|
const size_t SCREEN_WIDTH = 80;
|
|
|
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < bucket; j++)
|
|
|
|
|
{
|
|
|
|
|
if (maxlen<stolb[j]) maxlen = stolb[j];
|
|
|
|
|
size_t max_count = 0;
|
|
|
|
|
for (size_t count : bins) {
|
|
|
|
|
if (count > max_count) {
|
|
|
|
|
max_count = count;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < bucket; j++)
|
|
|
|
|
{
|
|
|
|
|
if (stolb[j] < 100) cout << " ";
|
|
|
|
|
if (stolb[j] < 10) cout << " ";
|
|
|
|
|
cout << stolb[j] << "|";
|
|
|
|
|
size_t height = stolb[j];
|
|
|
|
|
if (maxlen > MAX_ASTERISK)
|
|
|
|
|
{
|
|
|
|
|
if (maxlen != stolb[j]) height = MAX_ASTERISK * (static_cast<float>(stolb[j])/maxlen);
|
|
|
|
|
else if (maxlen == stolb[j]) height = MAX_ASTERISK;
|
|
|
|
|
for (size_t bin : bins) {
|
|
|
|
|
if (bin < 100) cout << " ";
|
|
|
|
|
if (bin < 10) cout << " ";
|
|
|
|
|
cout << bin << "|";
|
|
|
|
|
|
|
|
|
|
size_t height = bin;
|
|
|
|
|
if (max_count > MAX_ASTERISK) {
|
|
|
|
|
if (max_count != bin)
|
|
|
|
|
height = MAX_ASTERISK * (static_cast<float>(bin) / max_count);
|
|
|
|
|
else
|
|
|
|
|
height = MAX_ASTERISK;
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < height; i++) cout << "*";
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < height; i++) cout << "*";
|
|
|
|
|
cout << "\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
int main() {
|
|
|
|
|
auto in = input_data();
|
|
|
|
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
|
|
|
|
show_histogram_text(bins);
|
|
|
|
|
}
|
|
|
|
|