code: обновление кода
Этот коммит содержится в:
77
lab34.cpp
77
lab34.cpp
@@ -1,35 +1,51 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int main() {
|
const size_t SCREEN_WIDTH = 80;
|
||||||
const size_t SCREEN_WIDTH = 80;
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
||||||
|
struct Input {
|
||||||
|
vector<double> numbers;
|
||||||
|
size_t bin_count{};
|
||||||
|
};
|
||||||
|
|
||||||
|
Input
|
||||||
|
input_data() {
|
||||||
|
Input in;
|
||||||
size_t number_count;
|
size_t number_count;
|
||||||
cerr << "Enter amount of numbers \n";
|
|
||||||
cin >> number_count;
|
cin >> number_count;
|
||||||
vector<double> numbers(number_count);
|
in.numbers.resize(number_count);
|
||||||
for (size_t i = 0; i < number_count; i++) {
|
for (size_t i = 0; i < number_count; i++) {
|
||||||
cerr << "Input number " << i + 1 << endl;
|
cin >> in.numbers[i];
|
||||||
cin >> numbers[i];
|
|
||||||
}
|
|
||||||
double minc, maxc;
|
|
||||||
minc = maxc = numbers[0];
|
|
||||||
for (size_t i = 1; i < number_count; i++) {
|
|
||||||
if (numbers[i] < minc) {
|
|
||||||
minc = numbers[i];
|
|
||||||
}
|
|
||||||
else if (numbers[i] > maxc) {
|
|
||||||
maxc = numbers[i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
size_t bin_count;
|
size_t bin_count;
|
||||||
cerr << "Input number of bins \n";
|
cin >> in.bin_count;
|
||||||
cin >> bin_count;
|
return in;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||||
|
min = numbers[0];
|
||||||
|
for (size_t i = 1; i < numbers.size(); i++) {
|
||||||
|
if (numbers[i] < min) {
|
||||||
|
min = numbers[i];
|
||||||
|
}
|
||||||
|
else if (numbers[i] > max) {
|
||||||
|
max = numbers[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<size_t>
|
||||||
|
make_histogram(const vector <double>& numbers, size_t bin_count) {
|
||||||
|
double minc, maxc;
|
||||||
|
find_minmax(numbers, minc, maxc);
|
||||||
vector<size_t> bins(bin_count);
|
vector<size_t> bins(bin_count);
|
||||||
double bin_size = (maxc - minc) / bin_count;
|
double bin_size = (maxc - minc) / bin_count;
|
||||||
size_t bin_max_size = 0;
|
size_t bin_max_size = 0;
|
||||||
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++) {
|
||||||
auto lo = minc + j * bin_size;
|
auto lo = minc + j * bin_size;
|
||||||
@@ -49,11 +65,22 @@ int main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return bins;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_text(vector<size_t> bins) {
|
||||||
|
size_t bin_max_size = 0;
|
||||||
|
for (auto bin : bins) {
|
||||||
|
if (bin_max_size < bin) {
|
||||||
|
bin_max_size = bin;
|
||||||
|
}
|
||||||
|
}
|
||||||
double k = double(MAX_ASTERISK) / bin_max_size;
|
double k = double(MAX_ASTERISK) / bin_max_size;
|
||||||
if (k > 1) {
|
if (k > 1) {
|
||||||
k = 1;
|
k = 1;
|
||||||
}
|
}
|
||||||
for (size_t bin = 0; bin < bin_count; bin++) {
|
for (size_t bin = 0; bin < bins.size(); bin++) {
|
||||||
if (bins[bin] < 100) {
|
if (bins[bin] < 100) {
|
||||||
cout << " ";
|
cout << " ";
|
||||||
}
|
}
|
||||||
@@ -61,10 +88,16 @@ int main() {
|
|||||||
cout << " ";
|
cout << " ";
|
||||||
}
|
}
|
||||||
cout << bins[bin] << "|";
|
cout << bins[bin] << "|";
|
||||||
for (size_t i = 0; i < bin * k; i++) {
|
for (size_t i = 0; i < bins[bin] * k; i++) {
|
||||||
cout << "*";
|
cout << "*";
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Input in = input_data();
|
||||||
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||||
|
show_histogram_text(bins);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Ссылка в новой задаче
Block a user