diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..e235766 --- /dev/null +++ b/text.cpp @@ -0,0 +1,43 @@ +#include "text.h" + +using namespace std; + +void show_histogram_text(const vector& bins, size_t bin_count) { + const size_t SCREEN_WIDTH = 80; + const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + + size_t max_count = bins[0]; + for (size_t i = 0; i < bin_count; i++) + { + if (bins[i] > max_count) { + max_count = bins[i]; + } + } + + for (size_t i = 0; i < bin_count; i++) + { + if (bins[i]<10) { + cout << " " << bins[i] << "|"; + } + else if (bins[i]<100) { + cout << " " << bins[i] << "|"; + } + else if (bins[i]<1000) { + cout << bins[i] << "|"; + } + + size_t height; + + if (max_count<=MAX_ASTERISK) { + height = bins[i]; + } + else { + height = MAX_ASTERISK * (static_cast(bins[i]) / max_count); + } + + for (size_t j = 0; j < height; j++) { + cout << "*"; + } + cout << endl; + } +}