日期转换问题

编程解决如下日期转换问题:
1任意给定某年某月某日,打印出它是这一年的第几天。
2已知某一年的第几天,计算它是这一年的几月几日。

#include<stdio.h>
#include<stdlib.h>
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}};
void menu();
void monthday(int year,int yearday,int *pmonth,int *pday);
int dayofyear(int year,int month,int day);
int main()
{
	 int year,month,day,yearday,choice;
	 menu();
	 scanf("%d",&choice);
	 switch(choice)
	 {
	 	 case 1:printf("please enter year,month,day:");
	 	        scanf("%d,%d,%d",&year,&month,&day);
	 	        yearday=dayofyear(year,month,day);
	 	        printf("yearday=%d\n",yearday);
	 	        break;
	     case 2:printf("please enter year,yearday");
	            scanf("%d,%d",&year,&yearday);
	            monthday(year,yearday,&month,&day);
	            printf("month=%d,day=%d\n",month,day);
	            break;
	     case 3:exit(0);
	     default:printf("input error!");
	 }	  
	 return 0;
}
void menu()
{
	 printf("1.year/month/day->yearday\n");
	 printf("2.yearday-->year/month/day\n");
	 printf("3.exit\n");
	 printf("please enter your choice:");
	 
}
void monthday(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=yearday-daytab[leap][i];
	}
	*pmonth=i;
	*pday=yearday;
}
int dayofyear(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=day+daytab[leap][i];
	 	 
	 }
	 return day;
}

上一篇:蓝桥杯 第四讲 枚举、模拟与排序


下一篇:第十一届蓝桥杯B组C/C++省赛第二场(供个人复习时使用)