昨天在百度上逛时碰到要给这个代码加注释的,呵呵, 加了一点注释。这是个万年历程序,可输入具体日期计算这天是星期几,输出全年的日历,还可以判断是否是闰年。这个程序只是代码比较长,但难度不大, 应该能看读懂。
#include
#include
char* month_str[]={"january","february","march","april","may","june","july","august","september","october","november","december"};
char* week[]={"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};
int isleapyear(int year) /*find out the year is leap year or not*/
{
if((year%4==0&&year%100!=0)||(year%400==0)) //这里是判断是否是闰年的
return 1; //如果是闰年就返回值1
else
return 0;//不是的话返回0
}
int month_day(int year,int month) //这个函数用来判断这年的月分有多少天的
{
int mon_day[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(isleapyear(year)&&month==2) /*判断是判断是否是闰年,如果是闰年而且这个月是2月那这个月有29天*/
return 29;
else
return(mon_day[month-1]);
}
int daysearch(int year,int month,int day) /*这个函数是计算输入的日期对应的星期*/
{
int c=0;
float s;
int m;
for(m=1;m
c=c+month_day(year,m); //这是计算输入的月分的累计天数
c=c+day; //计算日期在这一年中是第几天
s=year-1+(int)(year-1)/4+(int )(year-1)/100+(int)(year-1)/400-40+c; /*这是计算日期对应的星期公式,这个公式可在网上查到*/
return ((int)s%7); //与上语句同属计算日期对应的星期
}
int printallyear(int year)/*这个函数是用来输出全年的日历*/
{
int temp;
int i,j;
printf("\n\n%d calander\n",year);
for(i=1;i<=12;i++)
{
printf("\n\n%s(%d)\n",month_str[i-1],i); //输出月分名称
printf("0 1 2 3 4 5 6 \n");
printf("s m t w t f s \n\n");
temp=daysearch(year,i,1);
for(j=1;j<=month_day(year,i)+temp;j++)
{
if(j-temp<=0)
printf(" ");
else if(j-temp<10)
printf("%d ",j-temp);
else
printf("%d ",j-temp);
if(j%7==0)
printf("\n");
}
}
return 0;
}
int main()
{
int option,da;
char ch;
int year,month,day;
printf("copyright @ 2005 tianqian all rights reserved!:):):)");
printf("\n\nwelcome to use the wannianli system!\n");
while(1)
{
printf("\nplease select the service you need:\n"); //用来提示选择执行功能
printf("\n1 search what day the day is"); //选择1时,用来计算这一天是星期几
printf("\n2 search whether the year is leap year or not"); //计算是否这年是闰年
printf("\n3 print the calander of the whole year"); //输入全年的日历
printf("\n4 exit\n"); //选择退出程序
scanf("%d",&option);
switch(option) //用来选择执行
{
case 1:
while(1)
{
printf("\nplease input the year,month and day(xxxx,xx,xx):"); //提示输入
scanf("%d,%d,%d,%c",&year,&month,&day); //读入数据
da=daysearch(year,month,day); //调用daysearch()函数来计算是星期几
printf("\n%d-%d-%d is %s,do you want to continue?(y/n)",year,month,day,week[da]);
fflush(stdin); //刷新输入缓冲区
scanf("%c",&ch);
if(ch=='n'||ch=='n')
break;
}
break;
case 2: /*当为2时,进行相应运算*/
while(1)
{
printf("\nplease input the year which needs searched?(xxxx)");
scanf("%d",&year);
if(isleapyear(year))
printf("\n%d is leap year,do you want to continue?(y/n)",year);
else
printf("\n%d is not leap year,do you want to continue(y/n)?",year);
fflush(stdin);
scanf("%c",&ch);
if(ch=='n'||ch=='n')
break;
}
break;
case 3: /*当为3时运行相应的运算*/
while(1)
{
printf("\nplease input the year which needs printed(xxxx)");
scanf("%d",&year);
printallyear(year);
printf("\ndo you want to continue to print(y/n)?");
fflush(stdin);
scanf("%c",&ch);
if(ch=='n'||ch=='n')
break;
}
break;
case 4:
fflush(stdin);
printf("are you sure?(y/n)");
scanf("%c",&ch);
if(ch=='y'||ch=='y')
exit(1);
break;
default:
printf("\nerror:sorry,there is no this service now!\n");
break;
}
}
return 0;
}
◆◆
评论读取中....
请登录后再发表评论!
◆◆
修改失败,请稍后尝试