fork download
  1. #include <iostream>
  2. #include<string>
  3. #include <cctype>
  4. #include <stdexcept>
  5.  
  6. struct day_type
  7. {
  8. enum weekday { SUN, MON, TUE, WED, THU, FRI, SAT };
  9.  
  10. day_type(weekday day) : _day(day) {}
  11. day_type(std::string day);
  12.  
  13. day_type prev() const { return dec(_day); }
  14. day_type next() const { return inc(_day); }
  15.  
  16. day_type operator+(unsigned n_days) { return weekday((_day + n_days) % 7); }
  17. day_type operator-(unsigned n_days) { return *this; /* I leave this to you! */ }
  18.  
  19. operator std::string() const;
  20.  
  21. private:
  22. weekday _day;
  23.  
  24. static weekday dec(weekday d) { return d == 0 ? weekday(6) : weekday(d-1); }
  25. static weekday inc(weekday d) { return weekday((d + 1) % 7);}
  26. };
  27.  
  28. std::string to_string(day_type::weekday day)
  29. {
  30. static const char* days [] =
  31. {
  32. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
  33. };
  34.  
  35. return days[day];
  36. }
  37.  
  38. void to_lower(std::string& str)
  39. {
  40. for (auto&ch : str)
  41. ch = std::tolower(ch);
  42. }
  43.  
  44. day_type::operator std::string() const
  45. {
  46. return to_string(_day);
  47. }
  48.  
  49. day_type::day_type(std::string day)
  50. {
  51. static const std::string days [] =
  52. {
  53. "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"
  54. };
  55.  
  56. unsigned matches = 0;
  57. unsigned match_index;
  58.  
  59. to_lower(day);
  60.  
  61. for (unsigned index=0; index != 7 && matches <= 1; ++index)
  62. {
  63. if (days[index].find(day) != std::string::npos)
  64. {
  65. match_index = index;
  66. ++matches;
  67. }
  68. }
  69.  
  70. if (matches != 1)
  71. throw std::invalid_argument("\"" + day + "\" is not a valid day");
  72.  
  73. _day = weekday(match_index);
  74. }
  75.  
  76. int main()
  77. {
  78. const char* day_prompt = "Enter a day of the week (quit to stop):\n";
  79. std::string today;
  80.  
  81. while (std::cout << day_prompt && std::cin >> today && today != "quit")
  82. {
  83. try
  84. {
  85. day_type day(today);
  86.  
  87. std::cout << "Day is " << std::string(day) << ".\n";
  88. std::cout << "Previous day was " << std::string(day.prev()) << ".\n";
  89. std::cout << "Next day is " << std::string(day.next()) << ".\n";
  90.  
  91.  
  92. std::cout << "How many days would you like to add?\n";
  93. unsigned to_add;
  94. std::cin >> to_add;
  95.  
  96. std::cout << "In " << to_add << " days it will be " ;
  97. std::cout << std::string(day + to_add) << ".\n\n";
  98. }
  99.  
  100. catch (std::exception & ex)
  101. {
  102. std::cout << "ERROR: " << ex.what() << "\n\n";
  103. }
  104. }
  105. }
Success #stdin #stdout 0s 3436KB
stdin
Tuesday
7
tuesday
8
mon
10
T
9
F
31
quit
stdout
Enter a day of the week (quit to stop):
Day is Tuesday.
Previous day was Monday.
Next day is Wednesday.
How many days would you like to add?
In 7 days it will be Tuesday.

Enter a day of the week (quit to stop):
Day is Tuesday.
Previous day was Monday.
Next day is Wednesday.
How many days would you like to add?
In 8 days it will be Wednesday.

Enter a day of the week (quit to stop):
Day is Monday.
Previous day was Sunday.
Next day is Tuesday.
How many days would you like to add?
In 10 days it will be Thursday.

Enter a day of the week (quit to stop):
ERROR: "t" is not a valid day

Enter a day of the week (quit to stop):
ERROR: "9" is not a valid day

Enter a day of the week (quit to stop):
Day is Friday.
Previous day was Thursday.
Next day is Saturday.
How many days would you like to add?
In 31 days it will be Monday.

Enter a day of the week (quit to stop):