Unfinished thing
This commit is contained in:
parent
4228d34719
commit
df047b3197
66
main.cpp
66
main.cpp
@ -1,11 +1,12 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <iomanip>
|
||||
|
||||
using string = std::string;
|
||||
using wordcount = std::map<string,int>;
|
||||
using dlist = std::list<double>;
|
||||
using wordstat = std::map<string,dlist*>;
|
||||
|
||||
//Place in header!
|
||||
template <typename T>
|
||||
@ -23,23 +24,24 @@ int prompti(string,bool*);
|
||||
double promptd(string,bool*);
|
||||
string prompts(string,bool*);
|
||||
double avg(std::list<double>*);
|
||||
void countw(std::list<string>&, wordcount*);
|
||||
void histogram(wordcount&);
|
||||
void spacep(int);
|
||||
void histogramp(int);
|
||||
double med(std::list<double>*);
|
||||
|
||||
int main() {
|
||||
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("Histogram:");
|
||||
wordcount wc {};
|
||||
countw(lst,&wc);
|
||||
histogram(wc);
|
||||
printlm("Get stats of inputs:");
|
||||
bool s1 {};
|
||||
bool s2 {};
|
||||
wordstat stat {};
|
||||
do {,
|
||||
auto v1 = prompts("Enter a word: ", &s1);
|
||||
auto v2 = promptd("Enter a double: ", &s2);
|
||||
if (s1 && s2) {
|
||||
if (stat[v1] == null) {
|
||||
stat[v1] = new dlist {};
|
||||
}
|
||||
stat[v1].push_back(v2);
|
||||
}
|
||||
} while (s1 && s2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -83,31 +85,19 @@ string prompts(string message, bool *status) {
|
||||
}
|
||||
|
||||
double avg(std::list<double> *values) {
|
||||
if (values->size() < 1) return 0.0;
|
||||
double sum {};
|
||||
for (const auto c : *values) sum += c;
|
||||
return sum / values->size();
|
||||
}
|
||||
|
||||
void countw(std::list<string> &values, wordcount *holder) {
|
||||
for (const auto &c : values) {
|
||||
++(*holder)[c];
|
||||
double med(std::list<double> *values) {
|
||||
if (values->size() < 1) return 0.0;
|
||||
sort(values->begin(), values->end());
|
||||
auto hw {values->size()/2};
|
||||
if (values->size()%2 == 1) {
|
||||
return values->at(hw);
|
||||
} else {
|
||||
return (values->at(hw - 1) + values->at(hw)) / 2.0;
|
||||
}
|
||||
}
|
||||
|
||||
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("*");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user