这是一个实现万年历的程序,我从书上copy到TC.exe上,再修改了多次依然未能完美的呈现万年历的效果。各位高手教教我吧~!注释部分纯属我个人的理解,还有我不理解之处。
#include<stdio.h>
int IsLeapYear(int);
main()
{
int i,day,year,temp,temp_i;
long int Year_days=0;
int Year_Start=1;
int Per_Year_Days;
int month_day[]={31,28,31,30,31,30,31,31,30,31,30,31,29};
printf("please input the year: ");
scanf("%d",&year);
while(Year_Start < year)/*while循环让Year_Start比所输入的年份多一年*/
{
if(IsLeapYear(Year_Start))
Per_Year_Days=366;
else
Per_Year_Days=365;
Year_days=Year_days+Per_Year_Days;
Year_Start++;
}
for(temp=1;temp<=12;temp++)/*循环输出一年12个月*/
{
switch(temp)
{
case 1:
printf("January(%d)\n",year);
break;
case 2:
printf("February(%d)\n",year);
break;
case 3:
printf("March(%d)\n",year);
break;
case 4:
printf("April(%d)\n",year);
break;
case 5:
printf("May(%d)\n",year);
break;
case 6:
printf("June(%d)\n",year);
break;
case 7:
printf("July(%d)\n",year);
break;
case 8:
printf("August(%d)\n",year);
break;
case 9:
printf("September(%d)\n",year);
break;
case 10:
printf("October(%d)\n",year);
break;
case 11:
printf("November(%d)\n",year);
break;
case 12:
printf("December(%d)\n",year);
break;
}
i=Year_days%7; /*这个i=Year_days%7在后面的i是否等于0用到,我真的很不明白这个i到底有什么用。365和366跟7求余都不会等于0 的啦*/
printf("Mon Tue Wed Thu Fri Sat Sun\n");
if(i!=0)
for(temp_i=0;temp_i<i;temp_i++)
printf(" ");
day =1;
if(IsLeapYear(year)&&temp==2)
while(day<= month_day[12])
{
if(day>1)
if(Year_days%7==0)
printf("\n");
if(day>=10)
printf("%d ",day);
else
printf("%d ",day);
Year_days++;
day++;
}
else
while(day<=month_day[temp-1])
{
if(day>1)
if(Year_days%7==0)
printf("\n");
if(day>=10)
printf("%d ",day);
else
printf("%d ",day);
Year_days++;
day++;
}
printf("\n");
if(getch()=='q')/*这个if语句里的条件我不明白是什么意思,程序由始至终都没出现'q'呀*/
exit(0);
}
getch();
}
int IsLeapYear(int year)/*判断闰年,因为闰年多一天要输出*/
{
if((year%4==0)&&(year%100!=0)||(year%400==0))
return 1;
else
return 0;
}
程序完毕!
注释里有我不明白的地方,知道的高手请多多指教!