build: выделение в отдельные файлы функции show_histogram_text()

main
Bob (ZharkovIG) 6 месяцев назад
Родитель aec0f7321b
Сommit 5623cec915

@ -1,11 +1,9 @@
#include <iostream>
#include <vector>
#include "histogram.h"
#include "text.h"
using namespace std;
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
struct Input {
vector<double> numbers;
size_t bin_count{};
@ -31,38 +29,6 @@ input_data(){
return in;
}
/*
void
find_minmax(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;
}
}
}
vector<size_t> make_histogram(vector<double>& numbers, size_t bin_count) {
double min, max;
find_minmax(numbers, min, max);
vector<size_t> bins(bin_count, 0);
double bin_size = (max - min) / bin_count;
for (double number : numbers) {
size_t bin_index = bin_count - 1; // default to last bin
if (number < max) {
bin_index = static_cast<size_t>((number - min) / bin_size);
}
bins[bin_index]++;
}
return bins;
}*/
void
show_histogram_text(const vector<size_t>& bins){
size_t i;
@ -97,7 +63,7 @@ show_histogram_text(const vector<size_t>& bins){
cout<<bin<<endl;
}
}
}
}*/
int
main()

@ -0,0 +1,36 @@
#include "text.h"
#include <iostream>
void
show_histogram_text(std::vector<size_t>& bins){
size_t max_count = 0;
for (size_t x: bins) {
if (x > max_count) {
max_count = x;
}
}
if (max_count > MAX_ASTERISK) {
for(size_t bin:bins){
size_t height = bin;
height = MAX_ASTERISK * (static_cast<double>(bin) / max_count);
for(size_t i = 0; i < height; i++){
std::cout<<"*";
}
std::cout<<"|";
if(bin<100) std::cout<<" ";
if(bin<10) std::cout<<" ";
std::cout<<bin<<std::endl;
}
} else {
for(size_t bin:bins){
size_t height = bin;
for(size_t i = 0; i < height; i++){
std::cout<<"*";
}
std::cout<<"|";
if(bin<100) std::cout<<" ";
if(bin<10) std::cout<<" ";
std::cout<<bin<<std::endl;
}
}
}

@ -0,0 +1,12 @@
#ifndef TEXT_H_INCLUDED
#define TEXT_H_INCLUDED
#include <vector>
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
void
show_histogram_text(std::vector<size_t>& bins);
#endif // TEXT_H_INCLUDED
Загрузка…
Отмена
Сохранить