code: созданы файлы histogram и text
Этот коммит содержится в:
112
main.cpp
112
main.cpp
@@ -1,6 +1,7 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include "histogram.h"
|
||||
#include "text.h"
|
||||
|
||||
using namespace std;
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
@@ -31,114 +32,9 @@ Input input_data()
|
||||
return in;
|
||||
|
||||
}
|
||||
void find_minmax(const vector<double> &numbers, double &min, double &max)
|
||||
|
||||
int main()
|
||||
{
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for ( double x : numbers )
|
||||
{
|
||||
if ( x < min )
|
||||
{
|
||||
min = x;
|
||||
}
|
||||
else if ( x > max )
|
||||
{
|
||||
max = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vector<size_t> make_histogram(const vector<double> &numbers, size_t bin_count)
|
||||
{
|
||||
double min = numbers[0];
|
||||
double max = numbers[0];
|
||||
find_minmax(numbers, min, max);
|
||||
double bin_size = (max - min) / bin_count;
|
||||
|
||||
vector<size_t> bins (bin_count);
|
||||
for (size_t i=0; i < numbers.size(); i++)
|
||||
{
|
||||
bool found = false;
|
||||
for (size_t j = 0; ( j < bin_count - 1) && !found; j++)
|
||||
{
|
||||
auto lo = min + j * bin_size;
|
||||
auto hi = min + (j + 1) * bin_size;
|
||||
if (lo <= numbers[i] && (numbers[i] < hi))
|
||||
{
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found)
|
||||
{
|
||||
bins[bin_count-1]++;
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
||||
void show_histogram_text(const vector<size_t> &bins)
|
||||
{
|
||||
size_t maxbin = bins[0];
|
||||
for (size_t i=1; i < bins.size(); i++){
|
||||
if (maxbin < bins[i]){
|
||||
maxbin = bins[i];
|
||||
}
|
||||
}
|
||||
if (maxbin <= MAX_ASTERISK){
|
||||
for (size_t i=0; i < bins.size(); i++){
|
||||
if ((bins[i] < 1000)&&(bins[i] > 99)){
|
||||
cout << bins[i] << "|";
|
||||
for ( size_t j=0; j < bins[i]; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
} else if ((bins[i] < 100)&&(bins[i]>9)) {
|
||||
cout << " " << bins[i] << "|";
|
||||
for ( size_t j=0; j < bins[i]; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
} else if ( bins[i] < 10 ){
|
||||
cout << " " << bins[i]<< "|";
|
||||
for ( size_t j=0; j < bins[i]; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (size_t i=0; i < bins.size(); i++){
|
||||
size_t heightG= MAX_ASTERISK * (static_cast<double>(bins[i]) / maxbin);
|
||||
|
||||
if ((bins[i] < 1000)&&(bins[i] > 99)){
|
||||
cout << bins[i] << "|";
|
||||
for ( size_t j=0; j < heightG; j++ ){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
} else if ((bins[i] < 100)&&(bins[i]>9)) {
|
||||
cout << " " << bins[i] << "|";
|
||||
for (size_t j=0; j < heightG; j++){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
} else if (bins[i] < 10){
|
||||
cout << " " << bins[i]<< "|";
|
||||
|
||||
for (size_t j=0; j < heightG; j++){
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
|
||||
Input in = input_data();
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user