code: добавлены функции расчёта и вывода текстовой гистограммы
Этот коммит содержится в:
56
main.cpp
56
main.cpp
@@ -3,13 +3,15 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
struct Input {
|
struct Input
|
||||||
|
{
|
||||||
vector<double> numbers;
|
vector<double> numbers;
|
||||||
size_t bin_count{};
|
size_t bin_count{};
|
||||||
};
|
};
|
||||||
|
|
||||||
Input
|
Input
|
||||||
input_data() {
|
input_data()
|
||||||
|
{
|
||||||
size_t number_count;
|
size_t number_count;
|
||||||
cerr << "Enter number count: ";
|
cerr << "Enter number count: ";
|
||||||
cin >> number_count;
|
cin >> number_count;
|
||||||
@@ -25,10 +27,12 @@ input_data() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
find_minmax(const vector<double>& numbers, double& min, double& max) {
|
find_minmax(const vector<double>& numbers, double& min, double& max)
|
||||||
|
{
|
||||||
min = numbers[0];
|
min = numbers[0];
|
||||||
max = numbers[0];
|
max = numbers[0];
|
||||||
for (double x : numbers) {
|
for (double x : numbers)
|
||||||
|
{
|
||||||
if (x > max )
|
if (x > max )
|
||||||
{
|
{
|
||||||
max = x;
|
max = x;
|
||||||
@@ -40,20 +44,18 @@ find_minmax(const vector<double>& numbers, double& min, double& max) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
vector<size_t>
|
||||||
|
make_histogram(const vector<double>& numbers, size_t& bin_count)
|
||||||
{
|
{
|
||||||
const size_t SCREEN_WIDTH = 80;
|
|
||||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
||||||
Input in = input_data();
|
|
||||||
vector<size_t> bins(in.bin_count);
|
|
||||||
double min = 0;
|
double min = 0;
|
||||||
double max = 0;
|
double max = 0;
|
||||||
find_minmax(in.numbers, min, max);
|
find_minmax(numbers, min, max);
|
||||||
auto bin_size = (max - min)/in.bin_count;
|
auto bin_size = (max - min)/bin_count;
|
||||||
for (double number : in.numbers)
|
vector<size_t> bins(bin_count);
|
||||||
|
for (double number : numbers)
|
||||||
{
|
{
|
||||||
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;
|
||||||
@@ -65,15 +67,23 @@ int main()
|
|||||||
}
|
}
|
||||||
if (!found)
|
if (!found)
|
||||||
{
|
{
|
||||||
bins[in.bin_count - 1]++;
|
bins[bin_count - 1]++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return bins;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_text(const vector<size_t>& bins, size_t& bin_count) {
|
||||||
|
const size_t SCREEN_WIDTH = 80;
|
||||||
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||||
size_t max_count = 0;
|
size_t max_count = 0;
|
||||||
for (size_t s = 0; s < in.bin_count; s++) {
|
for (size_t s = 0; s < bin_count; s++)
|
||||||
|
{
|
||||||
if (bins[s] > max_count)
|
if (bins[s] > max_count)
|
||||||
{
|
{
|
||||||
max_count = bins[s];
|
max_count = bins[s];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (size_t bin : bins)
|
for (size_t bin : bins)
|
||||||
{
|
{
|
||||||
@@ -86,7 +96,8 @@ int main()
|
|||||||
cout << " ";
|
cout << " ";
|
||||||
}
|
}
|
||||||
cout << bin << "|";
|
cout << bin << "|";
|
||||||
if (max_count > MAX_ASTERISK) {
|
if (max_count > MAX_ASTERISK)
|
||||||
|
{
|
||||||
size_t count = bin;
|
size_t count = bin;
|
||||||
size_t height = MAX_ASTERISK * (static_cast<double>(count) / max_count);
|
size_t height = MAX_ASTERISK * (static_cast<double>(count) / max_count);
|
||||||
for (size_t t = 0; t < height; t++)
|
for (size_t t = 0; t < height; t++)
|
||||||
@@ -101,3 +112,10 @@ int main()
|
|||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
Input in = input_data();
|
||||||
|
vector<size_t> bins = make_histogram(in.numbers, in.bin_count);
|
||||||
|
show_histogram_text(bins, in.bin_count);
|
||||||
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user