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>
|
#include <iomanip>
|
||||||
|
|
||||||
using string = std::string;
|
using string = std::string;
|
||||||
|
using wordcount = std::map<string,int>;
|
||||||
|
|
||||||
//Place in header!
|
//Place in header!
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -22,21 +23,25 @@ int prompti(string,bool*);
|
|||||||
double promptd(string,bool*);
|
double promptd(string,bool*);
|
||||||
string prompts(string,bool*);
|
string prompts(string,bool*);
|
||||||
double avg(std::list<double>*);
|
double avg(std::list<double>*);
|
||||||
void remdups(std::list<string>&);
|
void countw(std::list<string>&, wordcount*);
|
||||||
|
void histogram(int);
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
printlm("Remove duplicates for input:");
|
printlm("Get histogram of input:");
|
||||||
bool s {};
|
bool s {};
|
||||||
std::list<string> lst {};
|
std::list<string> lst {};
|
||||||
do {
|
do {
|
||||||
auto v = prompts("", &s);
|
auto v = prompts("", &s);
|
||||||
if (s) lst.push_back(v);
|
if (s) lst.push_back(v);
|
||||||
} while (s);
|
} while (s);
|
||||||
printlm("Current data:");
|
printlm("Histogram:");
|
||||||
for (const auto c : lst) printlm(c);
|
wordcount wc {};
|
||||||
printlm("With Duplicates removed:");
|
countw(lst,&wc);
|
||||||
remdups(lst);
|
for (const auto c : wc) {
|
||||||
for (const auto c : lst) printlm(c);
|
printm(c.first);
|
||||||
|
printm(" ");
|
||||||
|
histogram(c.second);
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,15 +90,12 @@ double avg(std::list<double> *values) {
|
|||||||
return sum / values->size();
|
return sum / values->size();
|
||||||
}
|
}
|
||||||
|
|
||||||
void remdups(std::list<string> &values) {
|
void countw(std::list<string> &values, wordcount *holder) {
|
||||||
std::map<string,bool> has {};
|
for (const auto &c : values) {
|
||||||
auto itr {values.cbegin()};
|
++(*holder)[c];
|
||||||
while (itr != values.cend()) {
|
|
||||||
if (has[*itr]) {
|
|
||||||
itr = values.erase(itr);
|
|
||||||
} else {
|
|
||||||
has[*itr] = true;
|
|
||||||
++itr;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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