帮一MM写的一个日历小程序
帮一MM写的一个日历小程序 。程序从1980年1月1日开始可以计算到9999年12月31日程序代码:
#include <stdio.h> #include <stdlib.h> #define LEAP(x) ( !(x % 4) && ( !(x % 400) || (x %100) ) ) char *w[] = { "Sun" , "Mon", "Tue" , "Wed" , "Thu" , "Fri" ,"Sat" } ; enum {Sun , Mon ,Tue , Wed , Thu , Fri,Sat} days ; void setsp(int n) { int i ; for ( i = 1 ; i <= n ; i++) putchar(' ') ; return ; } void Print_Week(char *w[]) { int i ; for (i = 0 ; i < 7 ; i++) printf("%+5s" , w[i]) ; printf("\n") ; return ; } void Print_date(int year ,int month , int date) { int i , j = date ,month_days ; if (month == 2) month_days = 28 + LEAP(year) ; else if (month == 4 || month == 6 || month == 9 || month == 11) month_days = 30 ; else month_days = 31 ; for (i = 1 ; i <= month_days ; i++,j++) { if (j-1 == Sat){ putchar('\n') ; j = Sun ; } printf("%5d" , i) ; } return ; } int Get_day(int current_year , int current_month){ int year=1980 ,month = 1 ,day = 2 , month_days = 31 ; //1981-1-1 Tuesday while (year < current_year) { for (month = 1 ; month <= 12 ; month++) { if (month == 2) month_days = 28 + LEAP(year) ; else if (month == 4 || month == 6 || month == 9 || month == 11) month_days = 30 ; else month_days = 31 ; day = (day + (month_days % 7) ) % 7 ; } year++ ; } for (month = 1 ; month <= current_month ; month++) { if (month == 2) month_days = 28 + LEAP(year) ; else if (month == 4 || month == 6 || month == 9 || month == 11) month_days = 30 ; else month_days = 31 ; day = (day + (month_days % 7) ) % 7 ; } return day ; } int main(void) { int year , month , day ; printf("Input the year and month like \"2008-10\":") ; scanf("%d-%d", &year , &month) ; if(year < 1980 || month > 12) { fprintf(stderr,"The year can not less than 1980 and the month can not more than 12\n") ; exit(1) ; } day = Get_day(year , month - 1) ; Print_Week(w) ; switch (day) { case Sun: setsp(0) ; Print_date(year,month,day) ; break ; case Mon: setsp(5) ; Print_date(year,month,day) ; break ; case Tue: setsp(10); Print_date(year,month,day) ; break ; case Wed: setsp(15); Print_date(year,month,day) ; break ; case Thu: setsp(20); Print_date(year,month,day) ; break ; case Fri: setsp(25); Print_date(year,month,day) ; break ; case Sat: setsp(30); Print_date(year,month,day) ; break ; } putchar('\n') ; return 0 ; }
[[it] 本帖最后由 iFreeBSD 于 2008-10-30 10:26 编辑 [/it]]