KovalenkoDM 1 год назад
Родитель 585d877931
Сommit 4b97489ed3

@ -0,0 +1,40 @@
#include <vector>
#include "histogram.h"
#include "histogram_internal.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, 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);

@ -0,0 +1,4 @@
#pragma once
#include <vector>
void find_minmax(const std::vector<double>& numbers, double& min, double& max);

@ -1,11 +1,10 @@
#include <iostream>
#include <vector>
#include "histogram.h"
#include "text.h"
using namespace std;
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
using namespace std;
struct Input {
vector<double> numbers;
@ -24,67 +23,6 @@ Input input_data() {
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() {
Input in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);

@ -0,0 +1,30 @@
#include <iostream>
#include <vector>
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 << "|" << string(height, '*');
cout << "\n";
}
}

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