#include "stdio.h" #include "stdlib.h" #include "string.h" static char daytab[2][13] = { {0,31,28,31,30,31,30,31,31,30,31,30,31}, {0,31,29,31,30,31,30,31,31,30,31,30,31} }; int day_of_year(int year,int month,int day) { int i,leap; leap = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0); if(month < 1 || month > 12) return -1; if(day < 1 || day > daytab[leap][month]) return -1; for(i = 1;i < month;i++) day += daytab[leap][i]; return day; } void month_day(int year,int yearday,int *pmonth,int *pday) { int i,leap; if(year < 1) { *pmonth = -1; *pday = -1; return; } leap = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0); for(i = 1;i <= 12 && yearday > daytab[leap][i];i++) yearday -= daytab[leap][i]; if(i > 12 && yearday > daytab[leap][12]) { *pmonth = -1; *pday = -1; } else { *pmonth = i; *pday = yearday; } } int main() { int n; int month = 0,day = 0; int *pmonth = &month,*pday = &day; n = day_of_year(2014,11,14); printf("%d ",n); month_day(2014,318,pmonth,pday); printf("%d %d",*pmonth,*pday); return 0; }
这个程序中,得到的注意事项:指针必须初始化,否则后果很严重。。不过我现在还不知道到底什么严重个后果。。。