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="-Wall" />
<Add option="-fexceptions" /> <Add option="-fexceptions" />
</Compiler> </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" /> <Unit filename="main.cpp" />
<Extensions> <Extensions>
<lib_finder disable_auto="1" /> <lib_finder disable_auto="1" />

@ -1,5 +1,7 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include "histogram.h"
using namespace std; using namespace std;
struct Input { struct Input {
@ -14,44 +16,11 @@ input_data(){
Input in; Input in;
in.numbers.resize(number_count); in.numbers.resize(number_count);
for (size_t i = 0; i < number_count; i++) { for (size_t i = 0; i < number_count; i++) {
cin >> in.numbers[i]; 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) { cin >> in.bin_count;
max = x; return in;
}
}
}
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;
} }
void show_histogram(vector<size_t> bins,size_t bin_count){ void show_histogram(vector<size_t> bins,size_t bin_count){
for(size_t i=0;i<bin_count;i++){ 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() int main()
{ auto in = input_data(); { 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); show_histogram(bins,in.bin_count);
} }

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