code: разделение на функции
Этот коммит содержится в:
115
main.cpp
115
main.cpp
@@ -2,55 +2,50 @@
|
||||
#include <conio.h>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTER = SCREEN_WIDTH - 3 - 1;
|
||||
int i, j;
|
||||
double num;
|
||||
size_t number_count, bin_count;
|
||||
cerr << "Enter number count: " << "\n";
|
||||
struct Input {
|
||||
vector<double> numbers;
|
||||
size_t bin_count{};
|
||||
};
|
||||
|
||||
Input input_data() {
|
||||
Input in;
|
||||
size_t number_count;
|
||||
cin >> number_count;
|
||||
vector<double> numbers(number_count);
|
||||
cerr << "Enter number: " << "\n";
|
||||
for (i = 0; i < number_count; i++)
|
||||
{
|
||||
cin >> num;
|
||||
numbers[i] = num;
|
||||
in.numbers.resize(number_count);
|
||||
for (size_t i = 0; i < number_count; i++) {
|
||||
cin >> in.numbers[i];
|
||||
}
|
||||
cerr << "Enter bin count: " << "\n";
|
||||
cin >> bin_count;
|
||||
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;
|
||||
}
|
||||
if (x > max)
|
||||
{
|
||||
max = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vector<size_t> make_histogram(vector<double>& numbers, size_t bin_count)
|
||||
{
|
||||
double min_number, max_number;
|
||||
find_minmax(numbers, min_number, max_number);
|
||||
vector<size_t> bins(bin_count);
|
||||
for (i = 0; i < bin_count; i++)
|
||||
for (int i = 0; i < bin_count; i++)
|
||||
{
|
||||
bins[i] = 0;
|
||||
}
|
||||
double min_number = numbers[0];
|
||||
double max_number = numbers[0];
|
||||
for (i = 1; i < bin_count, i++){
|
||||
if (bins[i] < min_number)
|
||||
{
|
||||
min_number = bins[i];
|
||||
}
|
||||
else if (bins[i] > max_number)
|
||||
{
|
||||
max_number = bins[i];
|
||||
}
|
||||
}
|
||||
|
||||
for (double x : numbers)
|
||||
{
|
||||
if (x < min_number)
|
||||
{
|
||||
min_number = x;
|
||||
}
|
||||
else if (x > max_number)
|
||||
{
|
||||
max_number = x;
|
||||
}
|
||||
}
|
||||
double bin_size = (max_number - min_number) / bin_count;
|
||||
for (size_t i = 0; i < number_count; i++)
|
||||
for (size_t i = 0; i < numbers.size(); i++)
|
||||
{
|
||||
bool found = false;
|
||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++)
|
||||
@@ -68,7 +63,12 @@ int main()
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
}
|
||||
size_t max_bin_capacity = bins[0];
|
||||
return bins;
|
||||
}
|
||||
|
||||
void find_max_capacity(vector<size_t>& bins, size_t& max_bin_capacity)
|
||||
{
|
||||
max_bin_capacity = bins[0];
|
||||
for (size_t x : bins)
|
||||
{
|
||||
if (x > max_bin_capacity)
|
||||
@@ -76,8 +76,14 @@ int main()
|
||||
max_bin_capacity = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void show_histogram_text(vector<size_t>& bins, size_t max_asterisk)
|
||||
{
|
||||
size_t max_bin_capacity{};
|
||||
find_max_capacity(bins, max_bin_capacity);
|
||||
size_t height = 0;
|
||||
for (i = 0; i < bin_count; i++)
|
||||
for (int i = 0; i < bins.size(); i++)
|
||||
{
|
||||
if (bins[i] < 10)
|
||||
{
|
||||
@@ -89,19 +95,30 @@ int main()
|
||||
}
|
||||
if (bins[i] >= 100)
|
||||
{
|
||||
cout << bins[i] << "|";
|
||||
cout << bins[i] << "|";
|
||||
}
|
||||
if (bins[i] > MAX_ASTER){
|
||||
height = MAX_ASTER * (static_cast<double>(bins[i]) / max_bin_capacity);
|
||||
if (max_bin_capacity > max_asterisk)
|
||||
{
|
||||
height = max_asterisk * (static_cast<double>(bins[i]) / max_bin_capacity);
|
||||
}
|
||||
if (bins[i] < MAX_ASTER){
|
||||
else
|
||||
{
|
||||
height = bins[i];
|
||||
}
|
||||
for (j = 0; j < height; j++)
|
||||
for (int j = 0; j < height; j++)
|
||||
{
|
||||
cout << "*";
|
||||
}
|
||||
cout << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
auto in = input_data();
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
show_histogram_text(bins, MAX_ASTERISK);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user