1
0

histogram prettified

This commit is contained in:
Captain ALM 2022-10-28 12:41:38 +01:00
parent 5f5d16d890
commit 4228d34719
3 changed files with 19 additions and 7 deletions

View File

@ -24,7 +24,9 @@ double promptd(string,bool*);
string prompts(string,bool*); string prompts(string,bool*);
double avg(std::list<double>*); double avg(std::list<double>*);
void countw(std::list<string>&, wordcount*); void countw(std::list<string>&, wordcount*);
void histogram(int); void histogram(wordcount&);
void spacep(int);
void histogramp(int);
int main() { int main() {
printlm("Get histogram of input:"); printlm("Get histogram of input:");
@ -37,11 +39,7 @@ int main() {
printlm("Histogram:"); printlm("Histogram:");
wordcount wc {}; wordcount wc {};
countw(lst,&wc); countw(lst,&wc);
for (const auto c : wc) { histogram(wc);
printm(c.first);
printm(" ");
histogram(c.second);
}
return 0; return 0;
} }
@ -96,6 +94,20 @@ void countw(std::list<string> &values, wordcount *holder) {
} }
} }
void histogram(int num) { void histogram(wordcount &wc) {
int maxl {};
for (const auto c : wc) if (c.first.length() > maxl) maxl = c.first.length();
for (const auto c : wc) {
printm(c.first);
spacep(1 + maxl - c.first.length());
histogramp(c.second);
}
}
void spacep(int num) {
for (auto i = 0; i < num; ++i) printm(" ");
}
void histogramp(int num) {
for (auto i = 1; i <= num; ++i) if (i == num) printlm("*"); else printm("*"); for (auto i = 1; i <= num; ++i) if (i == num) printlm("*"); else printm("*");
} }

BIN
main.o

Binary file not shown.

BIN
t

Binary file not shown.