一个简单的日期转换程序

编程解决日期转换问题具体要求如下:

  1. 任意给定某年某月某日,打印出他是这一年的第几天。
  2. 已知某年的第几天,计算他是这一年的第几月第几日
#include 
#include 

static int dayTab[][13] = {{0,31,28,31,30,31,31,30,3130,31},
					{0,31,29,31,30,31,31,30,3130,31}};

/*函数功能:对给定的某年某月某日计算出他在这一年的第几天
函数参数:整型变量year,month,day,分别代表年,月,日
函数返回值:这一年的第几天
*/
int DayofYear(int year,int month,int day)
{
	int i,leap;
	/*若year为闰年,则leap值为1,就用第二行元素dayTab[1][i]计算,
	否则leap值为0,用第一行dayofTab[0][i],则dayofTab[leap][i]计算*/
	leap = (((year%4 == 0)&&(year%100 !=0))||(year%400 == 0));
	for(i=1;idayTab[leap][i];i++)
	{
		yearDay = yearDay - dayTab[leap][i];
	}
	*pMonth = i;/*将计算出的月份值赋给pMonth所指向的变量*/
	*pDay = yearDay;/*将计算出的日号赋值给pDay所指向的变量*/
}

/*函数功能:显示菜单
函数参数:无
函数返回值:无
*/
void Menu(void)
{
	printf("1.year/month/day->yearDay\n");
	printf("2.yearDay->year/month/day\n");
	printf("Exit\n");
	printf("Please enter your choice:");
}

/*主函数*/
void main()
{
	int year,month,day,yearDay;
	char c;
	Menu();/*调用函数Menu()显示一个固定式菜单*/
	c = getchar();/*请输入选择*/
	switch(c)
	{
		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 the year,yearDay:");
			scanf("%d,%d",&year,&yearDay);
			MonthDay(year,yearDay,&month,&day);
			printf("month=%d,day=%d",&month,&day);
			break;
		case '3':
			exit(0);
			break;
		default:
			printf("Input error!");

	}
}

一个简单的日期转换程序_第1张图片

你可能感兴趣的:(c语言)