题目叙述
*按如下函数原型用函数编程解决如下的日期转换问题(要求考虑闰年的问题): 输入某一年的第几天,计算并输出它是这一年的第几月第几日。 / 函数功能: 对给定的某一年的第几天,计算它是这一年的第几月第几日 函数入口参数:整型变量year,存储年 整型变量yearDay,存储这一年的第几天 函数出口参数:整型指针pMonth,指向存储这一年第几月的整型变量 整型指针pDay,指向存储第几日的整型变量 函数返回值: 无 */ void MonthDay(int year, int yearDay, int pMonth, int pDay); 输入提示信息:“Please enter year, yearDay:” 输入格式:"%d,%d" 输出提示信息和格式:“month = %d, day = %d\n”
代码如下
#include <stdio.h>
void MonthDay(int year, int yearDay, int *pMonth, int *pDay);
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;
printf("Please enter year, yearDay:");
scanf("%d,%d", &year, &yearDay);
MonthDay(year, yearDay, &month, &day);
printf("month = %d, day = %d\n", month, day);
return 0;
}
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;
}
|