code: структурирование

master
KuzmenkoEA 1 год назад
Родитель 9efb08cf8d
Сommit afe54c5221

@ -3,37 +3,45 @@
using namespace std; using namespace std;
int main() struct Input {
{ vector<double> numbers;
size_t bin_count{};
};
Input
input_data() {
size_t number_count; size_t number_count;
cerr << "Enter number count: "; cout << "Enter number count: ";
cin >> number_count; cin >> number_count;
Input in;
in.numbers.resize(number_count);
vector<double> numbers(number_count); vector<double> numbers(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; size_t bin_count;
cerr << "Enter count bin : "; cout << "Enter count bin : ";
cin >> bin_count; cin >> in.bin_count;
double min = numbers[0]; return in;
double max = numbers[0]; }
void
find_minmax(vector<double> numbers, double& min, double& max) {
min = numbers[0];
max = numbers[0];
for (double x : numbers) for (double x : numbers)
{ {
if(x < min) if(x < min){min = x;}
{ else if (x > max){max = x;}
min = x; }}
} vector <double> make_histogram(const vector<double>& numbers, size_t bin_count){
else if (x > max) vector<double> bins (bin_count);
{ double min = 0;
max = x; double max = 0;
} find_minmax(numbers, min, max);
}
vector<size_t> bins(bin_count);
double bin_size = (max - min) / bin_count; double bin_size = (max - min) / 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++)
@ -44,37 +52,44 @@ int main()
{ {
bins[j]++; bins[j]++;
found = true; found = true;
} }}
} if (!found){bins[bin_count - 1]++;}
if (!found)
{
bins[bin_count - 1]++;
}
} }
const size_t SCREEN_WIDTH = 80; return bins;}
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
size_t bin_max = bins[0]; show_histogram_text (const vector<double>& bins, size_t MAX_ASTERISK, size_t bin_count){
size_t bin_max = 0;
size_t height = 0;
for (double y : bins) for (double y : bins)
{ {
if (y > bin_max) if (y > bin_max){bin_max = y;}
{
bin_max = y;
}
} }
for (size_t i = bin_max; i > 0; i--) for (size_t bin: bins)
{ {
for (size_t bin: bins) size_t height = bin;
height = MAX_ASTERISK * (static_cast<double>(bin) / bin_max);
if (bin < 100)
{ {
if (i <= bin) cout << " ";
{ }
cout << " * "; if (bin < 10)
} {
else cout << " ";
{ }
cout << " "; cout << bin << "|";
} for (size_t i = 0; i < height; i++)
{
cout << "*";
} }
cout << endl; cout << endl;
} }
return 0; }
int main(){
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins, MAX_ASTERISK, in.bin_count );
return 0;
} }

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