Сравнить коммиты
5 Коммитов
58a0eba938
...
8a83bc1587
Автор | SHA1 | Дата |
---|---|---|
![]() |
8a83bc1587 | 1 год назад |
![]() |
f7f6a9f2d7 | 1 год назад |
![]() |
a8381d16c1 | 1 год назад |
![]() |
5d1cc10956 | 1 год назад |
![]() |
214a060068 | 1 год назад |
@ -0,0 +1,37 @@
|
||||
#include "histogram.h"
|
||||
#include <vector>
|
||||
|
||||
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<std::size_t> make_histogram(const std::vector<double> &numbers, std::size_t bin_count) {
|
||||
double min = numbers[0];
|
||||
double max = numbers[0];
|
||||
find_minmax(numbers, min, max);
|
||||
double bin_size = ( max - min ) / bin_count;
|
||||
std::vector<std::size_t> bins ( bin_count );
|
||||
for (std::size_t i=0; i < numbers.size(); i++ ){
|
||||
bool found = false;
|
||||
for (std::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,9 @@
|
||||
#ifndef HISTOGRAM_H_INCLUDED
|
||||
#define HISTOGRAM_H_INCLUDED
|
||||
#include <vector>
|
||||
|
||||
std::vector<size_t>
|
||||
make_histogram(const std::vector<double>& numbers, size_t bin_count);
|
||||
|
||||
|
||||
#endif // HISTOGRAM_H_INCLUDED
|
@ -1,109 +1,48 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include "histogram.h"
|
||||
#include "text.h"
|
||||
using namespace std;
|
||||
|
||||
int main(){
|
||||
size_t bin_count, numbers_count;
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
cerr << "Enter number count and bin count:";
|
||||
cin >> numbers_count >> bin_count;
|
||||
vector<double> numbers ( numbers_count );
|
||||
for (size_t i=0; i < numbers_count; i++){
|
||||
cin >> numbers[i];
|
||||
}
|
||||
vector<size_t> bins ( bin_count );
|
||||
double minx = numbers[0];
|
||||
double maxx = numbers[0];
|
||||
for ( double x : numbers ){
|
||||
if ( x < minx ){
|
||||
minx = x;
|
||||
} else if ( x > maxx ){
|
||||
maxx = x;
|
||||
}
|
||||
}
|
||||
double bin_size = ( maxx - minx ) / bin_count;
|
||||
for (size_t i=0; i < numbers_count; i++ ){
|
||||
bool found = false;
|
||||
for (size_t j = 0; ( j < bin_count - 1 ) && !found; j++ ){
|
||||
auto lo = minx + j * bin_size;
|
||||
auto hi = minx + ( j + 1 ) * bin_size;
|
||||
if (lo <= numbers[i] && ( numbers[i] < hi )){
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if ( !found ){
|
||||
bins[bin_count-1]++;
|
||||
}
|
||||
}
|
||||
size_t maxbin = bins[0];
|
||||
for (size_t i=1; i < bin_count; i++){
|
||||
if (maxbin < bins[i]){
|
||||
maxbin = bins[i];
|
||||
}
|
||||
}
|
||||
if (maxbin <=MAX_ASTERISK){
|
||||
for ( size_t i=0; i < bin_count; i++ ){
|
||||
if ((bins[i] < 1000)&&(bins[i] > 99)){
|
||||
cout << bins[i] << "|";
|
||||
for ( size_t j=0; j < bins[i]; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
} else if ((bins[i] < 100)&&(bins[i]>9)) {
|
||||
cout << " " << bins[i] << "|";
|
||||
for ( size_t j=0; j < bins[i]; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
} else if ( bins[i] < 10 ){
|
||||
cout << " " << bins[i]<< "|";
|
||||
for ( size_t j=0; j < bins[i]; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (size_t i=0; i < bin_count; i++){
|
||||
if (bins[i] == maxbin){
|
||||
if ((bins[i] < 1000)&&(bins[i] > 99)){
|
||||
cout << bins[i] << "|";
|
||||
for ( size_t j=0; j < MAX_ASTERISK; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
} else if ((bins[i] < 100)&&(bins[i]>=MAX_ASTERISK)) {
|
||||
cout << " " << bins[i] << "|";
|
||||
for ( size_t j=0; j < bins[i]; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
} else {
|
||||
size_t heightG= MAX_ASTERISK * (static_cast<double>(bins[i]) / maxbin);
|
||||
if ((bins[i] < 1000)&&(bins[i] > 99)){
|
||||
cout << bins[i] << "|";
|
||||
for ( size_t j=0; j < heightG; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
} else if ((bins[i] < 100)&&(bins[i]>9)) {
|
||||
cout << " " << bins[i] << "|";
|
||||
for ( size_t j=0; j < heightG; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
} else if ( bins[i] < 10 ){
|
||||
cout << " " << bins[i]<< "|";
|
||||
for ( size_t j=0; j < heightG; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
struct Input {
|
||||
vector<double> numbers;
|
||||
size_t bin_count{};
|
||||
};
|
||||
|
||||
Input input_data() {
|
||||
|
||||
size_t number_count;
|
||||
|
||||
cerr << "Enter number count: ";
|
||||
cin >> number_count;
|
||||
|
||||
Input in;
|
||||
|
||||
cerr << "Enter number of bins: ";
|
||||
cin >> in.bin_count;
|
||||
|
||||
in.numbers.resize(number_count);
|
||||
|
||||
cerr << "Enter numbers: ";
|
||||
for (size_t i = 0; i < number_count; i++)
|
||||
{
|
||||
cin >> in.numbers[i];
|
||||
}
|
||||
|
||||
return in;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
Input in = input_data();
|
||||
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
|
||||
show_histogram_text(bins);
|
||||
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,45 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "text.h"
|
||||
|
||||
const std::size_t SCREEN_WIDTH = 80;
|
||||
const std::size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
|
||||
void show_histogram_text(const std::vector<std::size_t> &bins){
|
||||
std::size_t maxbin = bins[0];
|
||||
for (std::size_t i=1; i < bins.size(); i++){
|
||||
if (maxbin < bins[i]){
|
||||
maxbin = bins[i];
|
||||
}
|
||||
}
|
||||
if (maxbin <= MAX_ASTERISK){
|
||||
for (size_t i=0; i < bins.size(); i++ ){
|
||||
if (bins[i] < 10 ){
|
||||
std::cout << " " << bins[i] << "|";
|
||||
} else if (bins[i] < 100) {
|
||||
std::cout << " " << bins[i] << "|";
|
||||
} else if (bins[i] < 1000) {
|
||||
std::cout << bins[i] << "|";
|
||||
}
|
||||
for (size_t j=0; j < bins[i]; j++ ){
|
||||
std::cout << "*";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
} else {
|
||||
for (std::size_t i=0; i < bins.size(); i++){
|
||||
std::size_t heightG= MAX_ASTERISK * (static_cast<double>(bins[i]) / maxbin);
|
||||
if (bins[i] < 10 ){
|
||||
std::cout << " " << bins[i] << "|";
|
||||
} else if (bins[i] < 100) {
|
||||
std::cout << " " << bins[i] << "|";
|
||||
} else if (bins[i] < 1000) {
|
||||
std::cout << bins[i] << "|";
|
||||
}
|
||||
for (std::size_t j=0; j < heightG; j++ ){
|
||||
std::cout << "*";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
#ifndef TEXT_H_INCLUDED
|
||||
#define TEXT_H_INCLUDED
|
||||
#include <vector>
|
||||
|
||||
void show_histogram_text(const std::vector <std::size_t> &bins);
|
||||
|
||||
|
||||
#endif // TEXT_H_INCLUDED
|
Загрузка…
Ссылка в новой задаче