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

master
Alex 11 месяцев назад
Родитель 860ce1d0e1
Сommit 69a7b410dd

@ -3,31 +3,32 @@
using namespace std;
int main()
struct Input
{
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
int max_count = 0;
vector<double> numbers;
size_t bin_count{};
};
Input input_data()
{
Input in;
size_t number_count;
cerr << "Enter number count: ";
cin >> number_count;
vector<double> numbers(number_count);
in.numbers.resize(number_count);
for (size_t i = 0; i < number_count; i++)
{
cin >> numbers[i];
cin >> in.numbers[i];
}
size_t bin_count;
cerr << "Enter bin count: ";
cin >> bin_count;
vector<size_t> bins(bin_count);
double min = numbers[0];
double max = numbers[0];
cin >> in.bin_count;
return in;
}
void find_minmax(const vector<double>& numbers, double& min, double& max)
{
min = numbers[0];
for (double x : numbers)
{
if (x < min)
@ -39,10 +40,16 @@ int main()
max = x;
}
}
}
vector<double> make_histogram(const vector<double> &numbers, size_t bin_count)
{
double min = numbers[0];
double max = numbers[0];
find_minmax(numbers, min, max);
double bin_size = (max - min) / bin_count;
for (size_t i = 0; i < number_count; i++)
vector<double>bins(bin_count);
for (size_t i = 0; i < numbers.size(); i++)
{
bool found = false;
for (size_t j = 0; (j < bin_count - 1) && !found; j++)
@ -54,7 +61,6 @@ int main()
bins[j]++;
found = true;
}
}
if (!found)
{
@ -62,82 +68,72 @@ int main()
}
}
for (size_t i = 0; i < bin_count; i++)
double b;
for (size_t i = 0; i < bin_count-1; i++)
{
if (bins[i] > max_count)
if (bins[i] > bins[i+1])
{
max_count = bins[i];
b = bins[i];
bins[i] = bins[i + 1];
bins[i + 1] = b;
}
}
return bins;
}
if (max_count > MAX_ASTERISK)
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];
if (max_star_search > MAX_STAR)
{
for (size_t i = 0; i < bin_count; i++)
for (size_t x : bins)
{
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
if (bins[i] < 10)
{
cout << " " << bins[i] << "|";
for (int j = 0; j < height; j++)
{
cout << "*";
}
cout << endl;
}
else if (bins[i] < 100)
size_t height = MAX_STAR * (static_cast<double>(x) / max_star_search);
if (x < 100)
{
cout << " " << bins[i] << "|";
for (int j = 0; j < height; j++)
cout << " ";
if (x < 10)
{
cout << "*";
cout << " ";
}
cout << endl;
}
else
cout << x << "|";
for (size_t i = 0; i < height; i++)
{
cout << bins[i] << "|";
for (int j = 0; j < height; j++)
{
cout << "*";
}
cout << endl;
cout << "*";
}
cout << endl;
}
}
else
{
for (size_t i = 0; i < bin_count; i++)
for (size_t x : bins)
{
if (bins[i] < 10)
if (x < 100)
{
cout << " " << bins[i] << "|";
for (int j = 0; j < bins[i]; j++)
cout << " ";
if (x < 10)
{
cout << "*";
cout << " ";
}
cout << endl;
}
else if (bins[i] < 100)
cout << x << "|";
for (size_t i = 0; i < x; i++)
{
cout << " " << bins[i] << "|";
for (int j = 0; j < bins[i]; j++)
{
cout << "*";
}
cout << endl;
cout << "*";
}
else
{
cout << bins[i] << "|";
for (int j = 0; j < bins[i]; j++)
{
cout << "*";
}
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;
}

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