求程序,用C++完成,必有重谢。尽快完成!!!

来源:百度知道 编辑:UC知道 时间:2024/05/31 23:00:53
问题为:定义一个结构,描述日期(年,月,日),定义一个函数其参数是这个结构类型的指针。该函数的功能是计算传递进来的这个日期是这年的第几天,并返回这个结果由main函数将这个结果输出。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct
{
int year;
int month;
int day;
}Time;
int GetDay(Time *t);
int main()
{
Time testTime;
testTime.year=2008;
testTime.month=1;
testTime.day=14;
printf("%d",GetDay(&testTime));
return 0;
}
int GetDay(Time *t)
{
int mon[11]={31,28,31,30,31,30,31,31,30,31,30};
int tt=0;
int i;
if((t->year%4==0&&t->year!=0)||t->year%400==0)
{
mon[1]++;
for(i=0;i<t->month-1;i++)
{
tt+=mon[i];
}
tt+=t->day;
}
else
{
for(i=0;i<t->month;i++)
{
tt+=mon[i];
}
tt+=t->day;
}
return tt;
}