Skip to content
Snippets Groups Projects
Select Git revision
  • 2016ws
  • 2024ws default
  • 2023ws
  • 2022ws
  • 2021ws
  • 2020ws
  • 2018ws
  • 2019ws
  • 2017ws
9 results

incdate-9.c

Blame
  • incdate-9.c 1.01 KiB
    #include <stdio.h>
    
    typedef struct
    {
      char day, month;
      int year;
    }
    date;
    
    void set_date (date *d)
    {
      d->day = 28;
      d->month = 2;
      d->year = 2000;
    }
    
    void inc_date (date *d)
    {
      d->day++;
      int days_in_month = 31;
      if (d->month == 2)
        {
          int is_leap_year = 0;
          if (d->year % 4 == 0)
            {
              is_leap_year = 1;
              if (d->year % 100 == 0)
                {
                  is_leap_year = 0;
                  if (d->year % 400 == 0)
                    is_leap_year = 1;
                }
            }
          if (is_leap_year)
            days_in_month = 29;
          else
            days_in_month = 28;
        }
      else if (d->month == 4 || d->month == 6 || d->month == 9 || d->month == 11)
        days_in_month = 30;
      if (d->day > days_in_month)
        {
          d->month++;
          d->day = 1;
          if (d->month > 12)
            {
              d->year++;
              d->month = 1;
            }
        }
    }
    
    int main (void)
    {
      date today;
      set_date (&today);
      inc_date (&today);
      printf ("%d.%d.%d\n", today.day, today.month, today.year);
      return 0;
    }