Вы не можете выбрать более 25 тем
Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
47 строки
1.2 KiB
C++
47 строки
1.2 KiB
C++
#include <vector>
|
|
#include <math.h>
|
|
#include <iostream>
|
|
#include <conio.h>
|
|
#include "text.h"
|
|
using namespace std;
|
|
void show_histogram_text(size_t& bin_count, vector<size_t>& bins){
|
|
vector<size_t> counts(bin_count);
|
|
for (size_t i = 0; i < bin_count; i++){
|
|
for (size_t j = 0; j < bins[i]; j++){
|
|
counts[i]++;
|
|
}
|
|
}
|
|
size_t max_count = counts[0];
|
|
for (size_t i=0; i<bin_count; i++){
|
|
if(counts[i]>max_count){
|
|
max_count = counts[i];
|
|
}
|
|
}
|
|
const size_t screen_width = 80;
|
|
const size_t max_asterisk = screen_width - 3 - 1;
|
|
vector<size_t> heights(bin_count);
|
|
size_t height;
|
|
for (size_t i=0; i<bin_count; i++){
|
|
if (max_count > 76){
|
|
height = (max_asterisk*(static_cast<double>(counts[i])/max_count));
|
|
}
|
|
else {
|
|
height = counts[i];
|
|
}
|
|
heights[i] = height;
|
|
}
|
|
for (size_t i = 0; i < bin_count; i++){
|
|
if(bins[i]<100){
|
|
cout << " ";
|
|
}
|
|
if (bins[i]<10){
|
|
cout << " ";
|
|
}
|
|
cout << bins[i] << "|";
|
|
for (size_t j = 0; j < heights[i]; j++){
|
|
cout << "*";
|
|
}
|
|
cout << "\n";
|
|
}
|
|
}
|