fork(3) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct tm_ymd_641_t
  5. {
  6. int y;
  7. int m;
  8. int d;
  9. } tm_ymd_641;
  10.  
  11. int leapyear(int year)
  12. {
  13. return (((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) ? 1 : 0);
  14. }
  15.  
  16. int eom(const tm_ymd_641 date)
  17. {
  18. static const int daysinmonth[2][12] =
  19. {
  20. {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  21. {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  22. };
  23. return daysinmonth[leapyear(date.y)][date.m - 1];
  24. }
  25.  
  26. int iseom(const tm_ymd_641 date)
  27. {
  28. return date.d == eom(date) ? 1 : 0;
  29. }
  30.  
  31. int isvaliddate(const tm_ymd_641 date)
  32. {
  33. return 1 <= date.m && date.m <= 12 && 1 <= date.d && date.d <= eom(date) ? 1 : 0;
  34. }
  35.  
  36. tm_ymd_641 parse_date(char date[])
  37. {
  38. tm_ymd_641 ymd_date;
  39.  
  40. ymd_date.d = atoi(&date[6]);
  41. date[6] = '\0';
  42. ymd_date.m = atoi(&date[4]);
  43. date[4] = '\0';
  44. ymd_date.y = atoi(date);
  45.  
  46. return ymd_date;
  47. }
  48.  
  49. int main(void)
  50. {
  51. char date[9];
  52. tm_ymd_641 ymd;
  53.  
  54. printf("YYYYMMDD:");
  55. scanf("%s", date);
  56. ymd = parse_date(date);
  57. if (!isvaliddate(ymd))
  58. {
  59. puts("Input date is invalid!");
  60. abort();
  61. }
  62. if (iseom(ymd))
  63. {
  64. if (ymd.m == 12)
  65. {
  66. ymd.y++;
  67. ymd.m = 1;
  68. }
  69. else
  70. {
  71. ymd.m++;
  72. }
  73. ymd.d = 1;
  74. }
  75. else
  76. {
  77. ymd.d++;
  78. }
  79. printf("Next day is %d/%d/%d\n", ymd.y, ymd.m, ymd.d);
  80.  
  81. return 0;
  82. }
  83.  
Success #stdin #stdout 0.01s 1724KB
stdin
20120913
stdout
YYYYMMDD:Next day is 2012/9/14