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