根据日期计算对应这一天到底是星期几的一个算法, 如何利用C语言谢谢

Problem Description
Today is Saturday, 17th Nov,2007. Now, if i tell you a date, can you tell me what day it is ?

Input
There are multiply cases.
One line is one case.
There are three integers, year(0<year<10000), month(0<=month<13), day(0<=day<32).

Output
Output one line.
if the date is illegal, you should output "illegal". Or, you should output what day it is.

Sample Input
2007 11 17

Sample Output
Saturday

看下面的推荐目录里就有

基姆拉尔森计算公式(C++与VB.Net整数除法和取余运算符不同)
W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7 //C++计算公式
W = (D + 2 * M + 3 * (M + 1) \ 5 + Y + Y \ 4 - Y \ 100 + Y \ 400) Mod 7 'VB.Net计算公式
在公式中d表示日期中的日数,m表示月份数,y表示年数。
注意:在公式中有个与其他公式不同的地方:
把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。

程序:

1 #include
2

3 /**
4 功能:根据日期计算星期,使用基姆拉尔森公式
5 by: simple小强哥
6 github:LifeSimpleLwq
7 qq: 997950763 存在bug请练习本人更改,thanks
8 */
9

10

11 int main(void) {
12 int y = 1900,m = 12,d = 18;
13 int w;
14

15 if (m==1 || m==2)
16 {
17 m = (m==1?13:14);
18 y--;
19 }
20

21 w = (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%7;
22 printf("%d\r\n",w);
23 printf("hello,world\r\n");
24 return 0;
25 }

来源:
https://github.com/LifeSimpleLwq/calc_week

经抽象测试,计算结果正确。

作者:SimpleJY
来源:CSDN
原文:https://blog.csdn.net/qq_31247231/article/details/78830264
版权声明:本文为博主原创文章,转载请附上博文链接!

#include

const char * getWeekdayByYearday(int iY, int iM, int iD)
{
int iWeekDay = -1;
if (1 == iM || 2 == iM)
{

iM += 12;
iY--;
}

iWeekDay = (iD + 1 + 2 * iM + 3 * (iM + 1) / 5 + iY + iY / 4 - iY / 100 + iY / 400) % 7;
switch(iWeekDay)
{

case 0 : return "Sunday"; break;
case 1 : return "Monday"; break;
case 2 : return "Tuesday"; break;
case 3 : return "Wednesday"; break;
case 4 : return "Thursday"; break;
case 5 : return "Friday"; break;

case 6 : return "Saturday"; break;
default : return NULL; break;
}

return NULL;

}

int main()
{
int year,month,day;
char ch='1';
while(ch != '\033')
{
printf("\n请输入日期:\n格式为:1900,1,1\n");
scanf("%d,%d,%d",&year,&month,&day);
const char * p = getWeekdayByYearday(year, month, day);
printf("WeekDay : %s\n", p);
ch = getchar();
printf("\n");
}
}

使用struct tm atm_s; 结构体进行转换