#include <stdio.h>
typedef struct date {
int year ;
int month ;
int day ;
} DATE ;
int leapyear(int year)
{
int r = 0 ;
if(! (year % 4) && (year < 1582 || year % 100 || ! (year % 400))) r = 1 ;
return r ;
}
int mdays(int year , int month)
{
int md[] = {31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31} , r ;
r = md[month - 1] ;
if(leapyear(year) && month == 2) r ++ ;
if(year == 1582 && month == 10) r -= 10 ;
return r ;
}
int days(DATE date)
{
int k , r ;
for(r = date . day , k = 1 ; k < date . month ; k ++) r += mdays(date . year , k) ;
return r ;
}
int main(void)
{
DATE date ;
printf("year month day please : ") ;
scanf("%d%d%d" , & date . year , & date . month , & date . day) ;
printf("%d\n" , days(date)) ;
}
编译、运行实况:D:\00.Excise\C>g++ -o days days.c
D:\00.Excise\C>days
year month day please : 2020 12 5
340
D:\00.Excise\C>
|