由于C语言当初没学好,像C的结构体,文件流,指针等都还弄不明白。
现在又捡起来再看一本C语言的书--Application Programming in Ansi C
下面是最近的一个代码,关于打印日历的。
/*Calendar
打印出某年的日历,由用户指出1月1日是星期几以及该年是否为闰年。
表示1月1日是星期几的编码:
0 Sunday 1 Monday 2 Tuesday 3 Wednesday 4 Thuray 5 Friday 6 Saturday
表示是否为闰年的编码:
0 不是 1 是
@author Jeff Chen
@version 1.0 date:2012-03-08
*/
#include<stdio.h>
int main()
{
int day_code;
int leap_year;
do{
printf("Enter day and leap year code:");
scanf("%d%d",&day_code,&leap_year);
}while(day_code<0||day_code>6);
int days_in_month;
int month;
int day;
for(month=1;month<=12;month++)
{
switch(month)
{
case 1:
printf("\n\n\tJanuary");
days_in_month=31;
break;
case 2:
printf("\n\n\tFebruary");
days_in_month=leap_year ? 29:28;
break;
case 3:
printf("\n\n\tMarch");
days_in_month=31;
break;
case 4:
printf("\n\n\tApril");
days_in_month=30;
break;
case 5:
printf("\n\n\tMay");
days_in_month=31;
break;
case 6:
printf("\n\n\tJune");
days_in_month=30;
break;
case 7:
printf("\n\n\tJuly");
days_in_month=31;
break;
case 8:
printf("\n\n\tAugust");
days_in_month=31;
break;
case 9:
printf("\n\n\tSeptember");
days_in_month=30;
break;
case 10:
printf("\n\n\tOctober");
days_in_month=31;
break;
case 11:
printf("\n\n\tNovember");
days_in_month=30;
break;
case 12:
printf("\n\n\tDecember");
days_in_month=31;
break;
}
//输出星期
printf("\n\nSun Mon Tue Wed Thu Fri Sat\n");
for(day=1;day<day_code*5+1;day++)
{
printf(" ");
}
for(day=1;day<=days_in_month;day++)
{
printf("%2d ",day);
if((day+day_code)%7>0)
printf(" ");
else
printf("\n");
}
//设置下一个星期的星期代码
day_code=(day_code+days_in_month)%7;
}
return 0;
}