//1.定义一个结构体变量(包括年,月,日)。计算该日是本年中的第几天,注意闰年问题。
//2.写一个days,实现第一题的计算。由主函数将年,月,日传递给days函数
//,计算的日子数传回主函数输出。
#include <stdio.h>
//struct定义一个结构体变量
typedef struct Date
{
int year;
int mon;
int day;
}Date;
//判断是否是闰年
int Year(int a)
{
return((a%40&&a%100!=0)||a%4000)?1:0;
}
int Days(Date *p)
{
int tmp=0;
int y;
y=p->year;
//使用switch灵活处理,将本月之前的日子加上
switch(p->mon)
{
case 12:
tmp+=30;
case 11:
tmp+=31;
case 10:
tmp+=30;
case 9:
tmp+=31;
case 8:
tmp+=31;
case 7:
tmp+=30;
case 6:
tmp+=31;
case 5:
tmp+=30;
case 4:
tmp+=31;
case 3:
tmp+=28;
case 2:
tmp+=31;
case 1:
tmp+=0;
}
//将这个月已经过的日子加上
tmp+=(p->day);
//如果已经超过2月,判断是否增添闰年的那一天
if((p->mon)>2)
{
tmp+=Year(y);
}
//返回已经过了多少天
return tmp;
}
int main()
{
Date today={2019,2,26};
Date *p=&today;
//使用指针传递,占用内存小,速度快。
printf("%d\n",Days§);
return 0;
}
//虽然这道题比较简单,但是要把这个写好,还是需要细心