KorneevMA 7 месяцев назад
Родитель c45d9ca14c
Сommit 18991cb268

@ -0,0 +1,46 @@
#include <vector>
#include "histogram.h"
using namespace std;
void find_minmax(const vector<double> &numbers, double &min, double &max)
{
min = numbers[0];
max = numbers[0];
for (auto el : numbers)
{
if (el > max)
{
max = el;
}
if (el < min)
{
min = el;
}
}
}
vector<size_t> make_histogram(const vector<double> &numbers, const size_t bin_count){
vector<size_t> bins(bin_count);
double max, min;
find_minmax(numbers, min, max);
double binSize = (max - min) / bin_count;
for (size_t i = 0; i < numbers.size(); i++)
{
bool found = false;
for (size_t j = 0; (j < bin_count - 1) && !found; j++)
{
auto lo = min + j * binSize;
auto hi = min + (j + 1) * binSize;
if ((lo <= numbers[i]) && (numbers[i] < hi))
{
bins[j]++;
found = true;
}
}
if (!found)
{
bins[bin_count - 1]++;
}
}
return bins;
}

@ -0,0 +1,4 @@
#pragma once
#include <vector>
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count);

@ -1,11 +1,10 @@
#include <iostream> #include <iostream>
#include "histogram.h"
#include "text.h"
#include <vector> #include <vector>
using namespace std; using namespace std;
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
struct Input struct Input
{ {
vector<double> numbers; vector<double> numbers;
@ -26,74 +25,6 @@ Input input_data()
return in; return in;
} }
void find_minmax(const vector<double> &numbers, double &min, double &max)
{
min = numbers[0];
max = numbers[0];
for (auto el : numbers)
{
if (el > max)
{
max = el;
}
if (el < min)
{
min = el;
}
}
}
vector<size_t> make_histogram(const vector<double> &numbers, const size_t &bin_count){
vector<size_t> bins(bin_count);
double max, min;
find_minmax(numbers, min, max);
double binSize = (max - min) / bin_count;
for (size_t i = 0; i < numbers.size(); i++)
{
bool found = false;
for (size_t j = 0; (j < bin_count - 1) && !found; j++)
{
auto lo = min + j * binSize;
auto hi = min + (j + 1) * binSize;
if ((lo <= numbers[i]) && (numbers[i] < hi))
{
bins[j]++;
found = true;
}
}
if (!found)
{
bins[bin_count - 1]++;
}
}
return bins;
}
void show_histogram_text(const vector<size_t> &bins){
size_t maxCount = bins[0];
for (auto bin : bins)
{
if (maxCount < bin)
{
maxCount = bin;
}
}
for (auto bin : bins)
{
if (bin < 100)
{
cout << " ";
}
if (bin < 10)
{
cout << " ";
}
size_t height = maxCount < MAX_ASTERISK ? bin : MAX_ASTERISK * (static_cast<double>(bin) / maxCount);
cout << bin << "|" << string(height, '*');
cout << "\n";
}
}
int main() { int main() {
Input in = input_data(); Input in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count); auto bins = make_histogram(in.numbers, in.bin_count);

@ -0,0 +1,37 @@
#include <iostream>
#include <vector>
#include "text.h"
using namespace std;
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
void show_histogram_text(const vector<size_t> &bins)
{
size_t maxCount = bins[0];
for (auto bin : bins)
{
if (maxCount < bin)
{
maxCount = bin;
}
}
for (auto bin : bins)
{
if (bin < 100)
{
cout << " ";
}
if (bin < 10)
{
cout << " ";
}
size_t height = maxCount < MAX_ASTERISK ? bin : MAX_ASTERISK * (static_cast<double>(bin) / maxCount);
cout << bin << "|";
for (size_t j = 0; j < height; j++)
{
cout << "*";
}
cout << "\n";
}
}

@ -0,0 +1,4 @@
#pragma once
#include <vector>
void show_histogram_text(const std::vector<size_t> &bins);
Загрузка…
Отмена
Сохранить