求C语言程序 详解
发布网友
发布时间:2022-05-23 22:35
我来回答
共1个回答
热心网友
时间:2024-03-09 16:29
#include <stdio.h> //头文件
int main() //主函数
{
int day,mn,yr,i,days=0,s,k;//变量的定义
int mont[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //月份的天数,存入数组
char wek[][9]={ {'S','u','n','d','a','y'},
{'M','o','n','d','a','y'},
{'T','u','e','s','d','a','y'},
{'W','e','d','n','s','d','a','y'},
{'T','h','u','r','s','d','a','y'},
{'F','r','i','d','a','y'},
{'S','a','t','u','r','d','a','y'}
}; //星期的名称
printf("Inpute the date (year-month-day):"); //打印提示
scanf("%d-%d-%d",&yr,&mn,&day); //输入
if (yr%4==0||yr%100==0||yr%400==0) //判断是否闰年
mont[2]=29;
else
mont[2]=28;
for (i=0;i<mn;i++)
days+=mont[i]; //累加天数
days+=day;
s=yr-1+(int)((yr-1)/4)-(int)((yr-1)/100)+(int)((yr-1)/400)+days;
k=s%7; //得到星期几
printf("%d-%d-%d is %s.",yr,mn,day,wek[k]);
return 0;
}
补充:
mont[0]=0;mont[1]=31,mont[2]=28……依次下去~
易见,月份没有零月的,为了便于编程,故让mont[0]=0,占个位置,所以等于1也行,100也行,3289也行,反正用不到的。