2. Структурирование программы при помощи функций

main
ShevchukDS 2 недель назад
Родитель 4475c84c98
Сommit f6bdfa4bf2

@ -1,100 +1,102 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
using namespace std; using namespace std;
int main() struct Input {
{ vector<double> numbers;
const size_t SCREEN_WIDTH = 80; size_t bin_count = 0;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; };
char a,b,c;
cout << "viravnit = ";
cin >> a;
cout << "osi = ";
cin >> b;
cout << "risunok = ";
cin >> c;
cout << "viravnit = " << a << '\n'
<< "osi = " << b << '\n'
<< "risunok = " << c << '\n';
if (a == '\t' || a == '\n' || b == '\t' || b == '\n' || c == '\t' || c == '\n') {
cout << "ERROR!";
return 0;
}
int number_count, bucket; Input input_data() {
Input in;
int number_count;
do do {
{ cerr << "Enter number count: ";
cerr << "Enter number count: "; cin >> number_count; cin >> number_count;
} } while (number_count < 1);
while (number_count < 1);
do do {
{ cerr << "Enter bucket: ";
cerr << "Enter bucket: "; cin >> bucket; cin >> in.bin_count;
} } while (in.bin_count < 1);
while (bucket < 1);
cerr << "\n"; cerr << "\n";
vector <double> numbers(number_count); in.numbers.resize(number_count);
for (int i = 0; i < number_count; i++) cin >> numbers[i]; for (int i = 0; i < number_count; i++) {
cin >> in.numbers[i];
}
return in;
}
float min = numbers[0]; void find_minmax(const vector<double>& numbers, double& min, double& max) {
float max = numbers[0]; if (numbers.empty()) return;
for (float x : numbers) min = numbers[0];
{ max = numbers[0];
for (double x : numbers) {
if (x < min) min = x; if (x < min) min = x;
else if (x > max) max = x; if (x > max) max = x;
}
} }
float k = (max-min)/bucket; vector<int> make_histogram(const vector<double>& numbers, size_t bin_count) {
double min, max;
vector <int> stolb(bucket); find_minmax(numbers, min, max);
for (int j = 0; j < bucket; j++) stolb[j] = 0; float k = (max - min) / bin_count;
vector<int> bins(bin_count, 0);
for (int i = 0; i < number_count; i++) for (double num : numbers) {
{
bool flag = false; bool flag = false;
for (int j = 0; (j < bucket && !flag); j++) for (size_t j = 0; j < bin_count && !flag; j++) {
{ if (num >= (min + k * j) && num < (min + k * (j + 1))) {
if (numbers[i] >= (min+k*j) && numbers[i] < (min+k*(1+j))) bins[j]++;
{
stolb[j]++;
flag = true; flag = true;
} }
} }
if (!flag) stolb[bucket-1]++; if (!flag) bins[bin_count - 1]++;
}
return bins;
} }
int maxlen = 0; void show_histogram_text(const vector<int>& bins) {
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
for (int j = 0; j < bucket; j++) int max_count = 0;
{ for (int count : bins) {
if (maxlen<stolb[j]) maxlen = stolb[j]; if (count > max_count) max_count = count;
} }
for (int j = 0; j < bucket; j++) for (size_t j = 0; j < bins.size(); j++) {
{ if (bins[j] < 100) cout << " ";
if (stolb[j] < 100) cout << a; if (bins[j] < 10) cout << " ";
if (stolb[j] < 10) cout << a; cout << bins[j] << "|";
cout << stolb[j] << b;
size_t height = stolb[j]; size_t height = bins[j];
if (maxlen > MAX_ASTERISK) if (max_count > MAX_ASTERISK) {
{ if (max_count != bins[j]) {
if (maxlen != stolb[j]) height = MAX_ASTERISK * (static_cast<float>(stolb[j])/maxlen); height = static_cast<size_t>(MAX_ASTERISK * (static_cast<float>(bins[j]) / max_count));
else if (maxlen == stolb[j]) height = MAX_ASTERISK; }
else if (max_count == bins[j]) {
height = MAX_ASTERISK;
}
} }
for (int i = 0; i < height; i++) cout << c;
for (size_t i = 0; i < height; i++) cout << "*";
cout << "\n"; cout << "\n";
} }
}
int main() {
auto input = input_data();
auto bins = make_histogram(input.numbers, input.bin_count);
show_histogram_text(bins);
return 0; return 0;
} }

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