C语言,输入年月日,输出星期几

检测的时候大部分年份都对,单提交的时候有一个数据输出结果是错的,比如1900年1月1日应该星期一,而我的是星期六,找不到原因,求大神帮忙看一下~急~在线等!
以下是我的代码
#include <stdio.h>

int main ()
{
int y,m,d,c,w ;
scanf("%d%d%d",&y,&m,&d);
c=y/100-1;
if (m<=2)
{
y=y-1;m=m+12;
}
y=y%100;
w=y+y/4+c/4-2*c+(26*(m+1)/10)+d-1 ;
while(w<0)
w+=7;
if(c<=18)
w=w%7-1;
else
w = w%7;
switch(w)
{
case 1: printf("0\n"); break;
case 2: printf("1\n"); break;
case 3: printf("2\n"); break;
case 4: printf("3\n"); break;
case 5: printf("4\n"); break;
case 6: printf("5\n"); break;
case 0: printf("6\n"); break;
}
}

#include<stdio.h>
#define SIZE 31
void inputCheck(int,int,int);
int leaYear(int);

main()
{
int year,month,day,d1,result,sum=0;
scanf("%d%d%d",&year,&month,&day);
inputCheck(year,month,day);

for(;year>1; year--)
{
if((year%4==0 && year%100!=0)||(year%400==0))
sum+=366;
else
sum+=365;
}
d1=leaYear(month);
if((year%4==0 && year%100!=0) || (year%400==0))
{
result=(sum+d1+day)%7;}
else
{ if(month>2)
result=(sum+d1+day-1)%7;
else result=(sum+d1+day)%7;
}

switch(result)
{
case 1:printf("Monday\n");break;
case 2:printf("Tuesday\n");break;
case 3:printf("Wedensday\n");break;
case 4:printf("Thurfay\n");break;
case 5:printf("Friday\n");break;
case 6:printf("Saturday\n");break;
case 7:printf("Sunday\n");break;
}
return 0;
}

int leaYear(int m)
{
int t=0;

for(; m>1;m--)
{
switch(m)
{
case 2:
t+=29;
break;
case 4:
case 6:
case 9:
case 11:

t+=30;
break;

case 3:
case 5:
case 7:
case 8:
case 10:
case 12:

t+=31;break;
}
}

return t;

}

void inputCheck(int year,int month,int day)
{
if(year<0)

printf("input is wrong!\n");
else
{
switch(month)
{
case 2:
if(day>28 && (!((year%4==0 && year%100!=0) || (year%400==0))))
printf("input is wrong!\n");
if(day>29 && ((year%4==0 && year%100!=0) || (year%400==0)))
printf("input is wrong!\n");break;
case 4:
case 6:
case 9:
case 11:
if(day>30)
printf("input is wrong!\n");break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(day>31)
printf("input is wrong!\n");
}
}
}

感觉应该加上判断输入数据是否合格的函数,即便没这个要求,我用测试数据1900 1 1试了试,我的结果是星期一,你的错误你来找吧追问

你的程序我试了一下,闰年的结果都错了。。。。不过还是谢谢你!

追答

#include
int total_days(int,int,int);
main()
{
int year,month,day,days;
while(scanf("%d%d%d",&year,&month,&day)!=EOF)
{
if(year>=1 && month1 && d!=29 && (y%100==0 || (y%4==0 && y%100!=0)))
sum+=1;
return sum+d;
}
又写了个,你看看行不行

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-03-16
查过资料,以上公式只适合于1582年10月15日之后的情形(当时的罗马教皇将恺撒大帝制订的儒略历修改成格里历,即今天使用的公历)。追问

那1900年为什么会错呢?(⊙v⊙)。。

追答

好像是这样的

需要把一月和二月看成是上一年的十三月和十四月

例如,1990 1 1换算成1989 13 1,就可以得到正确的结果。

相似回答