code: разбиение на файлы

main
KhatyukhinYS 1 год назад
Родитель 5c17a9a2cf
Сommit 6e9a629025

@ -0,0 +1,48 @@
#include "histogram.h"
using namespace std;
static void
find_minmax(const std::vector<double>& numbers, double& Min, double& Max) {
Min = numbers[0];
Max = numbers[0];
for(size_t x : numbers)
{
if(x < Min)
{
Min = x;
}else
{
if(x > Max)
{
Max = x;
}
}
}
}
std::vector<size_t> make_histogram(std::vector<double> numbers, size_t bin_count)
{
std::vector<size_t> bins(bin_count);
double Min, Max;
find_minmax(numbers, Min, Max);
double bin_size = (Max - Min) / bin_count;
for (double x : numbers)
{
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 <= x) && (x < 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 <iostream>
#include <vector>
std::vector<size_t>
make_histogram(const std::vector<double> numbers, size_t bin_count);
#endif // HISTOGRAM_H_INCLUDED

@ -1,13 +1,12 @@
#include <iostream>
#include <vector>
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
#include "text.h"
#include "histogram.h"
using namespace std;
struct Input {
vector<double> numbers;
std::vector<double> numbers;
size_t bin_count{};
size_t number_count{};
};
Input
input_data() {
@ -22,72 +21,6 @@ input_data() {
cin >> in.bin_count;
return in;
}
void find_minmax(const vector<double>& numbers, double& Min, double& Max) {
Min = numbers[0];
Max = numbers[0];
for(size_t 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)
{
std::vector<size_t> bins(bin_count);
double Min, Max;
find_minmax(numbers, Min, Max);
double bin_size = (Max - Min) / bin_count;
for (double x : numbers)
{
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 <= x) && (x < hi))
{
bins[j]++;
found = true;
}
}
if (!found)
{
bins[bin_count - 1]++;
}
}
return bins;
}
void show_histogram_text(vector<size_t> bins, size_t bin_count)
{
size_t max_count = bins[0];
for(size_t x : bins)
{
if(x > max_count)
{
max_count = x;
}
}
for(size_t i = 0;i<bin_count;i++){
size_t height = bins[i];
printf("%3d:", bins[i]);
if(max_count > MAX_ASTERISK)
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();

@ -0,0 +1,30 @@
#include <iostream>
#include <vector>
#include "text.h"
using namespace std;
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
void show_histogram_text(const std::vector<size_t> bins, size_t bin_count)
{
size_t max_count = bins[0];
for(size_t x : bins)
{
if(x > max_count)
{
max_count = x;
}
}
for(size_t i = 0;i<bin_count;i++){
size_t height = bins[i];
printf("%3d:", bins[i]);
if(max_count > MAX_ASTERISK)
height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
for(size_t j = 0;j<height;j++)
{
cout<<"*";
}
cout<<endl;
}
}

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