diff --git a/cache.cpp b/cache.cpp index 2e9c2b2..59e5f94 100644 --- a/cache.cpp +++ b/cache.cpp @@ -64,6 +64,10 @@ void Cache::set_date(const std::tm& t) { std::strftime(tmp, 20, "%F", &date_tm); date = tmp; + +// this function should be run after set_date_hidden() + if (date_hidden_t == 0) throw 1; + age_when_found = (date_t - date_hidden_t) / (60*60*24); } void Cache::set_date_hidden(const std::tm& t) { diff --git a/cache.h b/cache.h index 873e807..5f72795 100644 --- a/cache.h +++ b/cache.h @@ -68,8 +68,8 @@ public: std::string origin; std::string owner; std::string owner_uuid; - std::time_t date_t; - std::time_t date_hidden_t; + std::time_t date_t = 0; + std::time_t date_hidden_t = 0; std::tm date_tm; std::tm date_hidden_tm; std::string year_month; @@ -80,6 +80,7 @@ public: std::string day_of_week; std::string date; std::string date_hidden; + int age_when_found; void set_date(const std::tm& t); void set_date_hidden(const std::tm& t); diff --git a/common.cpp b/common.cpp index a6b6b63..02b1993 100644 --- a/common.cpp +++ b/common.cpp @@ -237,3 +237,29 @@ long int get_num(char c, char* opt) { std::exit(EXIT_FAILURE); } } + +void average_html(const Caches& cc, float Cache::*ptr, const std::string& caption) { + std::cout << "
\n"; + std::cout << "Average " << caption << ": " << average(cc, ptr) << "
\n"; + std::cout << "
\n"; +} + +void average_html(const Caches& cc, int Cache::*ptr, const std::string& caption) { + std::cout << "
\n"; + std::cout << "Average " << caption << ": " << average(cc, ptr) << "
\n"; + std::cout << "
\n"; +} + +float average(const Caches& cc, float Cache::*ptr) { + float count = 0; + for (auto el : cc) + count += el.*ptr; + return count / cc.size(); +} + +float average(const Caches& cc, int Cache::*ptr) { + int count = 0; + for (auto el : cc) + count += el.*ptr; + return (1.0 * count) / cc.size(); +} diff --git a/common.h b/common.h index 58a0dba..f1ccd08 100644 --- a/common.h +++ b/common.h @@ -11,3 +11,8 @@ void show_nested_histogram(const Caches& cc, std::string Cache::*ptr, std::strin int find_streak(const std::multimap& cc, std::tm& start); long int get_num(char c, char* opt); + +void average_html(const Caches& cc, float Cache::*ptr, const std::string& caption); +void average_html(const Caches& cc, int Cache::*ptr, const std::string& caption); +float average(const Caches& cc, float Cache::*ptr); +float average(const Caches& cc, int Cache::*ptr); diff --git a/geostat.cpp b/geostat.cpp index c71a9ed..70d83d0 100644 --- a/geostat.cpp +++ b/geostat.cpp @@ -504,6 +504,16 @@ int main(int argc, char** argv) { } std::cout << "\n"; + std::cout << "
\n"; + std::cout << "Average age of cache at the moment of finding: " << std::fixed << average(cc, &Cache::age_when_found) / 365 * 12 << " months
\n"; + std::cout << "Number of caches found within first 24h: " << std::count_if(cc.begin(), cc.end(), [&](auto& a) { return a.age_when_found == 0; }) << "
\n"; + std::cout << "Number of caches found within first 48h: " << std::count_if(cc.begin(), cc.end(), [&](auto& a) { return a.age_when_found <= 1; }) << "
\n"; + std::cout << "Number of caches found during first week: " << std::count_if(cc.begin(), cc.end(), [&](auto& a) { return a.age_when_found < 7; }) << "
\n"; + std::cout << "Number of caches found during first month: " << std::count_if(cc.begin(), cc.end(), [&](auto& a) { return a.age_when_found < 30; }) << "
\n"; + std::cout << "Number of caches found after more than 5 years: " << std::count_if(cc.begin(), cc.end(), [&](auto& a) { return a.age_when_found > 365 * 5; }) << "
\n"; + std::cout << "Number of caches found after more than 10 years: " << std::count_if(cc.begin(), cc.end(), [&](auto& a) { return a.age_when_found > 365 * 10; }) << "
\n"; + std::cout << "
\n"; + n = 1; std::cout << "

Caches with most recommendations

\n"; @@ -529,6 +539,8 @@ int main(int argc, char** argv) { } std::cout << "\n"; + average_html(cc, &Cache::fav, "number of recommendations"); + n = 1; std::cout << "

Caches with most recommendations (percentage)

\n"; @@ -577,6 +589,8 @@ int main(int argc, char** argv) { } std::cout << "\n"; + average_html(cc, &Cache::founds, "number of finds"); + n = 1; std::cout << "

Caches with least finds

\n"; @@ -627,7 +641,9 @@ int main(int argc, char** argv) { std::cout << "\n"; std::cout << "
\n"; - std::cout << "Total " << n << " combinations found out of 81 (" << std::setprecision(3) << n / 0.81 << "%).\n"; + std::cout << "Total " << n << " combinations found out of 81 (" << std::setprecision(3) << n / 0.81 << "%).
\n"; + std::cout << "Average difficulty: " << average(cc, &Cache::diff) << "
\n"; + std::cout << "Average terrain: " << average(cc, &Cache::terr) << "
\n"; std::cout << "
\n"; // Days matrix