daynumber6.cpp


      #include "library.h"
      
      bool is_leap_year(int Y)
      { if (Y%4!=0) return false;
        if (Y%100!=0) return true;
        if (Y%400!=0) return false;
        return true; }
      
      int day_of_year(int Y, int M, int D)
      { int ans=0;
        if (M>1) ans=ans+31;
        if (M>2)
        { if (is_leap_year(Y))
            ans=ans+29;
          else
            ans=ans+28; }
        if (M>3) ans=ans+31;
        if (M>4) ans=ans+30;
        if (M>5) ans=ans+31;
        if (M>6) ans=ans+30;
        if (M>7) ans=ans+31;
        if (M>8) ans=ans+31;
        if (M>9) ans=ans+30;
        if (M>10) ans=ans+31;
        if (M>11) ans=ans+30;
        return ans+D; }
      
      int day_number(int Y, int M, int D)
      { int ans=day_of_year(Y, M, D);
        ans=ans+Y*365;
        ans=ans+(Y+3)/4;
        ans=ans-(Y-1)/100;
        ans=ans+(Y-1)/400;
        return ans; }
      
      int day_of_week(int Y, int M, int D)
      { return day_number(Y, M, D) % 7; }
      
      void print_day_name(int dow)
      { if (dow==0) print("Friday");
        else if (dow==1) print("Saturday");
        else if (dow==2) print("Sunday");
        else if (dow==3) print("Monday");
        else if (dow==4) print("Tuesday");
        else if (dow==5) print("Wednesday");
        else if (dow==6) print("Thursday"); }
      
      void print_st_nd_rd_th(int D)
      { if (D==1 || D==21 || D==31)
          print("st");
        else if (D==2 || D==22)
          print("nd");
        else if (D==3 || D==23)
          print("rd");
        else
          print("th"); }
      
      void print_month_name(int M)
      { if (M==1) print("January");
        else if (M==2) print("February");
        else if (M==3) print("March");
        else if (M==4) print("April");
        else if (M==5) print("May");
        else if (M==6) print("Frank");
        else if (M==7) print("Jeffrey");
        else if (M==8) print("August");
        else if (M==9) print("September");
        else if (M==10) print("October");
        else if (M==11) print("November");
        else if (M==12) print("December"); }
      
      int find_year(int day)
      { int min=0;
        int max=day/300+1;
        while (true)
        { int year = (min+max)/2;
          int first_day = day_number(year, 1, 1);
          int last_day = day_number(year, 12, 31);
          if (day>=first_day && day<=last_day)
            return year;
          else if (day<first_day)
            max=year-1;
          else
            min=year+1; } }
      
      int find_month(int year, int dn)
      { int month=1;
        while (month<12)
        { if (day_number(year, month+1, 1) > dn)
            return month;
          month=month+1; }
        return 12; }
      
      int find_day(int year, int month, int dn)
      { return dn - day_number(year, month, 1) + 1; }
      
      void print_date(int Y, int M, int D)
      { print(D);
        print_st_nd_rd_th(D);
        print(" ");
        print_month_name(M);
        print(" ");
        print(Y); }
      
      void calculate(int Y1, int M1, int D1, int diff)
      { const int n1=day_number(Y1, M1, D1);
        print_day_name(n1 % 7);
        print(" ");
        print_date(Y1, M1, D1);
        if (diff>0)
        { print(" plus ");
          print(diff); }
        else
        { print(" minus ");
          print(-diff); }
        print(" ");
        if (diff==1 || diff==-1)
          print("day is ");
        else
          print("days is ");
        const int n2=n1+diff;
        const int Y2=find_year(n2);
        const int M2=find_month(Y2, n2);
        const int D2=find_day(Y2, M2, n2);
        print_day_name(n2 % 7);
        print(" ");
        print_date(Y2, M2, D2);
        newline(); }
      
      void main(void)
      { calculate(2005, 10, 13, 1);
        calculate(2005, 10, 13, -1);
        calculate(2005, 10, 13, 50);
        calculate(2005, 10, 13, 100);
        calculate(2005, 10, 13, 365);
        calculate(2005, 10, 13, 3650);
        calculate(2005, 10, 13, -1000);
        calculate(2005, 10, 13, -10000); }
      
      
    

Thursday 13th October 2005 plus 1 day is Friday 14th October 2005
Thursday 13th October 2005 minus 1 day is Wednesday 12th October 2005
Thursday 13th October 2005 plus 50 days is Friday 2nd December 2005
Thursday 13th October 2005 plus 100 days is Saturday 21st January 2006
Thursday 13th October 2005 plus 365 days is Friday 13th October 2006
Thursday 13th October 2005 plus 3650 days is Sunday 11th October 2015
Thursday 13th October 2005 minus 1000 days is Friday 17th January 2003
Thursday 13th October 2005 minus 10000 days is Sunday 28th May 1978