Menu

Program day of the week

2002-12-23
2012-09-26
  • Nobody/Anonymous

    Hello, I am sending a little program, which calculates day of the week for a given date in the past or in the future. For example: 1.1.2003 program returns wednesday. Any comments/suggestions will be greatly appreciated.
    Franjo

    #include <iostream>
    #include <string>
    #include <sstream>
    #include <stdlib.h>
    string s;
    int day, month, year;
    void calculation(int day, int month, int year);
    void calculation1(int day, int month, int year);
    int monthc(int day, int month, int year);
    int monthc1(int day, int month, int year);
    void day_of_the_week1(int counter);
    void day_of_the_week(int counter);
    void call();
    using namespace std;
    int main()
    {
    while(!cin.eof())
    {
    call();
    }
    return 0;
    }

    void calculation(int day, int month, int year)
    {
    //The first of january 2003 if the reference date
    if(year >=2003)calculation1(day, month, year);
    else if(year < 2003)
    {
    int counter=0;
    for(int i=2002;i>year;i--)
    {
    if(i%4==0 && i%100!=0)counter+=366;
    else if(i%100==0 && i%400==0)counter+=366;
    else counter+=365;
    }//for(int i=2003;i>year;i--)
    counter+=monthc(day, month, year);
    counter%=7;
    day_of_the_week(counter);
    }//else if(year < 2003)
    }//void calculation(int day, int month, int year)

    int monthc(int day, int month, int year)
    {
    int stevec=1;
    if(month==12){stevec+=31-day;return stevec;}
    stevec+=31;
    if(month==11){stevec+=30-day;return stevec;}
    stevec+=30;
    if(month==10){stevec+=31-day;return stevec;}
    stevec+=31;
    if(month==9){stevec+=30-day;return stevec;}
    stevec+=30;
    if(month==8){stevec+=31-day;return stevec;}
    stevec+=31;
    if(month==7){stevec+=31-day;return stevec;}
    stevec+=31;
    if(month==6){stevec+=30-day;return stevec;}
    stevec+=30;
    if(month==5){stevec+=31-day;return stevec;}
    stevec+=31;
    if(month==4){stevec+=30-day;return stevec;}
    stevec+=30;
    if(month==3){stevec+=31-day;return stevec;}
    stevec+=31;
    if(month==2)
    {
    if((year%4==0 && year%100!=0) || (year%100==0 && year%400==0))
    {stevec+=29-day;return stevec;}
    stevec+=28-day;return stevec;
    }
    if((year%4==0 && year%100!=0) || (year%100==0 && year%400==0))stevec+=29;
    else stevec+=28;
    if(month==1){stevec+=31-day;return stevec;}

    }//int month(int day, int month, int year)

    void day_of_the_week(int counter)
    {
    if(counter==1)cout << "Tuesday";
    else if(counter==2)cout << "Monday";
    else if(counter==3)cout << "Sunday";
    else if(counter==4)cout << "Saturday";
    else if(counter==5)cout << "Friday";
    else if(counter==6)cout << "Thursday";
    else if(counter==0)cout << "Wednesday";
    }

    void calculation1(int day, int month, int year)
    {
    //1.1.2003 (Wednesday) is the reference date
    int counter=0;
    for(int i=2003;i<year;i++)
    {
    if(i%100==0 && i%400==0)counter+=366;
    else if(i%4==0 && i%100!=0)counter+=366;
    else counter+=365;
    }//for(int i=2003;i<year;i++)
    counter+=monthc1(day, month, year);
    counter%=7;
    day_of_the_week1(counter);
    }//void calculation1(int day, int month, int year)

    int monthc1(int day, int month, int year)
    {
    int stevec=0;
    if(month==1){stevec=day;return stevec;}
    stevec+=31;
    if(month==2){stevec+=day;return stevec;}
    if((year%100==0 && year%400==0) || (year%4==0) && (year%100!=0))stevec+=29;
    else stevec+=28;
    if(month==3){stevec+=day;return stevec;}
    stevec+=31;
    if(month==4){stevec+=day;return stevec;}
    stevec+=30;
    if(month==5){stevec+=day;return stevec;}
    stevec+=31;
    if(month==6){stevec+=day;return stevec;}
    stevec+=30;
    if(month==7){stevec+=day;return stevec;}
    stevec+=31;
    if(month==8){stevec+=day;return stevec;}
    stevec+=31;
    if(month==9){stevec+=day;return stevec;}
    stevec+=30;
    if(month==10){stevec+=day;return stevec;}
    stevec+=31;
    if(month==11){stevec+=day;return stevec;}
    stevec+=30;
    if(month==12){stevec+=day;return stevec;}
    }//int monthc1(int day, int month, int year)

    void day_of_the_week1(int counter)
    {
    if(counter==1)cout << "Wednesday";
    else if(counter==2)cout << "Thursday";
    else if(counter==3)cout << "Friday";
    else if(counter==4)cout << "Saturday";
    else if(counter==5)cout << "Sunday";
    else if(counter==6)cout << "Monday";
    else if(counter==0)cout << "Tuesday";
    }

    void call()
    {
    cout << "Enter a date like this: 1.1.2003\n"
    "It can be date in the history or in the future - whatever year\n";
    cin >> s;
    char m;
    istringstream iss(s);
    iss >> day >> m >> month >> m >> year;
    assert(month >=1 && month <=12);//Jan - Dec is 1-12
    if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
    assert(day>=1 && day<=31);
    if(month==4 || month==6 || month==9 || month==11)
    assert(day>=1 && day<=30);
    assert(year>=1);
    if(month==2)
    {
    if((year%100==0 && year%400==0) || (year%4==0 && year%100!=0))
    assert(day>=1 && day<=29);
    else assert(day>=1 && day<=28);
    }
    cout << s << " = " ;
    calculation(day, month, year);cout << endl;
    system("PAUSE");
    system("CLS");
    }

     
    • Nobody/Anonymous

      Hi Franjo...
      I'm new in this thing of programming and i know just a little about C but i can sugest you and algorithm based in Julian Day. It's simple and this is like appear in the Jean Meeus's Book "Astronomical Algorithm":

      "... Compute the Julian Day for that day at 0 hr, add 1.5, and divide the result by 7. The remainder of this division will indicate the weekday, as follows: 0 sunday, 1 monday, 2 tuesday, # Wednesday, 4 Thursday, 5 Friday, 6 aturday...

      You are using 2003 like reference but JD it's validate for any year from -4712 and independence you from a lot calculus problem and permit you using the algorithm for a more extensive range of years if you want. Julian Day is very easy to get...

      Condar

       
    • Nobody/Anonymous

      Comment: your code is very long, so it gets confusing and error-prone.

      Suggestion: use short coherent functions to better see what is going on.

      I won't write the whole thing for you, but I suggest:

      bool isLeapYear(int year)
      {
         return (year%100) ? !(year%4) : !(year%400);
      }

      int daysInYear(int year)
      {
         return isLeap(year) ? 366 : 365;
      }

      // Assume 1-based months with jan == 1 and dec == 12.
      int daysInMonth(int year, int month)
      {
         switch (month) {
         case 2:
            return isLeap(year) ? 29 : 28;
         case 4: case 6: case 9: case 11:
            return 30;
         }
         return 31;
      }

      Then starting from some base date like 1900-01-01 or whatever else you wish to pick, loop through the years, the months and finally the day of the month to count the number of days since that base date.  Adjust that number of days (by adding a constant from 1 to 6 if necessary) to match the day of the week on 1900-01-01 then do a modulo division by 7 to get the day of the week (0 to 6).

      Other methods will be faster, but this one is easy enough to follow...

      qWake

       
    • Nobody/Anonymous

      You probably noticed the discrepancy in my code, but the isLeapYear function should be called just isLeap since this is what the other functions use...  Volunteered code is always full of bugs and this one is no exception!

      qWake

       
    • Nobody/Anonymous

      Thanks a lot for your response qWake and Condar. I knew that my program is not optimized for speed, but it works. I'll study your solutions. Franjo

       
    • Nobody/Anonymous

      qWake, I've just studied your suggestions and I think that they are great. Here is my program with that corrections:

      #include <iostream>
      #include <string>
      #include <sstream>
      #include <stdlib.h>
      string s;
      int day, month, year;
      void calculation(int day, int month, int year);
      void calculation1(int day, int month, int year);
      int monthc(int day, int month, int year);
      int monthc1(int day, int month, int year);
      void day_of_the_week1(int counter);
      void day_of_the_week(int counter);
      void call();
      int daysinmonth(int i);
      bool is_leap_year(int year);
      int daysinyear(int year);
      using namespace std;
      int main()
      {
      while(!cin.eof())
      {
      call();
      }
      return 0;
      }

      void calculation(int day, int month, int year)
      {
      //The first of january 2003 if the reference date
      if(year >=2003)calculation1(day, month, year);
      else if(year < 2003)
      {
      int counter=0;
      for(int i=2002;i>year;i--)
      counter+=daysinyear(i);
      counter+=monthc(day, month, year);
      counter%=7;
      day_of_the_week(counter);
      }//else if(year < 2003)
      }//void calculation(int day, int month, int year)

      int monthc(int day, int month, int year)
      {
      int counter=1;int i;
      for(i=12;i>month;i--)
      counter+=daysinmonth(i);
      return counter+=daysinmonth(i)-day;
      }//int month(int day, int month, int year)

      void day_of_the_week(int counter)
      {
      if(counter==1)cout << "Tuesday";
      else if(counter==2)cout << "Monday";
      else if(counter==3)cout << "Sunday";
      else if(counter==4)cout << "Saturday";
      else if(counter==5)cout << "Friday";
      else if(counter==6)cout << "Thursday";
      else if(counter==0)cout << "Wednesday";
      }

      void calculation1(int day, int month, int year)
      {
      //1.1.2003 (Wednesday) is the reference date
      int counter=0;
      for(int i=2003;i<year;i++)
      counter+=daysinyear(i);
      counter+=monthc1(day, month, year);
      counter%=7;
      day_of_the_week1(counter);
      }//void calculation1(int day, int month, int year)

      int monthc1(int day, int month, int year)
      {
      int counter=0;
      for(int i=1;i<month;i++)
      counter+=daysinmonth(i);
      return counter+=day;
      }//int monthc1(int day, int month, int year)

      void day_of_the_week1(int counter)
      {
      if(counter==1)cout << "Wednesday";
      else if(counter==2)cout << "Thursday";
      else if(counter==3)cout << "Friday";
      else if(counter==4)cout << "Saturday";
      else if(counter==5)cout << "Sunday";
      else if(counter==6)cout << "Monday";
      else if(counter==0)cout << "Tuesday";
      }

      void call()
      {
      cout << "Enter a date like this: 1.1.2003\n"
      "It can be date in the history or in the future - whatever year\n";
      cin >> s;
      char m;
      istringstream iss(s);
      iss >> day >> m >> month >> m >> year;
      assert(month >=1 && month <=12);//Jan - Dec is 1-12
      assert(day>=1);
      switch(month)
      {
      case 2: is_leap_year(year)?assert(day<=29):assert(day<=28);
      case 4: case 6: case 9: case 11: assert(day <=30);
      case 1: case 3: case 5: case 7: case 8: case 10: case 12: assert(day<=31);
      }//switch(month)
      cout << s << " = " ;
      calculation(day, month, year);cout << endl;
      system("PAUSE");
      system("CLS");
      }

      bool is_leap_year(int year)
      {
      return (year%100)?!(year%4):!(year%400);
      }

      int daysinyear(int year)
      {
      return (is_leap_year(year))? 366:365;
      }

      int daysinmonth(int i)
      {
      switch(i)
      {
      case 2: return is_leap_year(year)?29:28;
      case 4: case 6: case 9: case 11:
      return 30;
      }
      return 31;
      }

      If you have any more suggestions/comments - they'll be greatly appreciated. Franjo

      P.S.
      About Condor's comment - I'll study that too.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.