Вы не можете выбрать более 25 тем
Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
125 строки
3.6 KiB
C++
125 строки
3.6 KiB
C++
#include <iostream>
|
|
#include <vector>
|
|
#include <math.h>
|
|
|
|
using namespace std;
|
|
|
|
int main()
|
|
{
|
|
const size_t SCREEN_WIDTH = 80;
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
size_t number_count, bin_count;
|
|
cerr << "Enter number count: ";
|
|
cin >> number_count;
|
|
cerr << "Enter bin count: ";
|
|
cin >> bin_count;
|
|
vector<double> numbers(number_count);
|
|
vector<size_t> bins(bin_count);
|
|
cerr << "Enter numbers: ";
|
|
for (size_t i = 0; i < number_count; i++) {
|
|
cin >> numbers[i];
|
|
}
|
|
double min = numbers[0];
|
|
double max = numbers[0];
|
|
for (double x : numbers) {
|
|
if (x < min) {
|
|
min = x;
|
|
}
|
|
else if (x > max) {
|
|
max = x;
|
|
}
|
|
}
|
|
double bin_size = (max - min) / 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++) {
|
|
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]++;
|
|
}
|
|
}
|
|
size_t max_count = bins[0];
|
|
size_t count = 0;
|
|
short space_count;
|
|
for (size_t x : bins) {
|
|
if (x > max_count) {
|
|
max_count = x;
|
|
}
|
|
}
|
|
if (max_count <= MAX_ASTERISK) {
|
|
for (size_t i = 0; i < bin_count; i++) {
|
|
if (bins[i] < 10) {
|
|
space_count = 2;
|
|
} else if (bins[i] >= 10 && bins[i] < 100) {
|
|
space_count = 1;
|
|
} else {
|
|
space_count = 0;
|
|
}
|
|
for (size_t k = 0; k < space_count; k++) {
|
|
cout << " ";
|
|
}
|
|
cout << bins[i];
|
|
cout << "|";
|
|
if (i == 0){
|
|
for (size_t j = 0; j < bins[i]; j++) {
|
|
if (j != bins[i+1]) {
|
|
cout << "*";
|
|
} else if (j == bins[i+1]){
|
|
cout << "V";
|
|
}
|
|
}
|
|
} else if (i == bin_count-1){
|
|
for (size_t j = 0; j < bins[i]; j++) {
|
|
if (j != bins[i-1]) {
|
|
cout << "*";
|
|
} else if (j == bins[i-1]){
|
|
cout << "^";
|
|
}
|
|
}
|
|
} else {
|
|
for (size_t j = 0; j < bins[i]; j++) {
|
|
if (j != bins[i-1]-1 && j != bins[i+1]-1) {
|
|
cout << "*";
|
|
} else if (j == bins[i-1]-1 && j == bins[i+1]-1){
|
|
cout << "N";
|
|
} else if (j == bins[i-1]-1){
|
|
cout << "^";
|
|
} else if (j == bins[i+1]-1){
|
|
cout << "V";
|
|
}
|
|
}
|
|
}
|
|
cout << "\n";
|
|
}
|
|
} /*else {
|
|
for (size_t i = 0; i < bin_count; i++) {
|
|
if (bins[i] < 10) {
|
|
space_count = 2;
|
|
} else if (bins[i] >= 10 && bins[i] < 100) {
|
|
space_count = 1;
|
|
} else {
|
|
space_count = 0;
|
|
}
|
|
for (size_t k = 0; k < space_count; k++) {
|
|
cout << " ";
|
|
}
|
|
cout << bins[i];
|
|
cout << "|";
|
|
size_t height = static_cast<size_t>(MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count));
|
|
for (size_t j = 0; j < height; j++) {
|
|
cout << "*";
|
|
}
|
|
cout << "\n";
|
|
}
|
|
}*/
|
|
system("pause");
|
|
|
|
return 0;
|
|
}
|