/* Program 9-4

   Program to determine tomorrow's date */

struct date
 {
  int month;
  int day;
  int year;
 };

struct months
 {
  char month_name[9];
 };
 
struct date date_update (struct date);
int number_of_days (struct date);
int is_leap_year (struct date);

main ()
 {
   struct date this_day, next_day;
   static struct months months_of_year[12] =
	{ {"January"}, {"February"}, {"March"},
	  {"April"}, {"May"}, {"June"},
	  {"July"}, {"August"}, {"September"},
	  {"October"}, {"November"}, {"December"} };

   printf ("Enter today's date (mm dd yyyy):\n");
   scanf ("%d%d%d", &this_day.month, &this_day.day, &this_day.year);
   printf ("Today's date is:    %s %d, %d\n",
	    months_of_year[this_day.month - 1].month_name,
	    this_day.day, this_day.year);

   next_day = date_update (this_day);

   printf ("Tomorrow's date is: %s %d, %d\n",
	    months_of_year[next_day.month - 1].month_name,
	    next_day.day, next_day.year);
 }

  /* Function to calculate tomorrow's date */

  struct date date_update (struct date today)
   {
    struct date tomorrow;

    if (today.day != number_of_days (today))
     {
      tomorrow.day = today.day + 1;
      tomorrow.month = today.month;
      tomorrow.year = today.year;
     }
    else if (today.month == 12)             /* end of year */
     {
      tomorrow.day = 1;
      tomorrow.month = 1;
      tomorrow.year = today.year + 1;
     }
    else
     {                                      /* end of month */
      tomorrow.day = 1;
      tomorrow.month = today.month + 1;
      tomorrow.year = today.year;
     }

    return (tomorrow);
   }

/* Function to find the number of days in a month */

int number_of_days (struct date d)
 {
  int answer;
  static int days_per_month[12] =
       {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

  if (is_leap_year (d) && d.month == 2)
    answer = 29;
  else
    answer = days_per_month[d.month - 1];

  return (answer);
 }

/* Function to determine if it is a leap year */

int is_leap_year (struct date d)
 {
  int leap_year_flag;

  if ((d.year % 4 == 0  &&  d.year % 100 != 0)  ||
       d.year % 400 == 0)
    leap_year_flag = 1;                            /* It is a leap year */
  else
    leap_year_flag = 0;                            /* Not a leap year */

  return (leap_year_flag);
 }


/* Program Output

Enter today's date (mm dd yyyy):
2 28 1984
Today's date is: February 28, 1984
Tomorrow's date is: February 29, 1984

*/

