日历问题
日历,输入年份和月份,输出该月日历
程序代码:
#include <stdio.h> #include <stdbool.h> //Define Global Variables int year,month,numberDays,weekFirstDay,lastDay; int daysPerMonth[12]={ 31,28,31,30,31,30,31,31,30,31,30,31 }; bool leapFlag; //Declare some Functions in the Program. void space (void); void dispcal (void); bool leapYear (void); int numbersOfMonth (void); int weekOfFirstday (void); int sumDay (int n); //Main Program Start... int main (void) { printf ("please date[year month]:"); scanf ("%i%i",&year,&month); printf ("\n"); //Call Functions. //weekOfFirstday():Get week of Current Month First day. //numbersOfMonth():Get numbers of Current Month. //dispcal(weekFirstDay):display Cureent month calendar. weekOfFirstday(); numbersOfMonth(); dispcal(); //Return OS. return 0; } // int weekOfFirstday (void) { switch (month) { case 1: numberDays=1;break; case 2:numberDays=sumDay(2);break; case 3:numberDays=sumDay(3);break; case 4:numberDays=sumDay(4);break; case 5:numberDays=sumDay(5);break; case 6:numberDays=sumDay(6);break; case 7:numberDays=sumDay(7);break; case 8:numberDays=sumDay(8);break; case 9:numberDays=sumDay(9);break; case 10:numberDays=sumDay(10);break; case 11:numberDays=sumDay(11);break; case 12:numberDays=sumDay(12);break; } if(leapYear()==true&&month>2) { numberDays+=1; } weekFirstDay=((year-1)*365+(year-1)/4-(year-1)/100+(year-1)/400+numberDays)%7; return weekFirstDay; } // int sumDay (int n) { int i,sum=1; for(i=0;i<(n-1);i++) { sum += daysPerMonth[i]; } return sum; } void space (void) { int i; for(i=0;i<weekFirstDay;i++) { printf ("\t"); } } // void dispcal (void) { int index,col,firstDay=1; printf ("---------------------------------------------------\n"); printf ("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n\n"); space(); for(col=1;col<=7-weekFirstDay;col++,firstDay++) { printf ("%i\t",firstDay); } printf ("\n\n"); for(index=1,col=firstDay;col<=lastDay;col++,index++) { printf ("%i\t",col); if(index%7==0) { printf ("\n\n"); } } printf ("\n--------------------------------------------------\n"); } // bool leapYear (void) { if(((year%4==0&&year%100!=0)||year%400==0)&&month==2) { leapFlag=true; }else{ leapFlag=false; } return leapFlag; } // int numbersOfMonth (void) { if(leapYear()==true) { lastDay=daysPerMonth[month-1]+1; }else{ lastDay=daysPerMonth[month-1]; } return lastDay; }
程序代码:
root@~/prog #./cale please date[year month]:2011 7 --------------------------------------------------- Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 -------------------------------------------------- root@~/prog #