code: сделаны функции
Этот коммит содержится в:
114
main.cpp
114
main.cpp
@@ -3,77 +3,110 @@
|
|||||||
#include<cmath>
|
#include<cmath>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
int main()
|
|
||||||
{
|
struct Input {
|
||||||
size_t SCREEN_WIDTH;
|
vector<double> numbers;
|
||||||
|
size_t bin_count{};
|
||||||
|
size_t SCREEN_WIDTH{};
|
||||||
|
size_t number_count{};
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
Input
|
||||||
|
input_data() {
|
||||||
|
|
||||||
|
|
||||||
|
Input in;
|
||||||
size_t number_count;
|
cerr << "Enter number count: ";
|
||||||
cerr << "Enter number count: ";
|
cin >> in.number_count;
|
||||||
cin >> number_count;
|
in.numbers.resize(in.number_count);
|
||||||
|
for (size_t i = 0; i < in.number_count; i++) {
|
||||||
|
cin >> in.numbers[i];
|
||||||
|
}
|
||||||
|
cerr<<"Enter bin count:";
|
||||||
|
cin >> in.bin_count;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
cerr << "Screen width: ";
|
cerr << "Screen width: ";
|
||||||
cin >> SCREEN_WIDTH;
|
cin >> in.SCREEN_WIDTH;
|
||||||
if (SCREEN_WIDTH < 7) {
|
if (in.SCREEN_WIDTH < 7) {
|
||||||
cerr << "<7"; cerr << endl;
|
cerr << "<7"; cerr << endl;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (SCREEN_WIDTH > 80) {
|
if (in.SCREEN_WIDTH > 80) {
|
||||||
cerr << ">80"; cerr << endl;
|
cerr << ">80"; cerr << endl;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SCREEN_WIDTH < (number_count / 3)){
|
if (in.SCREEN_WIDTH < (in.number_count / 3)){
|
||||||
cerr << "<number_count/3"; cerr << endl;
|
cerr << "<number_count/3"; cerr << endl;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
return in;
|
||||||
|
}
|
||||||
|
|
||||||
vector<double> numbers(number_count);
|
void
|
||||||
cerr << "Enter numbers: ";
|
find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||||
for (int i = 0; i < number_count; i++)
|
min = numbers[0];
|
||||||
{
|
max = numbers[0];
|
||||||
cin >> numbers[i];
|
for (double x : numbers) {
|
||||||
|
if (x < min) {
|
||||||
}
|
|
||||||
double min = numbers[0];
|
|
||||||
double max = numbers[0];
|
|
||||||
for (double x : numbers)
|
|
||||||
{
|
|
||||||
if (x < min)
|
|
||||||
min = x;
|
min = x;
|
||||||
else if (x > max)
|
}
|
||||||
|
else if (x > max) {
|
||||||
max = x;
|
max = x;
|
||||||
}
|
}
|
||||||
size_t bin_count;
|
}
|
||||||
cerr<<"Enter bin count:";
|
}
|
||||||
cin >> bin_count;
|
|
||||||
vector<size_t> bins(bin_count);
|
std::vector<double>
|
||||||
|
make_histogram(const std::vector<double>& numbers, size_t bin_count) {
|
||||||
|
vector<double> bins(bin_count);
|
||||||
|
|
||||||
|
double min = 0, max = 0;
|
||||||
|
find_minmax(numbers, min, max);
|
||||||
|
|
||||||
double bin_size = (max - min) / bin_count;
|
double bin_size = (max - min) / bin_count;
|
||||||
for (size_t i = 0; i < number_count; i++)
|
for (int i = 0; i < numbers.size(); i++) {
|
||||||
{
|
|
||||||
bool found = false;
|
bool found = false;
|
||||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++)
|
for (int j = 0; (j < bin_count - 1) && !found; j++) {
|
||||||
{
|
auto lo = min + j * bin_size;
|
||||||
auto low = min + j * bin_size;
|
auto hi = min + (j + 1) * bin_size;
|
||||||
auto high = min + (j + 1) * bin_size;
|
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
|
||||||
if ((low <= numbers[i]) && (numbers[i] < high))
|
|
||||||
{
|
|
||||||
bins[j]++;
|
bins[j]++;
|
||||||
found = true;
|
found = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!found)
|
if (!found) {
|
||||||
{
|
|
||||||
bins[bin_count - 1]++;
|
bins[bin_count - 1]++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return bins;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
auto in = input_data();
|
||||||
|
|
||||||
|
double min, max;
|
||||||
|
|
||||||
|
find_minmax(in.numbers, min, max);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const size_t MAX_ASTERISK = in.SCREEN_WIDTH - 3 - 1;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
double mxbins = bins[0];
|
double mxbins = bins[0];
|
||||||
|
|
||||||
@@ -88,7 +121,7 @@ int main()
|
|||||||
k = MAX_ASTERISK / mxbins;
|
k = MAX_ASTERISK / mxbins;
|
||||||
else
|
else
|
||||||
k = 1;
|
k = 1;
|
||||||
for (size_t i = 0; i < bin_count; i++)
|
for (size_t i = 0; i < in.bin_count; i++)
|
||||||
{
|
{
|
||||||
if (bins[i] < 10) {
|
if (bins[i] < 10) {
|
||||||
cout << " " << bins[i] << "|";
|
cout << " " << bins[i] << "|";
|
||||||
@@ -108,3 +141,4 @@ int main()
|
|||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user