C++ 计算一个日期是星期几
(2012-09-22 21:00:34)
标签:
公式计算蔡勒判断 |
分类: C 编程 |
#include<iostream>
#include<string>
using namespace std;
string CaculateWeekDay(int y,int m,int d);
string get_Month_string(int m);
bool is_valid(int m,int d,int y);
void main()
{
int month,day,year;
cout<<"Enter a date in the form of dd
mm yyyy: ";
cin>>day>>month>>year;
if(is_valid(month,day,year))
cout<<CaculateWeekDay(year,month,day)<<endl;
else
cout<<day<<"
"<<month<<"
"<<year<<"
is an invalid date."<<endl;
}
string CaculateWeekDay(int y,int m, int d)
{
int week = 0;
if(m==1){m=13;y--;}
if(m==2) {m=14;y--;}
if((y<1752)||((y==1752)&&(m<9))||((y==1752)&&(m==9)&&(d<3)))
//判断是否在1752年9月3日之前
week =(d+2*m+3*(m+1)/5+y+y/4+5)%7; //1752年9月3日之前的公式
else
week =(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7; //1752年9月3日之后的公式
string weekstr="";
switch(week)
{
case 0: {weekstr="Monday"; break;}
case 1: {weekstr="Tuesday"; break;}
case 2: {weekstr="Wednesday"; break;}
case 3: {weekstr="Thursday"; break;}
case 4: {weekstr="Friday"; break;}
case 5: {weekstr="Saturday"; break;}
case 6: {weekstr="Sunday"; break;}
}
return weekstr;
}
bool is_valid(int m,int d,int y)
{
if(m<0||m>12||d<0||d>31||y<0)
return false;
return true;
}
string get_Month_string(int m)
{
switch(m){
case 1: {return "January";break;}
case 2: {return "February";break;}
case 3: {return "March";break;}
case 4: {return "April";break;}
case 5: {return "May";break;}
case 6: {return "June";break;}
case 7: {return "July";break;}
case 8: {return "August";break;}
case 9: {return "September";break;}
case 10: {return "October";break;}
case 11: {return "November";break;}
case 12: {return "Desember";break;}
}
}
来源:http://hi.baidu.com/shoppingsee/item/b2c384102a6f340fb88a1a1f