#include <stdio.h>
int day_of_year(int year,int month,int day);
void month_of_day(int year, int yearday, int *pmonth, int *pday);
static int 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 main(void)
{
int year, month, day;
printf("请输入年份:");
scanf("%d",&year);
printf("请输入月份:");
scanf("%d",&month);
printf("请输入日期:");
scanf("%d",&day);
printf("天数是:%d\n",day_of_year(year,month,day));
month_of_day(year,day_of_year(year,month,day),&month,&day);
printf("今天是%d年 %d月 %d日\n",year,month,day);
}
/*将某月某日的日期转换为该年的第几天的表示形式*/
int day_of_year(int year,int month,int day)
{
int i,leap;
//判断闰年
leap = year%4 == 0 && year%100 != 0 || year%400 == 0;
for(i = 1; i < month; i++)
day += daytab[leap][i];
return day;
}
/*将某年的第几天转换为某月某日*/
void month_of_day(int year, int yearday, int *pmonth, int *pday)
{
int i,leap;
leap = year%4 == 0 && year%100 != 0 || year%400 == 0;
for(i = 1; yearday > daytab[leap][i]; i++)
yearday -= daytab[leap][i];
//用指针改变其值
*pmonth = i;
*pday = yearday;
}
运行结果: