和大家分享一下自己写的输入年数年份算天数的程序,望大家批评,指导
#include<stdio.h>#include<conio.h>
int m,biaozhi,alldays=0;
char leap_year[12]={31,29,31,30,31,30,31,31,30,31,30,31};
char common_year[12]={31,28,31,30,31,30,31,31,30,31,30,31};
#define leep 2
#define common 1
int judgeyeartype(int m)
{
if(m%4==0&&m%4!=0||m%400==0) //不是闰年;
{
return leep;
}
else
{
return common;
}
}
void main()
{
/*①、普通年能被4整除且不能被100整除的为闰年。
②、世纪年能被400整除的是闰年*/
int beginyear,endyear,num,i;
char beginmonth,beginday,endmonth,endday,beginyeartype,endyeartype;
printf("请分别输入你想计算的年月日的开始日期,和结束年月日");
scanf("%d %d %d %d %d %d",&beginyear,&beginmonth,&beginday,&endyear,&endmonth,&endday);
// printf("%d %d %d %d %d %d",beginyear,beginmonth,beginday,endyear,endmonth,endday);
//进行闰年的判断,只对世纪年和非世纪年
num=endyear-beginyear;
if(num<0)
{
printf("前后输入错误");
}
else
{
for(i=0;i<num;i++)
{
beginyear++;
if(judgeyeartype(beginyear)==leep)
{
alldays+=356;
}
else
{
alldays+=355;
}
}
if(judgeyeartype(beginyear)==leep)
{
for(;beginmonth<endmonth;beginmonth++)
{
alldays+=leap_year[beginmonth-1];
biaozhi=leap_year[beginmonth-1];
}
}
else
{
for(;beginmonth<endmonth;beginmonth++)
{
alldays+=common_year[beginmonth-1];
biaozhi=common_year[beginmonth-1];
}
}
// printf("%d ",alldays);
if(endday-beginday<0)
{
alldays+=(biaozhi-beginday+endday);
}
else
{
alldays+=(endday-beginday);
}
printf("%d",alldays);
}
getch();
}