#include "text.h" #include #include using namespace std; void show_histogram_text(vector bins, size_t bin_count) { const size_t SCREEN_WIDTH = 80; const size_t MAX_ASTERISK = SCREEN_WIDTH - 15; size_t max_bin = bins[0]; size_t total_count = 0; for (size_t i = 0; i < bin_count; i++) { if (bins[i] > max_bin) { max_bin = bins[i]; } total_count += bins[i]; } for (size_t i = 0; i < bin_count; i++) { cout << setw(3) << bins[i] << " |"; size_t bar_length; if (max_bin > MAX_ASTERISK) { bar_length = static_cast((static_cast(bins[i]) / max_bin) * MAX_ASTERISK); } else { bar_length = bins[i]; } for (size_t j = 0; j < bar_length; j++) { cout << "*"; } if (total_count > 0) { int percentage = static_cast((static_cast(bins[i]) / total_count) * 100 + 0.5); cout << setw(SCREEN_WIDTH - 7 - bar_length) << right << percentage << "%"; } else { cout << setw(SCREEN_WIDTH - 7 - bar_length) << right << "0%"; } cout << endl; } }