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

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

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

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