万年历c程序
#include
精彩内容,尽在百度攻略:https://gl.baidu.com
code unsigned char WeekTab[] = {//闰年月星期表
(3 << 5) + 31,//1月
(6 << 5) + 29,//2月精彩内容,尽在百度攻略:https://gl.baidu.com
(0 << 5) + 31,//3月
(3 << 5) + 30,//4月
(5 << 5) + 31,//5月精彩内容,尽在百度攻略:https://gl.baidu.com
(1 << 5) + 30,//6月
(3 << 5) + 31,//7月
(6 << 5) + 31,//8月精彩内容,尽在百度攻略:https://gl.baidu.com
(1 << 5) + 30,//9月
(4 << 5) + 31,//10月
(0 << 5) + 30,//11月精彩内容,尽在百度攻略:https://gl.baidu.com
(2 << 5) + 31 //12月
};
精彩内容,尽在百度攻略:https://gl.baidu.com
/*------------------------------------------------------------------------------
----------
2000年~2099年星期算法精彩内容,尽在百度攻略:https://gl.baidu.com
--------------------------------------------------------------------------------
---------*/
unsigned char WeekDay20(unsigned char y, unsigned char m, unsigned char d) {精彩内容,尽在百度攻略:https://gl.baidu.com
unsigned char week, day;
day = WeekTab[m - 1];//月表
week = day >> 5;//月星期数精彩内容,尽在百度攻略:https://gl.baidu.com
day &= 0x1f;//月天数
if ((m < 3) && (y & 0x03)){//平年
if (m == 2) day--;//平年月天数精彩内容,尽在百度攻略:https://gl.baidu.com
week++;//平年月表+1
}
y = y + (y >> 2);//年+年/4精彩内容,尽在百度攻略:https://gl.baidu.com
week = (week + y + d + 2) % 7;//(星期=年+年/4+月表+2日)%7
return (week << 5) | day;//返回星期和月天数
}精彩内容,尽在百度攻略:https://gl.baidu.com
/*------------------------------------------------------------------------------
----------精彩内容,尽在百度攻略:https://gl.baidu.com
0000年~9999年星期算法
--------------------------------------------------------------------------------
---------*/精彩内容,尽在百度攻略:https://gl.baidu.com
unsigned char WeekDay(unsigned char c, unsigned char y, unsigned char m,
unsigned char d)
{精彩内容,尽在百度攻略:https://gl.baidu.com
unsigned char week, day;
c &= 0x03;//百年%4
c = c | (c << 2);//百年%4*5精彩内容,尽在百度攻略:https://gl.baidu.com
day = WeekTab[m - 1];//月表
week = day >> 5;//月星期数
day &= 0x1f;//月天数精彩内容,尽在百度攻略:https://gl.baidu.com
if ((m < 3) && !((c == 0) | (y & ((y & 0x03) == 0)))){//平年
if (m == 2) day--;//平年月天数
week++;//平年月表+1精彩内容,尽在百度攻略:https://gl.baidu.com
}
y = y + (y >> 2);//年+年/4
week = (week + c + y + d + 2) % 7;//(星期=百年%4*5+年+年/4+月表+日+2)%7 return (week << 5) | day;//返回星期和月天数精彩内容,尽在百度攻略:https://gl.baidu.com
}
unsigned char BcdToBin(unsigned char val)精彩内容,尽在百度攻略:https://gl.baidu.com
{
val = (val>> 4) * 10 + (val& 0x0f);//将BCD码转换为10进制数
return val;//返回10进制数精彩内容,尽在百度攻略:https://gl.baidu.com
}
void main(void)精彩内容,尽在百度攻略:https://gl.baidu.com
{
unsigned char c, y, m, d;
unsigned char cx, yx, mx, dx;精彩内容,尽在百度攻略:https://gl.baidu.com
unsigned char WDay, Week, Day;
/*---------------------------------------------------------
0001年1月1日星期天精彩内容,尽在百度攻略:https://gl.baidu.com
---------------------------------------------------------*/
c = 0x00;
y = 0x01;精彩内容,尽在百度攻略:https://gl.baidu.com
m = 0x01;
d = 0x01;
cx = BcdToBin(c);//百年精彩内容,尽在百度攻略:https://gl.baidu.com
yx = BcdToBin(y);//年
mx = BcdToBin(m);//月
dx = BcdToBin(d);//日精彩内容,尽在百度攻略:https://gl.baidu.com
WDay = WeekDay(cx, yx, mx, dx);//取星期和月天数
Week = WDay>> 5;//得到星期
Day = WDay& 0x1f;//得到最大月天数精彩内容,尽在百度攻略:https://gl.baidu.com
/*---------------------------------------------------------
1918年1月21日星期一
---------------------------------------------------------*/ c = 0x19;精彩内容,尽在百度攻略:https://gl.baidu.com
y = 0x18;
m = 0x01;
d = 0x21;精彩内容,尽在百度攻略:https://gl.baidu.com
cx = BcdToBin(c);//百年
yx = BcdToBin(y);//年
mx = BcdToBin(m);//月精彩内容,尽在百度攻略:https://gl.baidu.com
dx = BcdToBin(d);//日
WDay = WeekDay(cx, yx, mx, dx);//取星期和月天数 Week = WDay>> 5;//得到星期
Day = WDay& 0x1f;//得到最大月天数 /*--------------------------------------------------------- 9999年12月31日星期天精彩内容,尽在百度攻略:https://gl.baidu.com
---------------------------------------------------------*/ c = 0x00;
y = 0x01;
m = 0x01;精彩内容,尽在百度攻略:https://gl.baidu.com
d = 0x01;
cx = BcdToBin(c);//百年
yx = BcdToBin(y);//年精彩内容,尽在百度攻略:https://gl.baidu.com
mx = BcdToBin(m);//月
dx = BcdToBin(d);//日
WDay = WeekDay(cx, yx, mx, dx);//取星期和月天数 Week = WDay>> 5;//得到星期精彩内容,尽在百度攻略:https://gl.baidu.com
Day = WDay& 0x1f;//得到最大月天数 /*--------------------------------------------------------- 2004年6月18日星期五
---------------------------------------------------------*/ c = 0x20;
y = 0x04;精彩内容,尽在百度攻略:https://gl.baidu.com
m = 0x06;
d = 0x18;
cx = BcdToBin(c);//百年精彩内容,尽在百度攻略:https://gl.baidu.com
yx = BcdToBin(y);//年
mx = BcdToBin(m);//月
dx = BcdToBin(d);//日精彩内容,尽在百度攻略:https://gl.baidu.com
WDay = WeekDay20(yx, mx, dx);//取星期和月天数 Week = WDay>> 5;//得到星期
Day = WDay& 0x1f;//得到最大月天数 /*--------------------------------------------------------- 2018年3月8日星期四
---------------------------------------------------------*/ c = 0x20;精彩内容,尽在百度攻略:https://gl.baidu.com
y = 0x18;
m = 0x03;
d = 0x08;精彩内容,尽在百度攻略:https://gl.baidu.com
cx = BcdToBin(c);//百年
yx = BcdToBin(y);//年
mx = BcdToBin(m);//月精彩内容,尽在百度攻略:https://gl.baidu.com
dx = BcdToBin(d);//日
WDay = WeekDay20(yx, mx, dx);//取星期和月天数 Week = WDay>> 5;//得到星期
Day = WDay& 0x1f;//得到最大月天数 while(1);精彩内容,尽在百度攻略:https://gl.baidu.com