有兴趣的来看看!
本程序的功能是查询所输入的年月日是星期几:程序清单:
#include "stdio.h"
void main()
{static int year,month,day,n,m,i,close=0,a[]={0,31,59,90,120,151,181,212,243,273,304,334};
printf("请输入年月日(如:2000.1.1以回车结束输入,以32767退出系统.):\n");
do{
loop: scanf("%d.%d.%d",&year,&month,&day);
if(year==32767) {close=1;break;}
else if(year>32767||year<1||month>12||month<1||day>31||day<1||month==2&&day>29||(month==4||month==6||month==9||month==11)&&day>30||!(year%400==0||year%100!=0&&year%4==0)&&month==2&&day==29)
{printf("对不起,您的输入错误,请重新输入!\n"); goto loop;}
m=a[month-1];
for(i=1;i<year;i++)
{if(i%400==0||i%100!=0&&i%4==0) n++; /*计算year年前闰年的个数*/
else continue;}
if(year%400==0||year%100!=0&&year%4==0)
if(month<=2) n=(year-1+n+m+day)%7;
else n=(year+n+m+day)%7;
else n=(year-1+n+m+day)%7; /*计算模7的余数*/
switch(n) /*输出结果*/
{case 0:printf("%d年%d月%d日是星期天.\n",year,month,day);break;
case 1:printf("%d年%d月%d日是星期一.\n",year,month,day);break;
case 2:printf("%d年%d月%d日是星期二.\n",year,month,day);break;
case 3:printf("%d年%d月%d日是星期三.\n",year,month,day);break;
case 4:printf("%d年%d月%d日是星期四.\n",year,month,day);break;
case 5:printf("%d年%d月%d日是星期五.\n",year,month,day);break;
default:printf("%d年%d月%d日是星期六.\n",year,month,day);}
year=0;month=0;day=0;n=0;m=0;i=0;
}while(close!=1);
}