Родитель
4475c84c98
Сommit
f6bdfa4bf2
@ -1,100 +1,102 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
|
||||
char a,b,c;
|
||||
cout << "viravnit = ";
|
||||
cin >> a;
|
||||
cout << "osi = ";
|
||||
cin >> b;
|
||||
cout << "risunok = ";
|
||||
cin >> c;
|
||||
|
||||
cout << "viravnit = " << a << '\n'
|
||||
<< "osi = " << b << '\n'
|
||||
<< "risunok = " << c << '\n';
|
||||
if (a == '\t' || a == '\n' || b == '\t' || b == '\n' || c == '\t' || c == '\n') {
|
||||
cout << "ERROR!";
|
||||
return 0;
|
||||
}
|
||||
struct Input {
|
||||
vector<double> numbers;
|
||||
size_t bin_count = 0;
|
||||
};
|
||||
|
||||
int number_count, bucket;
|
||||
Input input_data() {
|
||||
Input in;
|
||||
int number_count;
|
||||
|
||||
do
|
||||
{
|
||||
cerr << "Enter number count: "; cin >> number_count;
|
||||
}
|
||||
while (number_count < 1);
|
||||
do {
|
||||
cerr << "Enter number count: ";
|
||||
cin >> number_count;
|
||||
} while (number_count < 1);
|
||||
|
||||
do
|
||||
{
|
||||
cerr << "Enter bucket: "; cin >> bucket;
|
||||
}
|
||||
while (bucket < 1);
|
||||
do {
|
||||
cerr << "Enter bucket: ";
|
||||
cin >> in.bin_count;
|
||||
} while (in.bin_count < 1);
|
||||
|
||||
cerr << "\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];
|
||||
}
|
||||
|
||||
return in;
|
||||
}
|
||||
|
||||
float min = numbers[0];
|
||||
float max = numbers[0];
|
||||
void find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||
if (numbers.empty()) return;
|
||||
|
||||
for (float x : numbers)
|
||||
{
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (double x : numbers) {
|
||||
if (x < min) min = x;
|
||||
else if (x > max) max = x;
|
||||
if (x > max) max = x;
|
||||
}
|
||||
}
|
||||
|
||||
float k = (max-min)/bucket;
|
||||
|
||||
vector <int> stolb(bucket);
|
||||
vector<int> 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<int> bins(bin_count, 0);
|
||||
|
||||
for (int i = 0; i < number_count; i++)
|
||||
{
|
||||
for (double num : 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 (num >= (min + k * j) && num < (min + k * (j + 1))) {
|
||||
bins[j]++;
|
||||
flag = true;
|
||||
|
||||
}
|
||||
}
|
||||
if (!flag) stolb[bucket-1]++;
|
||||
if (!flag) bins[bin_count - 1]++;
|
||||
}
|
||||
|
||||
return bins;
|
||||
}
|
||||
|
||||
int maxlen = 0;
|
||||
void show_histogram_text(const vector<int>& 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];
|
||||
int max_count = 0;
|
||||
for (int count : bins) {
|
||||
if (count > max_count) max_count = count;
|
||||
}
|
||||
|
||||
for (int j = 0; j < bucket; j++)
|
||||
{
|
||||
if (stolb[j] < 100) cout << a;
|
||||
if (stolb[j] < 10) cout << a;
|
||||
cout << stolb[j] << b;
|
||||
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 j = 0; j < bins.size(); j++) {
|
||||
if (bins[j] < 100) cout << " ";
|
||||
if (bins[j] < 10) cout << " ";
|
||||
cout << bins[j] << "|";
|
||||
|
||||
size_t height = bins[j];
|
||||
if (max_count > MAX_ASTERISK) {
|
||||
if (max_count != bins[j]) {
|
||||
height = static_cast<size_t>(MAX_ASTERISK * (static_cast<float>(bins[j]) / max_count));
|
||||
}
|
||||
else if (max_count == bins[j]) {
|
||||
height = MAX_ASTERISK;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < height; i++) cout << c;
|
||||
|
||||
for (size_t i = 0; i < height; i++) cout << "*";
|
||||
cout << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto input = input_data();
|
||||
auto bins = make_histogram(input.numbers, input.bin_count);
|
||||
show_histogram_text(bins);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Загрузка…
Ссылка в новой задаче