#include<stdio.h>
int main()
{
int year,month,day,t,y,m,r,s,p,a,b,c,d,e,f;
printf("这是一个计算星期的程序,输入一个合法的日期,计算机将计算出这一天是星期几\n");
p=1;
while(p<=5)
{
printf("请输入一个日期(年份应在1901~2100之间):");
scanf("%d,%d,%d",&year,&month,&day);
/*判断输入的日期是否超出范围*/
do
{
/*判断平闰年*/
if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
t=1;
else
t=0;
}
else
t=1;
}
else
t=0;
/*判断输入的日期是否合法*/
a=0;b=0;c=0;d=0;e=0;f=0;
if(year<1901||year>2100)a=1;
if(month<1||month>12)b=1;
if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&(day<1||day>31))c=1;
if((month==4||month==6||month==9||month==11)&&(day<1||day>30))d=1;
if((month==2)&&(t==0&&(day<1||day>28)))e=1;
if((month==2)&&(t==1&&(day<1||day>29)))f=1;
if(a==1||b==1||c==1||d==1||e==1||f==1)
{
printf("输入的日期错误,请重新输入一个完整合法的日期:");
scanf("%d,%d,%d",&year,&month,&day);
}
}
while(a==1||b==1||c==1||d==1||e==1||f==1);
/*判断完毕,开始计算*/
y=(year-1)%400;
m=(y/4)*5+(y%4)-(y/100);
switch(month)
{
case 1: r=0;break;
case 2: r=31;break;
case 3: r=31+28+t;break;
case 4: r=31+28+t+31;break;
case 5: r=31+28+t+31+30;break;
case 6: r=31+28+t+31+30+31;break;
case 7: r=31+28+t+31+30+31+30;break;
case 8: r=31+28+t+31+30+31+30+31;break;
case 9: r=31+28+t+31+30+31+30+31+31;break;
case 10: r=31+28+t+31+30+31+30+31+31+30;break;
case 11: r=31+28+t+31+30+31+30+31+31+30+31;break;
default : r=31+28+t+31+30+31+30+31+31+30+31+30;break;
}
/*最后计算并输出*/
m=m+r+day;
s=m%7;
switch(s)
{
case 1: printf("星期一\n");break;
case 2: printf("星期二\n");break;
case 3: printf("星期三\n");break;
case 4: printf("星期四\n");break;
case 5: printf("星期五\n");break;
case 6: printf("星期六\n");break;
default : printf("星期日\n");break;
}
p++;
}
getch();
}