Вы не можете выбрать более 25 тем
Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
71 строка
1.5 KiB
C++
71 строка
1.5 KiB
C++
#include <iostream>
|
|
#include <vector>
|
|
#include "histogram.h"
|
|
|
|
using namespace std;
|
|
|
|
const size_t SCREEN_WIDTH = 80;
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 4;
|
|
|
|
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;
|
|
|
|
cout << "Enter bin count: ";
|
|
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;
|
|
};
|
|
|
|
void show_histogram_text(const vector<size_t> &bins){
|
|
size_t maxbin = bins[0];
|
|
for (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++) {
|
|
cout.width(4);
|
|
cout << bins[i] << "|";
|
|
for (size_t j = 0; j < bins[i]; j++) {
|
|
cout << "*";
|
|
}
|
|
cout << endl;
|
|
}
|
|
} else {
|
|
for (size_t i = 0; i < bins.size(); i++) {
|
|
cout.width(4);
|
|
cout << bins[i] << "|";
|
|
size_t height = static_cast<size_t>(MAX_ASTERISK * (static_cast<double>(bins[i]) / maxbin));
|
|
for (size_t j = 0; j < height; j++) {
|
|
cout << "*";
|
|
}
|
|
cout << endl;
|
|
}
|
|
}
|
|
}
|
|
int main(){
|
|
Input in = input_data();
|
|
|
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
|
|
|
show_histogram_text(bins);
|
|
}
|