Sep 26, 2014

How to calculate any day of the week

7:56 PM Posted by Unknown , No comments


//Auther   : Walter
//Date     : September 27, 2014 Firday
//Email    : Oursharingclub@163.com
//Tool     : Dev C++
//Language : C++
//Function : Calculate any day of the week

#include <iostream>
#include <cmath>      // for floor()
#include <conio.h>    // for getch()
#include <stdlib.h>   // for exit()
#include <windows.h>  // for system()


using std::cout;
using std::cin;

//=======================
void Menu( void );

int main( void )
{
Menu();

return 0;
}//  End of main function

//=======================
void Menu( void )
{
int year, month, day;
int week;
int century;
int temp;
bool flag= false;

system( "cls" );

cout<< "\n\n       =========== Calculate any day of the week =========== ";
cout<< "\n\n\t\t Enter year month day: ";
cin>> year>> month>> day;

temp= year;

if( year<= 1582 && month<= 10 && day<= 15 )
{
   flag= true;
}

if( month== 2 || month== 1 )
{
   year--;
month= 12+ month;
}

century= year/ 100;
year= year% 100;   // The last two digits of the year

if( flag== false )
{
  week= year+ ( int )floor( ( double )year/ 4 )+ ( int )floor( ( double )century/ 4 )- 2* century+
     ( int )floor( (double ) 26*( month+ 1 )/ 10 )+ day- 1;
}
else
{
  week= year+ ( int )floor( ( double )year/ 4 )+ ( int )floor( ( double )century/ 4 )- 2* century+
     ( int )floor( ( double ) 26*( month+ 1 )/ 10 )+ day- 3;
}

week= ( week% 7+ 7 )% 7;

year= temp;

switch( week )
{
  case 0:
{
   cout<< "\n\n\t\t ";
cout<< year<< "-"<< month<< "-"<< day<< ": "<< "Sunday";

break;
}
  case 1:
{
   cout<< "\n\n\t\t ";
cout<< year<< "-"<< month<< "-"<< day<< ": "<< "Monday";

break;
}
  case 2:
{
   cout<< "\n\n\t\t ";
cout<< year<< "-"<< month<< "-"<< day<< ": "<< "Tuesday";

break;
}
  case 3:
{
   cout<< "\n\n\t\t ";
cout<< year<< "-"<< month<< "-"<< day<< ": "<< "Wednesday";

break;
}
  case 4:
{
   cout<< "\n\n\t\t ";
cout<< year<< "-"<< month<< "-"<< day<< ": "<< "Thursday";

break;
}
  case 5:
{
   cout<< "\n\n\t\t ";
cout<< year<< "-"<< month<< "-"<< day<< ": "<< "Friday";

break;
}
  case 6:
{
   cout<< "\n\n\t\t ";
cout<< year<< "-"<< month<< "-"<< day<< ": "<< "Saturday";

break;
}
}

cout<< "\n\n\t\t Do you want to continue( Y/N ): ";

char letter;

cin>>letter;

if( toupper( letter)== 'Y' )
{
  Menu();
}
else
{
  cout<< "\n\n\t\t Thank U.";
  getch();

  exit( 1 );
}
}// End of Menu function




Screenshot:





Zeller’s congruence:





0 Comment:

Post a Comment