Сравнить коммиты
4 Коммитов
881bd34b2a
...
b7e2e9af42
Автор | SHA1 | Дата |
---|---|---|
![]() |
b7e2e9af42 | 4 недель назад |
![]() |
da84686c71 | 1 месяц назад |
![]() |
1a7558d214 | 2 месяцев назад |
![]() |
056cafb485 | 2 месяцев назад |
@ -1,85 +1,38 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "histogram.h"
|
||||
#include "text.h"
|
||||
#include "svg.h"
|
||||
//#include "doctest.h"
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
struct Input {
|
||||
vector<double> numbers;
|
||||
size_t bin_count{};
|
||||
size_t block_width{};
|
||||
};
|
||||
|
||||
size_t number_count;
|
||||
Input input_data(istream& in_steam = cin) {
|
||||
cerr << "Enter number count: ";
|
||||
cin >> number_count;
|
||||
vector<double> numbers(number_count);
|
||||
for (size_t i=0;i<number_count;i++){
|
||||
cin >> numbers[i];
|
||||
}
|
||||
|
||||
|
||||
size_t bin_count;
|
||||
cerr << "Enter bin count: ";
|
||||
cin >> bin_count;
|
||||
vector<size_t> bins(bin_count, 0);
|
||||
Input in;
|
||||
in.numbers.resize(number_count);
|
||||
|
||||
double min = numbers[0];
|
||||
double max = numbers[0];
|
||||
for (double x: numbers) {
|
||||
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 (size_t i = 0; i < number_count; i++) {
|
||||
cin >> in.numbers[i];
|
||||
}
|
||||
cerr << "Enter bin count: ";
|
||||
cin >> in.bin_count;
|
||||
|
||||
size_t max_count = bins[0];
|
||||
for (size_t i=0;i<bin_count;i++){
|
||||
if (bins[i]>max_count){
|
||||
max_count = bins[i];
|
||||
}
|
||||
}
|
||||
return in;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < bin_count; i++) {
|
||||
if (bins[i] < 10) {
|
||||
cout << " " << bins[i] << "|";
|
||||
}
|
||||
else if (bins[i] < 100) {
|
||||
cout << " " << bins[i] << "|";
|
||||
}
|
||||
else if (bins[i] < 1000) {
|
||||
cout << bins[i] << "|";
|
||||
}
|
||||
|
||||
size_t height;
|
||||
if (max_count <= MAX_ASTERISK){
|
||||
height = bins[i];
|
||||
}
|
||||
else {
|
||||
height = MAX_ASTERISK*(static_cast<double>(bins[i])/max_count);
|
||||
}
|
||||
for (size_t j=0; j<height;j++){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
int main() {
|
||||
auto in = input_data();
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
show_histogram_svg(bins, in.block_width);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Загрузка…
Ссылка в новой задаче