#include <stdio.h>
/*练习二维数组,判断给出的天数是某年中的几月几日;
或判断给出的日期是某年中的第几天
版本2.0 用指针方式代替数组下标方式
*/
int day_year(int year, int month, int day);
void day_month(int yearday, int year, int*, int*);
char daylab[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 main()
{
int day;
int *pday = NULL, *pmonth = NULL;
int d, m, y;
printf("请依次输入年月日:\n");
scanf("%d%d%d", &y, &m, &d);
day = day_year(y, m, d);
printf("%d年的%d月%d日是这一年的第%d天\n", y, m, d, day);
day_month(day, y, pday, pmonth);
return 0;
}
int day_year(int year, int month, int day)
{
int leap, yearday = 0;
int i;
leap = (year % 100 == 0 && year % 4 == 0) || year % 400 == 0; //还可以利用逻辑语句这样进行赋值
char *p;
p = daylab[leap];
if(year <= 0 || month < 0 || month > 12 || day < 0 || day > *(p+month))
return -1;
for(i = 1; i < month; i++){
yearday += *(p + i);
}
yearday += day;
return yearday;
}
void day_month(int yearday, int year, int *day, int *month)
{
int leap = 0;
leap = (year % 4 == 0 && year % 100 == 0) || year % 400 == 0;
if((leap == 0 && yearday > 365) || (leap == 1 && yearday > 366) || year <= 0)
return ;
char *p = daylab[leap];
p++;
while(yearday > *p){
yearday -= *p++;
}
day = &yearday;
month = *p - *daylab[leap];
printf("%d %d\n", *month, *day);
}