#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
int DayofYear(int year,int month, int day);
void MonthDay(int year,int yearDay,int *pMonth,int *pDay);
void Menu(void);
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()
{
int year,month,day,yearDay;
char c;
Menu();
c = getchar();
switch(c)
{
case '1':printf_s("please enter year,month,day:");
scanf_s("%d,%d,%d",&year,&month,&day,3*sizeof(year));
yearDay = DayofYear(year,month,day);
printf_s("yearDay = %d
",yearDay);
break;
case '2':printf_s("Please enter year,yearDay:");
scanf_s("%d,%d",&year,&yearDay);
MonthDay(year,yearDay,&month,&day);
printf_s("month = %d,day = %d
",month,day);
break;
case '3':exit(0);
default :printf_s("input the error");
}
system("pause");
return 0;
}
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;
}
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;
}
void Menu(void)
{
printf_s("1. year/month/day ->yearDay
");
printf_s("2. yearDay -> year/month/day
");
printf_s("3. Exit
");
printf_s("please enter your choice:");
}