原本是第七章后面的习题18,只要求求某天具体是该年第几天的,自己扩展了下,算出某年的某月某日具体是星期几,下面我写的源代码,仅供参考吧。。。
ps:明天开始啃C中最难的指针了,加油。。。image
源代码如下:
include<stdio.h> //某年某月某日具体是星期几的源代码
int main()
{
int leap_year(int year);//声明求是否闰年函数
int total_days(int day,int month);//声明求该日为该年的第几天函数
int year,month,day,days,s,d,leap;
char week[7][10]={ {'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("please input the year,month and day,sperate with space\n");
scanf("%d%d%d",&year,&month,&day);
leap=leap_year(year);
days=total_days(day,month);
if(leap==1&&month>=3)
days+=1;//当为闰年或者是3月及其以后的月份,则将总天数加1
s=(year-1)+(int)((year-1)/4)+(int)((year-1)/100)+(int)((year-1)/400)+days;
d=s%7;//d为星期几,若为0,则为星期日
printf("%d-%d-%d is the %dth days,and its %s\n",year,month,day,days,week[d]);
return 0;
}
int leap_year(int year)//定义求是否闰年函数
{
int leap;
return leap=(year%4==0&&year%100!=0||year%400==0);
}
int total_days(int day,int month)//定义求该日为该年的第几天函数
{
int month_lab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i;
for(i=1;i<month;i++)
day+=month_lab[i];
return day;
}
程序运行后截图