basic histogram of input word count
This commit is contained in:
parent
ccee442f97
commit
5f5d16d890
34
main.cpp
34
main.cpp
@ -5,6 +5,7 @@
|
||||
#include <iomanip>
|
||||
|
||||
using string = std::string;
|
||||
using wordcount = std::map<string,int>;
|
||||
|
||||
//Place in header!
|
||||
template <typename T>
|
||||
@ -22,21 +23,25 @@ int prompti(string,bool*);
|
||||
double promptd(string,bool*);
|
||||
string prompts(string,bool*);
|
||||
double avg(std::list<double>*);
|
||||
void remdups(std::list<string>&);
|
||||
void countw(std::list<string>&, wordcount*);
|
||||
void histogram(int);
|
||||
|
||||
int main() {
|
||||
printlm("Remove duplicates for input:");
|
||||
printlm("Get histogram of input:");
|
||||
bool s {};
|
||||
std::list<string> lst {};
|
||||
do {
|
||||
auto v = prompts("", &s);
|
||||
if (s) lst.push_back(v);
|
||||
} while (s);
|
||||
printlm("Current data:");
|
||||
for (const auto c : lst) printlm(c);
|
||||
printlm("With Duplicates removed:");
|
||||
remdups(lst);
|
||||
for (const auto c : lst) printlm(c);
|
||||
printlm("Histogram:");
|
||||
wordcount wc {};
|
||||
countw(lst,&wc);
|
||||
for (const auto c : wc) {
|
||||
printm(c.first);
|
||||
printm(" ");
|
||||
histogram(c.second);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -85,15 +90,12 @@ double avg(std::list<double> *values) {
|
||||
return sum / values->size();
|
||||
}
|
||||
|
||||
void remdups(std::list<string> &values) {
|
||||
std::map<string,bool> has {};
|
||||
auto itr {values.cbegin()};
|
||||
while (itr != values.cend()) {
|
||||
if (has[*itr]) {
|
||||
itr = values.erase(itr);
|
||||
} else {
|
||||
has[*itr] = true;
|
||||
++itr;
|
||||
void countw(std::list<string> &values, wordcount *holder) {
|
||||
for (const auto &c : values) {
|
||||
++(*holder)[c];
|
||||
}
|
||||
}
|
||||
|
||||
void histogram(int num) {
|
||||
for (auto i = 1; i <= num; ++i) if (i == num) printlm("*"); else printm("*");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user