Anastasia 2 месяцев назад
Родитель 709d781106
Сommit 8a6ff01ca9

@ -1,65 +1,96 @@
#include <vector> #include <iostream>
#include <iostream> #include <vector>
using namespace std; using namespace std;
int main() { struct Input {
const size_t SCREEN_WIDTH = 80; vector<double> numbers;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; size_t bucket{};
int i,j; size_t number_count{};
string a,b,c; };
size_t number_count, bin_count;
cerr << "Enter number count: "; Input
cin >> number_count; input_data() {
cerr << "Enter colichestvo corzin: "; Input in;
cin >> bin_count; cin >> in.number_count;
vector <size_t> bins(bin_count); cin >> in.bucket;
vector <double> numbers(number_count); in.numbers.resize(in.number_count);
for (i = 0; i < number_count; i++) { for (size_t i = 0; i < in.number_count; i++) {
cin >> numbers[i]; cin >> in.numbers[i];
} }
double min = numbers[0]; return in;
double max = numbers[0];
for (double x : numbers) {
if (x < min) {
min = x;
} }
else if (x > max) {
max = x; 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;
} }
} }
double bin_size = (max - min) / bin_count;
for (size_t i = 0; i < number_count; i++) { vector <size_t>
bool found = false; make_histogram(const vector<double>& numbers, size_t bucket, size_t number_count) {
for (size_t j = 0; (j < bin_count - 1) && !found; j++) { vector <size_t> stolb(bucket);
double lo = min + j * bin_size; double min, max;
double hi = min + (j + 1) * bin_size; find_minmax(numbers, min, max);
if ((lo <= numbers[i]) && (numbers[i] < hi)) { float k = (max - min) / bucket;
bins[j]++; for (size_t i = 0; i < bucket; i++) stolb[i] = 0;
found = true; for (size_t i = 0; i < number_count; i++)
{
bool flag = false;
for (size_t j = 0; (j < bucket && !flag); j++)
{
if (numbers[i] >= (min + k * j) && numbers[i] < (min + k * (1 + j)))
{
stolb[j]++;
flag = true;
} }
} }
if (!found) { if (!flag) stolb[bucket - 1]++;
bins[bin_count - 1]++;
} }
return stolb;
} }
void
show_histogram_text(vector <size_t> stolb, size_t bucket) {
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
int maxlen = 0; int maxlen = 0;
for (int j = 0; j < bin_count; j++)
for (int j = 0; j < bucket; j++)
{ {
if (maxlen < bins[j]) maxlen = bins[j]; if (maxlen < stolb[j]) maxlen = stolb[j];
} }
for (int j = 0; j < bin_count; j++)
for (int j = 0; j < bucket; j++)
{ {
if (bins[j] < 100) cout << " "; if (stolb[j] < 100) cout << " ";
if (bins[j] < 10) cout << " "; if (stolb[j] < 10) cout << " ";
cout << bins[j] << "|"; cout << stolb[j] << "|";
size_t height = bins[j]; size_t height = stolb[j];
if (maxlen > MAX_ASTERISK) if (maxlen > MAX_ASTERISK)
{ {
if (maxlen != bins[j]) height = MAX_ASTERISK * (static_cast<float>(bins[j]) / maxlen); if (maxlen != stolb[j]) height = MAX_ASTERISK * (static_cast <float> (stolb[j]) / maxlen);
else if (maxlen == bins[j]) height = MAX_ASTERISK; else if (maxlen == stolb[j]) height = MAX_ASTERISK;
} }
for (int i = 0; i < height; i++) cout << "*"; for (int i = 0; i < height; i++) cout << "*";
cout << "\n"; cout << "\n";
} }
}
int main()
{
auto in = input_data();
auto stolb = make_histogram(in.numbers, in.bucket, in.number_count);
show_histogram_text(stolb, in.bucket);
return 0; return 0;
} }
Загрузка…
Отмена
Сохранить