原创 不需查表的万年星期算法C语言程序源码(菜农自创)

2007-5-29 13:07 4464 7 10 分类: 软件与OS

本程序实际上是我的博客的<LPCARM之RTC应用举例及日期校验和星期自动调节祥解>


一文中的程序摘录. 见: http://www.google.cn/search?complete=1&hl=zh-CN&q=LPCARM%E4%B9%8BRTC%E5%BA%94%E7%94%A8%E4%B8%BE%E4%BE%8B%E5%8F%8A%E6%97%A5%E6%9C%9F%E6%A0%A1%E9%AA%8C%E5%92%8C%E6%98%9F%E6%9C%9F%E8%87%AA%E5%8A%A8%E8%B0%83%E8%8A%82%E7%A5%A5%E8%A7%A3&btnG=Google+%E6%90%9C%E7%B4%A2&meta=


本贴主要是回复网友江小鸥的高论,我非常感谢!!!


见: http://www.ednchina.com/blog/hotpower/11893/message.aspx#19407


回复网友江小鸥:


非常感谢您的参与!!!不过心算和计算机笨算很不同~~~


不需查表算法程序摘录如下:
/*----------------------------------------------------------------------------------------
  0000年~9999年星期算法(菜农自创)
-----------------------------------------------------------------------------------------*/
unsigned int GetDow(unsigned int y, unsigned int m, unsigned int d)
{
unsigned int w, c;
  if (m <= 2){
    m |= 4;//1月2月同5月六月表
    y--;
  }
  c = y / 100;
  c &= 0x03;//百年%4
  y %= 100;
  w = ((c | (c << 2)) + (y + (y >> 2)) + (13 * m + 8)/ 5 + d) % 7;//(星期=百年%4*5+年+年/4+(13*月+8)/5+日)%7
  return w;//返回星期
}


/*----------------------------------------------------------------------------------------
  附赠0000年~9999年月最大天数算法(菜农自创)
-----------------------------------------------------------------------------------------*/
int GetDom(unsigned int y, unsigned int m)
{
int dn;
  dn = GetDow(y, m + 1, 1) - GetDow(y, m, 1);//m+1=13表示明年的1月
  if (dn < 0) dn += 7;
  return dn + 28;//返回当月的最大天数
}

文章评论3条评论)

登录后参与讨论

用户99599 2007-6-7 18:55

这里介绍一种不用查表的公式:Zeller's Rule

The following formula is named Zeller's Rule after a Reverend Zeller. [x] means the greatest integer that is smaller than or equal to x. You can find this number by just dropping everything after the decimal point. For example, [3.79] is 3. Here's the formula:

    f = k + [(13*m-1)/5] + D + [D/4] + [C/4] - 2*C.

    (k + [(13*m-1)/5] + D + [D/4] + [C/4] - 2*C)mod 7

    k日、m月、D年、C百年。谁会推导这个公式?(不难的)

  • k is the day of the month. Let's use January 29, 2064 as an example. For this date, k = 29.
  • m is the month number. Months have to be counted specially for Zeller's Rule: March is 1, April is 2, and so on to February, which is 12. (This makes the formula simpler, because on leap years February 29 is counted as the last day of the year.) Because of this rule, January and February are always counted as the 11th and 12th months of the previous year. In our example, m = 11.
  • D is the last two digits of the year. Because of the month numbering, D = 63 in our example, even though we are using a date from 2064.
  • C stands for century: it's the first two digits of the year. In our case, C = 20.

Now let's substitute our example numbers into the formula.

    f = k + [(13*m-1)/5] + D + [D/4] + [C/4] - 2*C
    = 29 + [(13*11-1)/5] + 63 + [63/4] + [20/4] - 2*20
    = 29 + [28.4] + 63 + [15.75] + [5] - 40
    = 29 + 28 + 63 + 15 + 5 - 40
    = 100.

Once we have found f, we divide it by 7 and take the remainder (if the remainder is negative, add 7). A remainder of 0 corresponds to Sunday, 1 means Monday, etc. For our example, 100 / 7 = 14, remainder 2, so January 29, 2064 will be a Tuesday.

雁塔菜农 2007-5-29 21:16

哈哈~~~那是阿莫站长送个我的自留地~~~

http://www.ouravr.com/bbs/bbs_list.jsp?bbs_id=1033&bbs_page_no=1

 

实际上这个算法的优点是不查表.

我反常规地"排表"为3月起头,即

3,4,5,6,7,8,9,10,11,12,1,2

而且1,2刚好与5,6表相同,这样就彻底地抛开了表的束缚~~~

用机器语言表述如下:

  if (m <= 2){
    m |= 4;//1月2月同5月六月表
    y--;
  }

这句是"精华"~~~

用户1079511 2007-5-29 14:51

好东西,收下了。敢问一声“菜农”是OURAVR上的吗?

相关推荐阅读
雁塔菜农 2012-04-05 17:51
2012年度新唐Cortex-M0助学开发套件有约束条件赠送申报贴
2012年度新唐Cortex-M0助学套件从4月起每月有约束条件发放16套。 申报人必须是21ic或 EDNC 会员 并具备1月的会龄。 申报时必须注明“遵守约束条件,缴纳250元订金,上...
雁塔菜农 2012-04-05 17:04
2012年度Cortex-M0助学园地推广框图
...
雁塔菜农 2012-04-01 17:59
2012年度Cortex-M0助学园地奖励计划细则
抢楼请点击:菜地公告:即日起创建《菜农Cortex-M0助学园地》(盖楼入口) 每月10号和25号的下午2:50进行2次抢楼,规则同去年12月的疯狂抢楼活动。 奖品由21IC、北航、广州迪圣...
雁塔菜农 2012-02-03 08:19
菜农谋略:搞定牛人宋俊德,对女牛人孙昌旭说:“记住,俺是雁塔菜农~~~ ”
http://www.baidu.com/s?wd=%B2%CB%C5%A9%D0%A6%CC%B8%A1%B6%D0%C2%C0%CB%C3%FB%C8%CB%D0%A7%D3%A6%A1%B7...
我要评论
3
7
关闭 站长推荐上一条 /2 下一条