回复 5楼 S_Ringo
但是还是要考虑闰年问题啊,笼统的乘365不成吧。。
#include <stdio.h>
#include <math.h>
int year_leap(int );
int day_of_year(int , int , int );
main()
{
struct date
{
int year;
int month;
int day;
};
int sum_day1, sum_day2, day_between;
struct date date1, date2;
printf("please in put the first date\n");
printf("year month date\n");
scanf("%d%d%d", &date1.year,&date1.month,&date1.day);
printf("compute the day of the year:");
sum_day1 = day_of_year (date1.year , date1.month , date1.day );
printf("first days :%d\n", sum_day1);
printf("please in put the second date\n");
printf("year month date\n");
scanf("%d%d%d", &date2.year,&date2.month,&date2.day);
printf("compute the day of the year:");
sum_day2 = day_of_year(date2.year , date2.month , date2.day);
printf("second days :%d\n", sum_day2);
day_between = abs(sum_day2 - sum_day1);
printf("the number of days between two date is:%d\n", day_between);
}
int year_leap(int year)
{
int flag;
if (year%4==0&&year%100!=0||year%400==0)
flag = 0;
else
flag = 1;
return flag;
}
int day_of_year(int year, int month, int day)
{
int i;
int dayofmonth[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}
};
if (year_leap(year))
for (i = 1; i<month; i++)
day += dayofmonth[year_leap(year)][i] ;
return day;
}
这个程序仅仅适用于计算同一年中两日期之间的天数,不适用于任意两个日期间的天数!