Сравнить коммиты
2 Коммитов
5b43f9c1bb
...
2633ce2005
| Автор | SHA1 | Дата | |
|---|---|---|---|
| 2633ce2005 | |||
| 89d83a8b0b |
7106
doctest.h
Обычный файл
7106
doctest.h
Обычный файл
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
117
mpei.cpp
117
mpei.cpp
@@ -5,21 +5,25 @@ using namespace std;
|
|||||||
const size_t SCREEN_WIDTH = 80;
|
const size_t SCREEN_WIDTH = 80;
|
||||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||||
|
|
||||||
int main()
|
struct Input {
|
||||||
{
|
vector<double> numbers;
|
||||||
|
size_t bin_count{};
|
||||||
|
};
|
||||||
|
Input
|
||||||
|
input_data() {
|
||||||
size_t number_count;
|
size_t number_count;
|
||||||
cout << "Enter number count: ";
|
|
||||||
cin >> number_count;
|
cin >> number_count;
|
||||||
vector<double> numbers(number_count);
|
Input in;
|
||||||
for (int i = 0; i < number_count; i++) {
|
in.numbers.resize(number_count);
|
||||||
cin >> numbers[i];
|
for (size_t i = 0; i < number_count; i++) {
|
||||||
|
cin >> in.numbers[i];
|
||||||
}
|
}
|
||||||
size_t bin_count;
|
cin >> in.bin_count;
|
||||||
cout << "Enter bin count: ";
|
return in;
|
||||||
cin >> bin_count;
|
}
|
||||||
vector<size_t> bins(bin_count);
|
void find_minmax(const vector<double>& numbers, double& min, double& max){
|
||||||
double min = numbers[0];
|
min = numbers[0];
|
||||||
double max = numbers[0];
|
max = numbers[0];
|
||||||
for (double x : numbers) {
|
for (double x : numbers) {
|
||||||
if (x < min) {
|
if (x < min) {
|
||||||
min = x;
|
min = x;
|
||||||
@@ -28,13 +32,22 @@ int main()
|
|||||||
max = x;
|
max = x;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count)
|
||||||
|
{
|
||||||
|
double min, max;
|
||||||
|
find_minmax(numbers, min, max);
|
||||||
|
|
||||||
|
vector<size_t> bins(bin_count);
|
||||||
double bin_size = (max - min) / bin_count;
|
double bin_size = (max - min) / bin_count;
|
||||||
for (size_t i = 0; i < number_count; i++) {
|
|
||||||
|
for (double num : numbers) {
|
||||||
bool found = false;
|
bool found = false;
|
||||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++) {
|
for (size_t j = 0; j < bin_count - 1 && !found; j++) {
|
||||||
auto lo = min + j * bin_size;
|
double lo = min + j * bin_size;
|
||||||
auto hi = min + (j + 1) * bin_size;
|
double hi = min + (j + 1) * bin_size;
|
||||||
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
|
if (lo <= num && num < hi) {
|
||||||
bins[j]++;
|
bins[j]++;
|
||||||
found = true;
|
found = true;
|
||||||
}
|
}
|
||||||
@@ -43,37 +56,49 @@ int main()
|
|||||||
bins[bin_count - 1]++;
|
bins[bin_count - 1]++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return bins;
|
||||||
|
}
|
||||||
|
|
||||||
|
void show_histogram_text(vector<size_t> bins, size_t bin_count)
|
||||||
|
{
|
||||||
double max_count = bins[0];
|
double max_count = bins[0];
|
||||||
for (double x : bins) {
|
for (double x : bins) {
|
||||||
if (x > max_count) {
|
if (x > max_count) {
|
||||||
max_count = x;
|
max_count = x;
|
||||||
}
|
|
||||||
}
|
|
||||||
size_t height;
|
|
||||||
bool flag = true;
|
|
||||||
for (size_t j = 0; j < bin_count; j++) {
|
|
||||||
if (bins[j] < 100) {
|
|
||||||
cout << " ";
|
|
||||||
}
|
|
||||||
if (bins[j] < 10) {
|
|
||||||
cout << " ";
|
|
||||||
}
|
|
||||||
cout << bins[j] << "|";
|
|
||||||
if (max_count > MAX_ASTERISK) {
|
|
||||||
for (size_t i = 0; i < bins[j]; i++) {
|
|
||||||
if (bins[j] != MAX_ASTERISK && flag) {
|
|
||||||
double count = bins[j];
|
|
||||||
height = MAX_ASTERISK * (static_cast<double>(count) / max_count);
|
|
||||||
bins[j] = height;
|
|
||||||
flag = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (size_t i = 0; i < bins[j]; i++) {
|
size_t height;
|
||||||
cout << "*";
|
bool flag = true;
|
||||||
|
for (size_t j = 0; j < bin_count; j++) {
|
||||||
|
if (bins[j] < 100) {
|
||||||
|
cout << " ";
|
||||||
|
}
|
||||||
|
if (bins[j] < 10) {
|
||||||
|
cout << " ";
|
||||||
|
}
|
||||||
|
cout << bins[j] << "|";
|
||||||
|
if (max_count > MAX_ASTERISK) {
|
||||||
|
for (size_t i = 0; i < bins[j]; i++) {
|
||||||
|
if (bins[j] != MAX_ASTERISK && flag) {
|
||||||
|
double count = bins[j];
|
||||||
|
height = MAX_ASTERISK * (static_cast<double>(count) / max_count);
|
||||||
|
bins[j] = height;
|
||||||
|
flag = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < bins[j]; i++) {
|
||||||
|
cout << "*";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
flag = true;
|
||||||
}
|
}
|
||||||
cout << endl;
|
}
|
||||||
flag = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
int main()
|
||||||
|
{
|
||||||
|
Input in = input_data();
|
||||||
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||||
|
show_histogram_text(bins,in.bin_count);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user