1. #include <iostream>
  2. #include <fstream>
  3. #include <map>
  4. #include <set>
  5. #include <string>
  6. #include <cstdlib>
  7. using namespace std;
  8. bool younger(string s1, string s2) {
  9. string y1 = s1.substr(s1.rfind('-')+1);
  10. string y2 = s2.substr(s2.rfind('-')+1);
  11. if(y1 < y2) return true;
  12. else if(y1 > y2) return false;
  13. string m1 = s1.substr(s1.find('-')+1, s1.rfind('-')-s1.find('-')-1);
  14. string m2 = s2.substr(s2.find('-')+1, s2.rfind('-')-s2.find('-')-1);
  15. if(m1 < m2) return true;
  16. else if(m1 > m2) return false;
  17. string d1 = s1.substr(0, s1.find('-'));
  18. string d2 = s2.substr(0, s2.find('-'));
  19. return d1 < d2;
  20. }
  21. int main() {
  22. ifstream in("lifepath.txt");
  23. map<int, int> pathcounts;
  24. set<string> bdays;
  25. string oldest, youngest;
  26. map<string, int> years;
  27. string line;
  28. while(getline(in, line)) {
  29. string monthday = line.substr(0, line.rfind('-'));
  30. bdays.insert(monthday);
  31. string num = line;
  32. while(num.find('-') != string::npos) num.erase(num.find('-'));
  33. int path = atoi(num.c_str()) % 9 + 1;
  34. pathcounts[path]++;
  35. if(youngest == "" || younger(line, youngest)) youngest = line;
  36. if(oldest == "" || younger(oldest, line)) oldest = line;
  37. years[line.substr(line.rfind('-')+1)]++;
  38. }
  39. for(int i = 1; i <= 9; i++) {
  40. cout << i << ": " << pathcounts[i] << endl;
  41. }
  42. cout << endl;
  43. cout << "The oldest birthday is " << oldest << endl;
  44. cout << "The youngest birthday is " << youngest << endl;
  45. cout << endl;
  46. cout << "There are " << bdays.size() << " unique birthdays" << endl;
  47. cout << endl;
  48. string modeyear = "";
  49. int modeyearcount = 0;
  50. for(map<string, int>::iterator it = years.begin(); it != years.end(); it++) {
  51. if(it->second > modeyearcount) {
  52. modeyear = it->first;
  53. modeyearcount = it->second;
  54. }
  55. else if(it->second == modeyearcount) modeyear += " " + it->first;
  56. }
  57. cout << "The mode of the birthyears is " << modeyear;
  58. cout << ", appearing " << modeyearcount << " times" << endl;
  59. return 0;
  60. }

main.cpp