/*****根据当前的年、月、日 算出当前是周几.
基姆拉尔森计算公式: W = (D + M*2 + 3*(M+1)/5 + Y + Y/4 -Y/100 + Y/400)%7.
公式有一个特殊之处: 当 M = 1或2时,要把当前的年份转化为上一年的13或14作为数值
代入公式计算;
- #include<iostream>
- using namespace std;
- char *weekday[7] = {
- "Monday",
- "Tuesday",
- "Wednesday",
- "Thursday",
- "Friday",
- "Satureday",
- "Sunday"
- };
- int main()
- {
- int Y, M, D;/*Y is year, M is month, D is day */
- int wpos;
- char Tmp;
- bool IsSpecial = 0;
- while(cout<<"Input The Date(Among the date is devided by '-'):", cin>>Y>>Tmp>>M>>Tmp>>D){
- if(M==1 || M==2){
- --Y; /* 把当前年份当做上一年计算 */
- M+=12;/* 把当前月份作为上一年的13月、14月 计算 */
- IsSpecial = 1;
- }
- wpos = (D+2*M+3*(M+1)/5+Y+Y/4-Y/100+Y/400)%7;
- if(IsSpecial){
- ++Y;
- M-=12;
- }
- char p[50];
- sprintf(p, "The Current Date is : %d.%d.%d.%s\n", Y,M,D,weekday[wpos]);
- cout<<p;
- }
- system("pause");
- return 0;
- }
转载于:https://blog.51cto.com/hustluy/427880