fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #include <iostream>
  5.  
  6. // type for representing day in a month: [1, 31]
  7. typedef int Day;
  8.  
  9. // type for representing a month: JAN for January, etc.
  10. enum Month {JAN =1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
  11.  
  12. // return next month
  13. Month inc(Month m);
  14.  
  15. // return previous month
  16. Month dec(Month m);
  17.  
  18. // check if given year is leap
  19. bool isLeap(int year);
  20.  
  21. // set next date (account leap years)
  22. void nextDate(int & year, Month & month, Day & day);
  23.  
  24. // return next month
  25. Month inc(Month m) {
  26. return (m == DEC) ? JAN : static_cast<Month> (m + 1);
  27. }
  28.  
  29. // return previous month
  30. Month dec(Month m) {
  31. return (m==JAN) ? DEC :static_cast<Month> (m-1);
  32. }
  33.  
  34. // check if given year is leap
  35. bool isLeap(int year) {
  36. if (year % 400==0)
  37. return true;
  38. else
  39. if ((year % 100!=0)&&(year % 4 ==0))
  40. return true;
  41. else
  42. return false;
  43.  
  44. }
  45.  
  46. // set next date (account leap years)
  47. void nextDate(int & year, Month & month, Day & day) {
  48. switch(day) {
  49. case 1:
  50. case 2:
  51. case 3:
  52. case 4:
  53. case 5:
  54. case 6:
  55. case 7:
  56. case 8:
  57. case 9:
  58. case 10:
  59. case 11:
  60. case 12:
  61. case 13:
  62. case 14:
  63. case 15:
  64. case 16:
  65. case 17:
  66. case 18:
  67. case 19:
  68. case 20:
  69. case 21:
  70. case 22:
  71. case 23:
  72. case 24:
  73. case 25:
  74. case 26:
  75. case 27: {
  76. day++;
  77. break;
  78. }
  79. case 28: {
  80. if (!(isLeap(year))&&(month==FEB)) {
  81. day=1;
  82. inc(month);
  83. }
  84. else day++;
  85. break;
  86. }
  87. case 29: {
  88. if (month==FEB){
  89. month = inc(month);
  90. day=1;
  91.  
  92. }
  93. else day++;
  94. break;
  95. }
  96. case 30: {
  97. if ((month==APR)||(month==JUN)||(month==SEP)||(month==NOV)) {
  98. day=1;
  99. inc(month);
  100. }
  101. else
  102. day++;
  103. break;
  104. }
  105. case 31: {
  106. if (month==DEC)
  107. year++;
  108. day=1;
  109. inc(month);
  110. break;
  111. }
  112. }
  113. }
  114.  
  115. using namespace std;
  116. int main() {
  117. int y = 2012;
  118. Month m = FEB;
  119. Day d = 28;
  120.  
  121. nextDate ( y, m, d );
  122.  
  123. cout << "was: " << y << " " << m << " " << d << endl;
  124.  
  125. nextDate ( y, m, d );
  126.  
  127. cout << "become: " << y << " " << m << " " << d << endl;
  128. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
was: 2012 2 29
become: 2012 3 1