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

main
StepanishchevVR 12 месяцев назад
Родитель a51ad74403
Сommit 32c0cacf24

@ -1,8 +1,5 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
const size_t SCREEN_WIDTH = 80;
const size_t MAX_STAR = SCREEN_WIDTH - 3 - 1;
using namespace std; using namespace std;
struct Input struct Input
@ -14,7 +11,7 @@ struct Input
Input input_data() Input input_data()
{ {
Input in; Input in;
size_t number_count, bin_count; size_t number_count;
cin >> number_count; cin >> number_count;
in.numbers.resize(number_count); in.numbers.resize(number_count);
@ -44,24 +41,22 @@ void find_minmax(const vector<double>& numbers, double& min, double& max)
} }
} }
int main() vector<double> make_histogram(const vector<double> &numbers, size_t bin_count)
{ {
Input in = input_data(); double min = numbers[0];
double max = numbers[0];
vector<double>bins(in.bin_count); find_minmax(numbers, min, max);
double bin_size = (max - min) / bin_count;
vector<double>bins(bin_count);
double min, max; for (size_t i = 0; i < numbers.size(); i++)
find_minmax(in.numbers, min, max);
double bin_size = (max - min) / in.bin_count;
for (size_t i = 0; i < in.numbers.size(); i++)
{ {
bool found = false; bool found = false;
for (size_t j = 0; (j < in.bin_count - 1) && !found; j++) for (size_t j = 0; (j < bin_count - 1) && !found; j++)
{ {
auto lo = min + j * bin_size; auto lo = min + j * bin_size;
auto hi = min + (j + 1) * bin_size; auto hi = min + (j + 1) * bin_size;
if ((lo <= in.numbers[i]) && (in.numbers[i] < hi)) if ((lo <= numbers[i]) && (numbers[i] < hi))
{ {
bins[j]++; bins[j]++;
found = true; found = true;
@ -69,12 +64,11 @@ int main()
} }
if (!found) if (!found)
{ {
bins[in.bin_count - 1]++; bins[bin_count - 1]++;
} }
} }
double b; double b;
for (size_t i = 0; i < in.bin_count-1; i++) for (size_t i = 0; i < bin_count-1; i++)
{ {
if (bins[i] > bins[i+1]) if (bins[i] > bins[i+1])
{ {
@ -83,8 +77,15 @@ int main()
bins[i + 1] = b; bins[i + 1] = b;
} }
} }
return bins;
}
void show_histogram_text(const vector <double> &bins)
{
const size_t SCREEN_WIDTH = 80;
const size_t MAX_STAR = SCREEN_WIDTH - 3 - 1;
size_t max_star_search = bins[-1];
size_t max_star_search = bins[-1]; // поиск наибольшего кол-ва из корзины
if (max_star_search > MAX_STAR) if (max_star_search > MAX_STAR)
{ {
for (size_t x : bins) for (size_t x : bins)
@ -126,5 +127,12 @@ int main()
cout << endl; cout << endl;
} }
} }
}
int main()
{
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins);
return 0; return 0;
} }

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