#include <stdio.h>
#define ISLOOP(year) (((year) % 4 == 0 && (year) % 100 != 0) || ((year) %400 == 0)) ? 1 : 0
struct date
{
int year;
int month;
int day;
};
const int months[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
int days(struct date *);
int main(int argc, char * argv[])
{
struct date dt1,*p;
p = &dt1;
printf("please input year,month,day:");
scanf("%d,%d,%d",&dt1.year,&dt1.month,&dt1.day);
while (dt1.year < 1900 || dt1.month > 12 || dt1.month < 1 || dt1.day > 31 || dt1.day < 1)
{
printf("your put data is error,please reinput:\n");
scanf("%d,%d,%d",&dt1.year,&dt1.month,&dt1.day);
}
printf("%d year %d month %d day is : %d day's.",p->year,p->month,p->day,days(p));
system("pause");
return 0;
}
int days(struct date *p)
{
int *month = months;
int i,result = 0;
for (i = 0; i < p->month - 1;i++)
{
result +=*month++;
}
result += p->day;
if(p->month > 2 && ISLOOP(p->year))
{
result += 1;
}
return result;
}
|