#include <stdio.h>
#include <time.h>
//获取公历年初至某整月的天数
int year_sumday(int year,int month)
{
int sum=0;
int rui[12]={31,29,31,30,31,30,31,31,30,31,30,31};
int ping[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int ruiflag=0;
if((year%4==0 &&year%100!=0) || year%400==0) ruiflag=1;
for(int index=0;index<month-1;index++)
{
if(ruiflag==1) sum+=rui[index];else sum+=ping[index];
}
return sum;
}
//获取公历年的天数
int year_alldays(int year)
{
if((year%4==0 && year%100!=0) || year%400==0) return 366; else return 365;
}
//获取公历年某月某日至年末的天数
int year_half_sumday(int year,int month,int day)
{
return year_alldays(year)-year_sumday(year,month)-day;
}
//获取从出生年月日至当前日期的总天数
int get_g_alldays(int year,int month,int day)
{
struct tm t;
time_t now;
time(&now);
localtime_s(&t,&now);
int cur_year=t.tm_year+1900;
int cur_month=t.tm_mon+1;
int cur_day=t.tm_mday;
int days=year_half_sumday(year,month,day);
year++;
while(year<cur_year)
{
days+=year_alldays(year);
year++;
}
int days2=year_sumday(cur_year,cur_month);
return days+days2+cur_day;
}
//获取从出生年月日至若干年后的总天数
int get_n_alldays(int year,int month,int day,int next_year)
{
int days=year_half_sumday(year,month,day);
year++;
while(year<next_year)
{
days+=year_alldays(year);
year++;
}
int days2=year_sumday(next_year,month);
return days+days2+day;
}
int main()
{
int cur_days=get_g_alldays(1975,12,23);
printf("你已经活了%d 天 .........",cur_days);
int remain_days=get_n_alldays(1975,12,23,1975+80)-cur_days;
printf("你还能活%d 天\n",remain_days);
return 0;
}