Вы не можете выбрать более 25 тем
Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
130 строки
3.6 KiB
C++
130 строки
3.6 KiB
C++
//main.cpp -- the program gets number count, numbers and bin size, then builds a histogram.
|
|
//Task 15 -- make a scale under the histogram.
|
|
|
|
#include <iostream>
|
|
#include <vector>
|
|
|
|
using namespace std;
|
|
|
|
struct Input {
|
|
vector<double> numbers;
|
|
size_t bin_count{};
|
|
//size_t interval_task{};
|
|
};
|
|
|
|
|
|
Input
|
|
input_data() {
|
|
size_t number_count;
|
|
cin >> number_count;
|
|
|
|
Input in;
|
|
|
|
in.numbers.resize(number_count);
|
|
for (size_t i = 0; i < number_count; i++) {
|
|
cin >> in.numbers[i];
|
|
}
|
|
|
|
cin >> in.bin_count;
|
|
|
|
//cin >> in.interval_task;
|
|
return in;
|
|
}
|
|
|
|
void
|
|
find_minmax(const std::vector<double>& numbers, double& min, double& max) {
|
|
min = numbers[0];
|
|
for(double element : numbers)
|
|
{
|
|
if(element < min) {min = element;}
|
|
}
|
|
|
|
max = numbers[0];
|
|
for(double element : numbers)
|
|
{
|
|
if(element > max) {max = element;}
|
|
}
|
|
}
|
|
|
|
|
|
void
|
|
find_max(const std::vector<std::size_t>& numbers, std::size_t& max) {
|
|
max = numbers[0];
|
|
for(double element : numbers)
|
|
{
|
|
if(element > max) {max = element;}
|
|
}
|
|
}
|
|
|
|
void
|
|
show_histogram_text(const std::vector<std::size_t>& bins){
|
|
std::size_t max_bin;
|
|
find_max(bins, max_bin);
|
|
double modifier;
|
|
|
|
/*
|
|
* In case when maximum bin > 76, histogram will be scaled according to
|
|
* the formula "max_asterisk * (static_cast<double>(bins[i]) / max_bin)"
|
|
* or "bins[i] * (static_cast<double>(max_asterisk) / max_bin)".
|
|
* In other case, histogram won't be scaled, i.e, asterisk count depends on the current bin number.
|
|
*/
|
|
|
|
if(max_bin > 76) {modifier = static_cast<double>(74) / (max_bin);}
|
|
else {modifier = 1;}
|
|
|
|
for(std::size_t i = 0; i < bins.size(); ++i) //Histogram output with alignment, if necessary.
|
|
{
|
|
if(bins[i] >= 10)
|
|
{
|
|
if(bins[i] >= 100) {std::cout << bins[i] << '|';} //Output a three-digit number.
|
|
else {std::cout << ' ' << bins[i] << '|';} //Output a two-digit number with alignment.
|
|
}
|
|
else {std::cout << " " << bins[i] << '|';} //Output a single-digit number with alignment.
|
|
|
|
|
|
size_t height = modifier * bins[i]; //Height stands for the number of output asterisks.
|
|
for(size_t k = 0; k < height; ++k)
|
|
std::cout << '*';
|
|
std::cout << "\n";
|
|
}
|
|
}
|
|
|
|
std::vector<std::size_t>
|
|
make_histogram(const std::vector<double>& numbers, std::size_t bin_count){
|
|
double max;
|
|
double min;
|
|
|
|
find_minmax(numbers, min, max);
|
|
|
|
double bin_size = static_cast<double>(max - min) / (bin_count);
|
|
|
|
std::vector <std::size_t> bins(bin_count);
|
|
|
|
|
|
for(std::size_t i = 0; i < numbers.size(); ++i) //Checking if a number is in a bin.
|
|
{
|
|
bool found = false;
|
|
for(std::size_t j = 0; (j < bin_count - 1) && !found; ++j)
|
|
{
|
|
if( (numbers[i] >= (min + j * bin_size)) && //Where (min + j * bin_size) is equal to the lower border
|
|
(numbers[i] < (min + (j + 1) * bin_size)) ) //and (min + (j + 1) * bin_size) is equal to the higher border.
|
|
{
|
|
++bins[j];
|
|
found = true;
|
|
}
|
|
}
|
|
if(!found) {++bins[bin_count - 1];} //A special case when current number is equal to the maximum.
|
|
}
|
|
return bins;
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
auto in = input_data();
|
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
|
show_histogram_text(bins);
|
|
|
|
return 0;
|
|
}
|