输入年月日判断星期几

#define _CRT_SECURE_NO_WARNINGS
#include
#include

//通过百度,找到了基姆拉尔森计算公式
//该公式中把一月和二月看成是上一年的十三月和十四月

char *weekDay(int year, int month, int day)
{
	int my_weekday = -1;
	if (month == 1 || month == 2)
	{
		month += 12;
		year--;
	}
	my_weekday = (day + 1 + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;

	switch (my_weekday)
	{
	case 0: return "星期7"; break;
	case 1: return "星期1"; break;
	case 2: return "星期2"; break;
	case 3: return "星期3"; break;
	case 4: return "星期4"; break;
	case 5: return "星期5"; break;
	case 6: return "星期6"; break;
	default: return NULL;
	}
	return NULL;
}

int main(void)
{
	int year, month, day;
	printf("请输入年月日\n");
	scanf("%d%d%d", &year, &month, &day);
	char *p = weekDay(year, month, day);
	printf("%s\n", p);

	system("pause");
	return 0;
}

你可能感兴趣的:(经验)