Родитель
							
								
									36e46ca8cf
								
							
						
					
					
						Сommit
						1bff5bee6c
					
				@ -1,106 +1,9 @@
 | 
				
			|||||||
#include <iostream>
 | 
					#include "histogram.h"
 | 
				
			||||||
#include <vector>
 | 
					 | 
				
			||||||
#include <limits>
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
using namespace std;
 | 
					int main() {
 | 
				
			||||||
 | 
					 | 
				
			||||||
const size_t SCREEN_WIDTH = 80;
 | 
					 | 
				
			||||||
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;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    cerr << "Enter number count: ";
 | 
					 | 
				
			||||||
    cin >> number_count;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    in.numbers.resize(number_count);
 | 
					 | 
				
			||||||
    cerr << "Enter " << number_count << " numbers: ";
 | 
					 | 
				
			||||||
    for (size_t i = 0; i < number_count; i++) {
 | 
					 | 
				
			||||||
        cin >> in.numbers[i];
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    cerr << "Enter bin count: ";
 | 
					 | 
				
			||||||
    cin >> in.bin_count;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return in;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Ôóíêöèÿ ïîèñêà ìèíèìóìà è ìàêñèìóìà
 | 
					 | 
				
			||||||
void
 | 
					 | 
				
			||||||
find_minmax(const vector<double>& numbers, double& min, double& max) {
 | 
					 | 
				
			||||||
    min = numeric_limits<double>::max();
 | 
					 | 
				
			||||||
    max = numeric_limits<double>::lowest();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    for (double x : numbers) {
 | 
					 | 
				
			||||||
        if (x < min) min = x;
 | 
					 | 
				
			||||||
        if (x > max) max = x;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Ôóíêöèÿ ñîçäàíèÿ ãèñòîãðàììû
 | 
					 | 
				
			||||||
vector<size_t>
 | 
					 | 
				
			||||||
make_histogram(const vector<double>& numbers, size_t bin_count) {
 | 
					 | 
				
			||||||
    double min, max;
 | 
					 | 
				
			||||||
    find_minmax(numbers, min, max);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (max == min) {
 | 
					 | 
				
			||||||
        return vector<size_t>(bin_count, numbers.size());
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    double bin_size = (max - min) / bin_count;
 | 
					 | 
				
			||||||
    vector<size_t> bins(bin_count, 0);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    for (double x : numbers) {
 | 
					 | 
				
			||||||
        size_t bin_index = static_cast<size_t>((x - min) / bin_size);
 | 
					 | 
				
			||||||
        if (bin_index >= bin_count) bin_index = bin_count - 1;
 | 
					 | 
				
			||||||
        bins[bin_index]++;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return bins;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Ôóíêöèÿ âûâîäà ãèñòîãðàììû
 | 
					 | 
				
			||||||
void
 | 
					 | 
				
			||||||
show_histogram_text(const vector<size_t>& bins) {
 | 
					 | 
				
			||||||
    size_t max_count = 0;
 | 
					 | 
				
			||||||
    for (size_t count : bins) {
 | 
					 | 
				
			||||||
        if (count > max_count) max_count = count;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    cerr << "Bin counts:" << endl;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    for (size_t j = 0; j < bins.size(); j++) {
 | 
					 | 
				
			||||||
        size_t height = (max_count > MAX_ASTERISK)
 | 
					 | 
				
			||||||
            ? static_cast<size_t>(MAX_ASTERISK * (static_cast<double>(bins[j]) / max_count))
 | 
					 | 
				
			||||||
            : bins[j];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (bins[j] < 10) {
 | 
					 | 
				
			||||||
            cout << "  " << bins[j] << "|";
 | 
					 | 
				
			||||||
        } else if (bins[j] < 100) {
 | 
					 | 
				
			||||||
            cout << " " << bins[j] << "|";
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
            cout << bins[j] << "|";
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        for (size_t k = 0; k < height; k++) cout << "*";
 | 
					 | 
				
			||||||
        cout << endl;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
int
 | 
					 | 
				
			||||||
main() {
 | 
					 | 
				
			||||||
    auto in = input_data();
 | 
					    auto in = input_data();
 | 
				
			||||||
    auto bins = make_histogram(in.numbers, in.bin_count);
 | 
					    auto bins = make_histogram(in.numbers, in.bin_count);
 | 
				
			||||||
    show_histogram_text(bins);
 | 
					    show_histogram_text(bins);
 | 
				
			||||||
 | 
					 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
					Загрузка…
					
					
				
		Ссылка в новой задаче