用C语言在控制台打印日历
/*************************************** Student:赵忠印 032-25 Date: 2006.9.23 Purpose: Coursework 1 这个程序能打印一年的日历 ***************************************/ #include<stdio.h> int isLeapyear(int year)/*this function is used to calculate that if a year is a leap year*/ { if(year%4==0) { if(year%100!=0) return 1; else if(year%100==0&&year%400==0) return 1; else return 0; } else return 0; } void printCalendar(int year,int day)/*this function is used to print the calendar of a year*/ { int month; int days; int FristDayOfMonth; int LastDayOfMonth; int i; for(month=1;month<=12;month++) { printf("\n\n"); switch(month) { case 1:printf("\tJanuary %d\n",year); days=31;/*how many days in this month*/ break; case 2:printf("\tFebruary %d\n",year); days=(isLeapyear(year))? 29:28;/*this is used to calculate if there are 29 or 28 days in february*/ break; case 3:printf("\tMarch %d\n",year); days=31; break; case 4:printf("\tApril %d\n",year); days=30; break; case 5:printf("\tMay %d\n",year); days=31; break; case 6:printf("\tJune %d\n",year); days=30; break; case 7:printf("\tJuly %d\n",year); days=31; break; case 8:printf("\tAugust %d\n",year); days=31; break; case 9:printf("\tSeptember %d\n",year); days=30; break; case 10:printf("\tOctober %d\n",year); days=31; break; case 11:printf("\tNovember %d\n",year); days=30; break; case 12:printf("\tDecember %d\n",year); days=31; break; default:printf("error"); } printf(" S M T W T F S\n");/*print the days of a week*/ if(month==1)/*January*/ { FristDayOfMonth=day;/*this is used to calculate the day of week that it is the frist day of the month*/ LastDayOfMonth=(days-8+day)%7;/*this is used to calculate the day of week that it is the last day of the month*/ } else/*other months*/ { FristDayOfMonth=(LastDayOfMonth==7)?1:LastDayOfMonth+1;/*this is used to calculate the day of week that it is the frist day of the month*/ LastDayOfMonth=(days-8+FristDayOfMonth)%7;/*this is used to calculate the day of week that it is the last day of the month*/ } for(i=1;i<FristDayOfMonth*3-2;i++)/*calculate the location of the frist day of a month*/ printf(" "); for(i=1;i<=days;i++)/*print the calendar of a month*/ { printf("%3d",i); if((i-(7-FristDayOfMonth+1))%7==0) printf("\n");/* because it is Saturday*/ } } } int main() { int choose;/*what is day today*/ int year;/*which year,you'd better input a interger*/ printf("\n\n\t\tWelcome You!\n\n"); printf("please input a year\n"); scanf("%d",&year); printf("\nplease choose the day of the week that is January 1st\n"); printf("\n1:Sunday 2:Monday 3:Tuseday 4:Wednesday 5:Thrusday 6;Friday 7:Saturday\n"); printf("\nyour choose:[ ]\b\b"); scanf("%d",&choose); if(choose<1||choose>7) { printf("choose error"); return 1; } printCalendar(year,choose);/*this function will print the whole calendar of this year that it input by you*/ getchar(); getchar(); return 1;/*because my English is poor,it is difficulty to read,please excuse you,I swear I will study it well*/ }