利用结构型实现(年-月-日)=>(年-天数)以及(年-天数)=>(年-月-日)之间的转换。
计算天数前先判断闰年。 char daytab[2][1 3] {{0,31,28,31,30,3 1,30,3 1,3 1,30,3 1,30,31}, {0,31,29,31,30,3 1,30,3 1,3 1,30,3 1,30,31}}; struct date { int year;int month;int day; } dl; int day_of_year(int year;int month;int day) {int i,leap; leap=year%4=-0&&year%100 1=0¦¦year%400==0; for(i=1;idaytab[1eap][i];i++) yearday-=daytab[1eap][i]; *pmonth=i; *pday=yearday; } main() {int k,days; prinff("Please select:1.(year,month,day)->(yeardays)\n"); prntf("Please select:2.(year,yeardays)->(year,month,day)\n"); scanf("%d",&k); if(k==1) { prinff("Enter year,month,day:\n"), scanf("%d,%d,%d,&d1.year,&d1.month,&d1.day); printf("the year_day is:%d\n", day_of_year(d1.year,d1.month,d1.day)); } else if(k==2) { printf(Enter year,yeardays:\n"); scanf("%d,%d",&d1.year,&days); month_day(d1.year,days,&d1.month,&d1.day); printf("the year-month-day is:%d-%d-%d\n", d1.year,d1.month,dl,day); } else printf("Data error!k=1 or k=2...\n"); }