From df047b3197a38c7c3e6a541676e2878dbed58376 Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Fri, 4 Nov 2022 11:58:21 +0000 Subject: [PATCH] Unfinished thing --- main.cpp | 66 ++++++++++++++++++++++++-------------------------------- 1 file changed, 28 insertions(+), 38 deletions(-) diff --git a/main.cpp b/main.cpp index 59c6f19..42c198a 100644 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,12 @@ #include #include -#include +#include #include #include using string = std::string; -using wordcount = std::map; +using dlist = std::list; +using wordstat = std::map; //Place in header! template @@ -23,23 +24,24 @@ int prompti(string,bool*); double promptd(string,bool*); string prompts(string,bool*); double avg(std::list*); -void countw(std::list&, wordcount*); -void histogram(wordcount&); -void spacep(int); -void histogramp(int); +double med(std::list*); int main() { - printlm("Get histogram of input:"); - bool s {}; - std::list 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 *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 &values, wordcount *holder) { - for (const auto &c : values) { - ++(*holder)[c]; +double med(std::list *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("*"); -}