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