#include <stdio.h>
#include <stdlib.h>
// 历史上的某一天是星期几?未来的某一天是星期几?
//关于这个问题,最著名的计算公式是蔡勒(Zeller)公式。即w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1
//
// output: w: 0 - Sunday;
// 1 - Monday;
// 2 - Tuesday;
// 3 - Wednesday;
// 4 - Thursday;
// 5 - Friday;
// 6 - Saturday;
//公式中的符号含义如下,w:星期;
// c:世纪-1;
// y:年(两位数);
// m:月(m大于等于3,小于等于14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月来计算,
// 比如2003年1月1日要看作2002年的13月1日来计算);
// d:日;[ ]代表取整,即只要整数部分。
//
//注:C是世纪数减一,y是年份后两位,M是月份,d是日数。1月和2月要按上一年的13月和14月来算,这时C和y均按上一年取值。)
//int current_year = 2000 + year_input; //以2000.1.1为例,设置如下:
//uint8_t year_input = 0;
//uint8_t month_input = 1;
//uint8_t day_input = 1;
typedef unsigned char uint8_t; //无符号8位数
typedef unsigned short uint16_t; //无符号16位数
//输入年月日,输出星期几
uint8_t ZellerFunction2Week(uint16_t year_local, uint8_t month_local, uint8_t day_local)
{
uint8_t week_output = 0;
uint8_t y, m, c;
//3<=month<=14 //重要!!!!
if (month_local >= 3)
{
m = month_local;
y = year_local % 100; //年(后两位数)
c = year_local / 100; //当前世纪数减一
}
else /* 某年的1、2月要看作上一年的13、14月来计算 */
{
m = month_local + 12;
y = (year_local - 1) % 100;
c = (year_local - 1) / 100;
}
int week = y + (y/4) + (c/4) - 2*c + ((26*(m+1))/10) + day_local - 1;
//w = y + y / 4 + c / 4 - 2 * c + ((uint16_t)26*(m+1))/10 + d - 1;
printf ("Before: week is %d\n\r", week);
if (week < 0)/* 如果week是负数,则计算余数方式不同 */
{
week = 7 - (-week) % 7;
}
else
{
week = week % 7;
}
printf ("After: week is %d\n\r", week);
week_output = (uint8_t)week;
return week_output;
}
//输入年月日,判断日期的有效性
bool CheckDate(uint16_t year1, uint8_t month1, uint8_t day1)
{
bool check_result = false;
printf("CheckDate: the input date is %d-%d-%d\n\r\n", year1, month1, day1);
if(month1>12)
{
check_result = false;
return check_result;
}
else if(day1>31)
{
check_result = false;
return check_result;
}
else if((month1==2||month1==4||month1==6||month1==9||month1==11)&&day1==31)
{
check_result = false;
return check_result;
}
else if(month1==2&&day1==30)
{
check_result = false;
return check_result;
}
else if(month1==2&&day1==29)
{
if( ((year1 % 4 == 0) && (year1 % 100 != 0)) || (year1 % 400 == 0) ) //闰年判断 :能被4整除且不能被100整除; 能被400整除
{
check_result = true;
}
else
{
check_result = false;
return check_result;
}
}
else
{
check_result = true;
}
printf("CheckDate: %c\n\r", check_result?'y':'n');
return check_result;
}
#define WEEK_TEST_MUNUAL 1
int main()
{
#if WEEK_TEST_MUNUAL
//uint8_t year_input = 0 ; //int current_year = 2000 + year_input; //以2000.1.1为例
//uint8_t month_input = 0;
//uint8_t day_input = 0;
unsigned int year_input = 0 ; //int current_year = 2000 + year_input; //以2000.1.1为例
unsigned int month_input = 0;
unsigned int day_input = 0;
#else
uint8_t year_input = 0 ; //int current_year = 2000 + year_input; //以2000.1.1为例
uint8_t month_input = 1;
uint8_t day_input = 1;
#endif
uint8_t week_get;
bool flag = 0;
#if WEEK_TEST_MUNUAL
do
{
printf ("please input year(1byte: 0 - 127):\n\r");
scanf("%u" , &year_input);
printf ("please input month(1byte: 1 - 12):\n\r");
scanf("%u" , &month_input);
printf ("please input day(1byte: 1 - 31):\n\r");
scanf("%u" , &day_input);
//判断输入年月日的有效性,如果有效 flag = 1;无效flag = 0;
flag = CheckDate(year_input + 2000, month_input, day_input);//注:以下函数中的代码可以计算所有年、月、日对应的星期几,但是此处年份限制在2000以后
if(flag == 0)
{
printf("you input a wrong date! Please inpuu again!\n\r");
printf("\n\r");
}
}while(flag == 0);
#endif
printf ("You have inputed date is %u-%u-%u\n\r", year_input, month_input, day_input);
week_get = ZellerFunction2Week(year_input + 2000, month_input, day_input);
printf ("Get: week_get is %d\n\r", week_get);
system("pause");
return 0;
}
执行结果: