code: Програма разбита на функции

main
KIrsanovEs 2 лет назад
Родитель c79b408a6e
Сommit acf6273621

@ -0,0 +1,36 @@
#include "histogram.h"
const 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<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count){
double max,min;
std::vector<size_t> bins(bin_count);
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,8 @@
#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

@ -32,6 +32,12 @@
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="histogram.cpp">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="histogram.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="main.cpp" />
<Extensions>
<lib_finder disable_auto="1" />

@ -1,5 +1,7 @@
#include <iostream>
#include <vector>
#include "histogram.h"
using namespace std;
struct Input {
@ -18,40 +20,7 @@ input_data(){
cin >> in.numbers[i];
}
cin >> in.bin_count;
return in;
}
void find_minmax(const 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(const vector<double>& numbers,size_t bin_count){
double max,min;
find_minmax(numbers,min,max);
double bin_size = (max - min) / bin_count;
vector<size_t> bins(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;
return in;
}
void show_histogram(vector<size_t> bins,size_t bin_count){
for(size_t i=0;i<bin_count;i++){
@ -62,7 +31,7 @@ void show_histogram(vector<size_t> bins,size_t bin_count){
}
int main()
{ auto in = input_data();
auto bins=make_histogram(in.numbers,in.bin_count);
auto bins = make_histogram(in.numbers,in.bin_count);
show_histogram(bins,in.bin_count);
}

Загрузка…
Отмена
Сохранить