本程序的作用是计算所输入的日期在该年中的第几天。
在RQ函数中判断所输入的日期是否正确,若不正确则重新输入直至输入正确,days函数用于处理该日属于该年中第几天的问题并进行打印。
typedef struct _Date
{
int year;
int month;
int day;
}Date;
void days(Date p,int* a)
{
int sum, i;
sum = p.day;
for (i = 0; i < p.month - 1; ++i)
{
sum += a[i];
}
if ((p.year % 4 == 0 && p.year % 100 != 0 || p.year % 400 == 0) && p.month > 2)
printf("该日是在%d年中的第%d天\n", p.year, sum + 1);
else
printf("该日是在%d年中的第%d天\n", p.year, sum);
}
int RQ(int year, int month, int day)
{
if (month < 1 || month>12)
{
return 0;
}
if (year < 1)
{
return 0;
}
switch (month)
{
case 1:case 3:
case 5:case 7:
case 8:case 10:
case 12:
if (day < 1 || day>31)
{
return 0;
break;
}
case 4:case 6:case 9:case 11:
if (day < 1 || day>30)
{
return 0;
break;
}
case 2:
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
if (day < 1 || day > 29)
{
return 0;
break;
}
}