Родитель
5cb80b6277
Сommit
e6b2c08e61
@ -0,0 +1,52 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include "histogram.h"
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void static find_minmax(const vector<double> &numbers, double &min, double &max)
|
||||||
|
{
|
||||||
|
min = numbers[0];
|
||||||
|
max = numbers[0];
|
||||||
|
for(double number : numbers)
|
||||||
|
{
|
||||||
|
if(number < min)
|
||||||
|
{
|
||||||
|
min = number;
|
||||||
|
}
|
||||||
|
if(number > max)
|
||||||
|
{
|
||||||
|
max = number;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<size_t> make_histogram(std::vector<double>& numbers, size_t bin_count)
|
||||||
|
{
|
||||||
|
|
||||||
|
double min;
|
||||||
|
double max;
|
||||||
|
find_minmax (numbers, min, max);
|
||||||
|
double bin_size = (max - min) / bin_count;
|
||||||
|
vector<size_t> bins(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 * bin_size;
|
||||||
|
auto hi = min + (j + 1) * bin_size;
|
||||||
|
if((lo <= numbers[i]) && (numbers[i] < hi))
|
||||||
|
{
|
||||||
|
bins[j]++;
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found)
|
||||||
|
{
|
||||||
|
bins[bin_count-1]++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bins;
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
#ifndef HISTOGRAM_H_INCLUDED
|
||||||
|
#define HISTOGRAM_H_INCLUDED
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
std::vector<size_t> make_histogram(std::vector<double>& numbers, size_t bin_count);
|
||||||
|
|
||||||
|
#endif // HISTOGRAM_H_INCLUDED
|
@ -1,75 +1,75 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "histogram.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int main()
|
struct Input {
|
||||||
{
|
vector<double> numbers;
|
||||||
setlocale(LC_ALL, "Russian");
|
size_t bin_count{};
|
||||||
|
};
|
||||||
|
|
||||||
|
Input
|
||||||
|
input_data() {
|
||||||
size_t number_count, bin_count;
|
size_t number_count, bin_count;
|
||||||
size_t max_count = 0;
|
cerr << "Enter number count: ";
|
||||||
|
|
||||||
cerr << "Ââåäèòå êîëè÷åñòâî îöåíîê: ";
|
|
||||||
cin >> number_count;
|
cin >> number_count;
|
||||||
|
Input in;
|
||||||
vector<double> numbers(number_count);
|
in.numbers.resize(number_count);
|
||||||
|
for (size_t i = 0; i < number_count; i++)
|
||||||
for(size_t i = 0; i < number_count; i++){
|
{
|
||||||
cerr << "Ââåäèòå ÷èñëî " << i + 1 << " : ";
|
cerr << "Enter Num[" << i << "]: ";
|
||||||
cin >> numbers[i];
|
cin >> in.numbers[i];
|
||||||
|
}
|
||||||
|
cerr << "Enter bin count: "; cin >> bin_count;
|
||||||
|
cerr << "Enter bin count: ";
|
||||||
|
cin >> in.bin_count;
|
||||||
|
return in;
|
||||||
}
|
}
|
||||||
|
|
||||||
cerr << "Ââåäèòå êîëè÷åñòâî êîðçèí: ";
|
void show_histogram_text(vector<size_t> bins, size_t bin_count){
|
||||||
cin >> bin_count;
|
const size_t SCREEN_WIDTH = 80;
|
||||||
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||||
vector<size_t> bins(bin_count, 0);
|
|
||||||
|
|
||||||
double min = numbers[0];
|
size_t max_bin = bins[0];
|
||||||
double max = numbers[0];
|
for(size_t i = 0; i < bin_count; i++)
|
||||||
for (size_t i = 1; i < number_count; i++) {
|
{
|
||||||
if (numbers[i] < min) {
|
if(bins[i] > max_bin)
|
||||||
min = numbers[i];
|
{
|
||||||
}
|
max_bin = bins[i];
|
||||||
if (numbers[i] > max) {
|
|
||||||
max = numbers[i];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
double bin_size = (max - min) / bin_count;
|
for (size_t bin: bins)
|
||||||
|
{
|
||||||
|
size_t height = bin;
|
||||||
|
|
||||||
for (size_t i = 0; i < number_count; i++) {
|
if (max_bin > MAX_ASTERISK)
|
||||||
bool found = false;
|
{
|
||||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++) {
|
height = MAX_ASTERISK * (static_cast<double>(bin) / max_bin);
|
||||||
auto lo = min + j * bin_size;
|
|
||||||
auto hi = min + (j + 1) * bin_size;
|
|
||||||
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
|
|
||||||
bins[j]++;
|
|
||||||
found = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!found) {
|
|
||||||
bins[bin_count - 1]++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//ìàêñèìóì ýëåìåíòîâ â êîðçèíå
|
if (bin < 100)
|
||||||
for (size_t i = 0; i < bin_count; i++) {
|
{
|
||||||
if (bins[i] > max_count) {
|
cout << ' ';
|
||||||
max_count = bins[i];
|
|
||||||
}
|
}
|
||||||
|
if (bin < 10)
|
||||||
|
{
|
||||||
|
cout << ' ';
|
||||||
}
|
}
|
||||||
|
cout << bin << "|";
|
||||||
for (size_t i = 1; i <= max_count; i++) { //ïî òñðîêàì
|
for(size_t i = 0; i < height; i++)
|
||||||
for (size_t j = 0; j < bin_count; j++) { //ïî êîðçèíàì
|
{
|
||||||
if (bins[j] >= i) {
|
|
||||||
cout << "*";
|
cout << "*";
|
||||||
} else {
|
|
||||||
cout << " ";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
auto in = input_data();
|
||||||
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||||
|
show_histogram_text(bins, in.bin_count);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Загрузка…
Ссылка в новой задаче