Сравнить коммиты
4 Коммитов
86ea4a326a
...
main
| Автор | SHA1 | Дата | |
|---|---|---|---|
|
|
161fd910f1 | ||
|
|
6cd71782fd | ||
|
|
17deb398d5 | ||
|
|
faa5b7af4e |
50
histogram.cpp
Обычный файл
50
histogram.cpp
Обычный файл
@@ -0,0 +1,50 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
void
|
||||||
|
find_minmax(const std::vector<double> &numbers, double &min, double &max){
|
||||||
|
min = numbers[0];
|
||||||
|
max = numbers[0];
|
||||||
|
for (double x : numbers)
|
||||||
|
{
|
||||||
|
if (x < min)
|
||||||
|
{
|
||||||
|
min = x;
|
||||||
|
}
|
||||||
|
else if (x > max)
|
||||||
|
{
|
||||||
|
max = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<int>
|
||||||
|
make_histogram(std::vector<double> numbers, size_t bin_count){
|
||||||
|
double min;
|
||||||
|
double max;
|
||||||
|
vector<int> bins(bin_count);
|
||||||
|
find_minmax(numbers, min, max);
|
||||||
|
double bin_size = (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 * 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;
|
||||||
|
}
|
||||||
9
histogram.h
Обычный файл
9
histogram.h
Обычный файл
@@ -0,0 +1,9 @@
|
|||||||
|
#ifndef HISTOGRAM_H_INCLUDED
|
||||||
|
#define HISTOGRAM_H_INCLUDED
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
std::vector<int>
|
||||||
|
make_histogram(std::vector<double> numbers, size_t bin_count);
|
||||||
|
|
||||||
|
#endif
|
||||||
77
main.cpp
77
main.cpp
@@ -1,11 +1,16 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "histogram.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
struct Input {
|
const size_t SCREEN_WIDTH = 80;
|
||||||
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||||
|
|
||||||
|
struct
|
||||||
|
Input {
|
||||||
vector<double> numbers;
|
vector<double> numbers;
|
||||||
size_t bin_count{};
|
int bin_count{};
|
||||||
};
|
};
|
||||||
|
|
||||||
Input
|
Input
|
||||||
@@ -17,49 +22,21 @@ input_data(){
|
|||||||
for (size_t i = 0; i < number_count; i++) {
|
for (size_t i = 0; i < number_count; i++) {
|
||||||
cin >> in.numbers[i];
|
cin >> in.numbers[i];
|
||||||
}
|
}
|
||||||
size_t bin_count;
|
int bin_count;
|
||||||
cin >> bin_count;
|
cin >> in.bin_count;
|
||||||
return in;
|
return in;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
void
|
||||||
{
|
show_histogramm_text(vector<int> bins, vector<double> numbers, size_t bin_count){
|
||||||
Input in = input_data();
|
|
||||||
|
|
||||||
|
size_t max_count;
|
||||||
double min = numbers[0];
|
for (int i =0; i<bin_count;i++){
|
||||||
double max = numbers[0];
|
if (numbers[i]>max_count){
|
||||||
for (double x : numbers)
|
max_count=numbers[i];
|
||||||
{
|
|
||||||
if (x < min)
|
|
||||||
{
|
|
||||||
min = x;
|
|
||||||
}
|
|
||||||
else if (x > max)
|
|
||||||
{
|
|
||||||
max = x;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
double bin_size = (max - min) / bin_count;
|
|
||||||
for (size_t i = 0; i < number_count; 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]++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i =0; i<bin_count;i++){
|
for (int i =0; i<bin_count;i++){
|
||||||
if (bins[i]>max_count){
|
if (bins[i]>max_count){
|
||||||
@@ -67,28 +44,26 @@ int main()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//*cout<<max_count<<endl;
|
|
||||||
|
|
||||||
if (max_count>MAX_ASTERISK){
|
if (max_count>MAX_ASTERISK){
|
||||||
for (int i=0; i<bin_count; i++){
|
for (int i=0; i<bin_count; i++){
|
||||||
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
|
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
|
||||||
if (bins[i]<10){
|
if (bins[i]<10){
|
||||||
cout<<" "<<bins[i]<<"|";
|
cout<<" "<<bins[i]<<"|";
|
||||||
for (int j=0;j<height;j++){
|
for (int j=0;j<bins[i];j++){
|
||||||
cout<<"*";
|
cout<<"*";
|
||||||
}
|
}
|
||||||
cout<<endl;
|
cout<<endl;
|
||||||
}
|
}
|
||||||
else if(bins[i]<100){
|
else if(bins[i]<100){
|
||||||
cout<<" "<<bins[i]<<"|";
|
cout<<" "<<bins[i]<<"|";
|
||||||
for (int j=0;j<height;j++){
|
for (int j=0;j<bins[i];j++){
|
||||||
cout<<"*";
|
cout<<"*";
|
||||||
}
|
}
|
||||||
cout<<endl;
|
cout<<endl;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
cout<<bins[i]<<"|";
|
cout<<bins[i]<<"|";
|
||||||
for (int j=0;j<height;j++){
|
for (int j=0;j<bins[i];j++){
|
||||||
cout<<"*";
|
cout<<"*";
|
||||||
}
|
}
|
||||||
cout<<endl;
|
cout<<endl;
|
||||||
@@ -121,4 +96,16 @@ int main()
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
main()
|
||||||
|
{
|
||||||
|
|
||||||
|
auto in = input_data();
|
||||||
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||||
|
show_histogramm_text(bins, in.numbers, in.bin_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Ссылка в новой задаче
Block a user