diff options
author | Tom Smeding <tom.smeding@gmail.com> | 2019-09-14 13:41:48 +0200 |
---|---|---|
committer | Tom Smeding <tom.smeding@gmail.com> | 2019-09-14 13:41:48 +0200 |
commit | 5014d5aa0f98a8adf9c435b0d9768508b241beb2 (patch) | |
tree | 4b1f11360fa179b483a2176cedd873abe8ecf7ae | |
parent | 40ad7163a09b80d0ada0f6c4d5ad23427ea24665 (diff) |
-rw-r--r-- | histogram.cpp | 54 |
1 files changed, 36 insertions, 18 deletions
diff --git a/histogram.cpp b/histogram.cpp index c7bbc6a..25b40d3 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -10,7 +10,7 @@ static const char *argv0; void usage() { - std::cerr << "Usage: " << argv0 << " [-h] [<lowbound> <highbound>] [<nbins>]" << std::endl + std::cerr << "Usage: " << argv0 << " [-h] [-v] [<lowbound> <highbound>] [<nbins>]" << std::endl << "Prints a simple histogram of stdin data in your terminal, using" << std::endl << "either the given lower and upper bounds, or the minimum and" << std::endl << "maximum extracted from the data." << std::endl @@ -18,6 +18,9 @@ void usage() { << "to 10.)" << std::endl << "Data on stdin is assumed to be a list of floating-point values." << std::endl << std::endl + << " -v When passed, prints a histogram with vertical bars. This" << std::endl + << " usually fits more bars on a terminal, but is less informative" << std::endl + << " since this view omits exact frequency numbers." << std::endl << " -h Show this help." << std::endl; } @@ -47,14 +50,18 @@ int main(int argc, char **argv) { argv0 = argv[0]; const int64_t BARWIDTH = 80; + const int64_t BARHEIGHT = 24; + bool vertical = false; double low = 0, high = 1; // will be overwritten either from arguments or from data bool havebounds; int64_t nbins = 10; std::vector<char*> plainargs; for (int i = 1; i < argc; i++) { - if (strcmp(argv[i], "-h") == 0) { + if (strcmp(argv[i], "-v") == 0) { + vertical = true; + } else if (strcmp(argv[i], "-h") == 0) { usage(); return 0; } else { @@ -163,21 +170,32 @@ int main(int argc, char **argv) { } // Print histogram - char fullbar[BARWIDTH + 1]; - memset(fullbar, '#', BARWIDTH); - fullbar[BARWIDTH] = '\0'; - char emptybar[BARWIDTH + 1]; - memset(emptybar, ' ', BARWIDTH); - emptybar[BARWIDTH] = '\0'; - - for (size_t i = 0; i < histogram.size(); i++) { - int64_t tally = histogram[i]; - double binlow = low + (double)i / nbins * (high - low); - double binhigh = low + (double)(i + 1) / nbins * (high - low); - int64_t width = BARWIDTH * tally / maxtally; - char terminator = ")]"[i == histogram.size() - 1]; - std::cout << fullbar + BARWIDTH - width << emptybar + width - << " [" << std::setw(11) << binlow << " - " << std::setw(11) << binhigh << terminator - << " [" << tally << "]" << std::endl; + if (vertical) { + for (int y = BARHEIGHT; y >= 1; y--) { + for (size_t i = 0; i < histogram.size(); i++) { + std::cout << " #"[BARHEIGHT * histogram[i] / maxtally >= y]; + } + std::cout << std::endl; + } + std::cout << std::left << std::setw(nbins / 2) << low << ' ' + << std::right << std::setw(nbins - nbins / 2 - 1) << high << std::endl; + } else { + char fullbar[BARWIDTH + 1]; + memset(fullbar, '#', BARWIDTH); + fullbar[BARWIDTH] = '\0'; + char emptybar[BARWIDTH + 1]; + memset(emptybar, ' ', BARWIDTH); + emptybar[BARWIDTH] = '\0'; + + for (size_t i = 0; i < histogram.size(); i++) { + int64_t tally = histogram[i]; + double binlow = low + (double)i / nbins * (high - low); + double binhigh = low + (double)(i + 1) / nbins * (high - low); + int64_t width = BARWIDTH * tally / maxtally; + char terminator = ")]"[i == histogram.size() - 1]; + std::cout << fullbar + BARWIDTH - width << emptybar + width + << " [" << std::setw(11) << binlow << " - " << std::setw(11) << binhigh << terminator + << " [" << tally << "]" << std::endl; + } } } |