#include
#include
#include
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},
};
static char *name[] = {
"Illegal month",
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"
};
int date_to_day_point(int year, int month, int day);
void day_to_date_point(int year, int yearday, int *pmonth, int *pday);
char *month_name(int n);
int main() {
int year = 2019, month = 7, day = 20;
printf("%d年%d月%d号是今年第%d天.\n",year, month, day,date_to_day_point(year, month, day));
int yearday = 201;
int month_1, day_1;
day_to_date_point(year, yearday, &month_1, &day_1);
printf("%d年第%d天是今年的%d月%d号.\n", year, yearday, month_1, day_1);
printf("%s\n", month_name(month_1));
printf("%s\n", name[2]);
return 0;
}
int date_to_day_point(int year, int month, int day) {
int leap;
char *p;
leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
p = daytab[leap];
while (--month) {
day += *++p; //因为*p = 0存在,先++
}
return day;
}
void day_to_date_point(int year, int yearday, int *pmonth, int *pday) {
int leap;
char *p;
leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
p = daytab[leap];
while (yearday >= *++p) {
yearday -= *p; //已经++了
}
*pmonth = p - *(daytab + leap); //重点!!!!!!!!!!!
*pday = yearday;
}
char *month_name(int n) {
return (n < 1 || n > 12) ? name[0] : name[n];
}
注意这行代码
*pmonth = p - *(daytab + leap); //重点!!!!!!!!!!!)