c语言电子万年历函数 c语言万年历设计思路

C语言编写万年历~~速度/*C语言编程万年历
要求输入年月c语言电子万年历函数,判断是否闰年;
输入年月日c语言电子万年历函数,判断星期几;
输入年份,打出12个月c语言电子万年历函数的月历;
输入年份,月份,打印出本月日历;
要求用多个函数实现 。*/
#includestdio.h
#includetime.h
#includestring.hint calendar[12][6][7];/*月历*/
char* week[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Satarday"};
char* Monthname[]={"January","February","March","April","May","June","July",
"August","September","October","November","December"};
int monthday[]={31,28,31,30,31,30,31,31,30,31,30,31};
char* menu[]={/*操作菜单*/
"1.Input a year number,check whether it's a leap year.\n",
"2.Input year,month,day,check the weekday.\n",
"3.Input year,month,output the calendar of that month.\n",
"4.Input year,output all the month calendar.\n",
"0.Exit.\n"
};/*判断参数year传递c语言电子万年历函数的年份是否是闰年*/
int IsLeapyear(int year)
{
if(!(year%4)year%100||!(year%400)) return 1;
else return 0;
}/*输入年月日,判断星期几,利用Zeller公式w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1
w是结果星期数,y是年份的后两位,c是年份的前两位,m是月份,3≤m≤14,也就是当
m≤2时,要算到前一年的13月份和14月份,最后要将w对7取模
*/
int WeekDay(int year,int month,int day)
{
int w,y,c,m,d;
c=year/100;
if(month3){
m=12+month;
y=year%100-1;
}
else{
m=month;
y=year%100;
}
d=day;
w=y+y/4+c/4-2*c+26*(m+1)/10+d-1;
return (w%7+7)%7;
}/*输入年份,月份,打印出本月的日历*/
void Monthly(int year,int month)
{
int weekday,i,j;
if(month==2)
if(IsLeapyear(year)) monthday[1]+=1;
weekday=WeekDay(year,month,1);
printf("%s\n",Monthname[month-1]);
printf("Sun. Mon. Tue. Wed. Thu. Fri. Sat.\n");
for(i=1,j=weekday;i=monthday[month-1];i++,j++){
calendar[month-1][j/7][j%7]=i;
}
for(i=0;i6;i++){
for(j=0;j7;j++)
{
if(calendar[month-1][i][j]==0) printf("%5c",' ');
else printf("%-5d",calendar[month-1][i][j]);
}
printf("\n");
}
}void allMonth(int year)/*输入年份,打印出12个月的月历*/
{
int i;
for(i=1;i=12;i++){
Monthly(year,i);
getch();/*按任意键继续执行*/
}
}void main(void)
{
int year,month,day,i,n,weekday;
memset(calendar,sizeof(calendar),0);/*初始化月历*/
for(i=0;i5;i++)
printf("%s",menu[i]);
while(1){
printf("Please choose the menu:");
scanf("%d",n);
printf("\n");
switch(n){
case 1:
printf("Please input year:");
scanf("%d",year);
if(IsLeapyear(year)) printf("\n%d is leap year.\n",year);
else printf("%d isn't leap year.\n",year);
break;
case 2:
printf("Please input year month day:");
scanf("%d%d%d",year,month,day);
printf("\n");
weekday=WeekDay(year,month,day);
printf("That day is %s\n",week[weekday]);
break;
case 3:
printf("Please input year month,then it output a calendar of that month:");
scanf("%d%d",year,month);
printf("\n");
Monthly(year,month);
break;
case 4:
printf("Please input year,then it will output the calendar of that year:");
scanf("%d",year);
printf("\n");
allMonth(year);
break;
case 0:
return;
default:
printf("The number you input is invalid.\n");
break;
}
}
getch();/*按任意键,程序退出*/
}这个程序是可用的~~希望能帮到c语言电子万年历函数你~~~

推荐阅读