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