代码实现:
#include <stdio.h>
int mouth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int leap(int year){
if ((year%4==0&&year%100!=0)||year%400==0) {
return 1;
}
return 0;
}
int main()
{
int date1,date2,y,m,d,yy,mm,dd,day=1;
printf("请输入您的出生日期:");
scanf("%d",&date1);
printf("请输入今天的日期:");
scanf("%d",&date2);
y=date1/10000;
yy=date2/10000;
m=date1%10000/100;
mm=date2%10000/100;
d=date1%100;
dd=date2%100;
while (y<=yy-1) {
day+=365+leap(y);
y++;
}
while (y!=yy||m!=mm||d!=dd) {
d++;
day++;
if (m==2&&d>mouth[m-1]+leap(y)) {
m++;
d=1;
} else if (d>mouth[m-1]) {
m++;
d=1;
}
if (m>12) {
y++;
m=1;
}
}
printf("您已经活了%d天。\n",day);
return 0;
}