Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

60 строки
1.3 KiB
C++

#include <iostream>
#include <vector>
#include "histogram.h"
using namespace std;
struct Input {
vector<double> numbers;
size_t bucket{};
size_t number_count{};
};
Input
input_data() {
Input in;
cin >> in.number_count;
cin >> in.bucket;
in.numbers.resize(in.number_count);
for (size_t i = 0; i < in.number_count; i++) {
cin >> in.numbers[i];
}
return in;
}
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;
for (int j = 0; j < bucket; j++)
{
if (maxlen<stolb[j]) maxlen = stolb[j];
}
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 (int i = 0; i < height; i++) cout << "*";
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;
}