#include<stdio.h>
#include<math.h>
#include<string.h>
int main()
{
int count_sum(int y,int m,int d);
int jud1,jud2,judge(int y,int m,int d);
int year1,month1,day1,year2,month2,day2;
int sum1,sum2;
int pass,passornot(char character);
printf("给出两个日期的年月日计算这两天是公元第几天,并计算这两天相差几日!\n\n\n");
label3:
printf("请输入需要查询的年月日(公元1世纪元日之后)(中间用空格间隔)比如:2000 12 23");
printf("\n\n");
label1:
printf("日期一:");
scanf("%d%d%d",&year1,&month1,&day1);
jud1=judge(year1,month1,day1);
if(!jud1)
{
printf("查无此日!请重新输入!\n\n\n");
goto label1;
}
printf("\n\n");
label2:
printf("日期二:");
scanf("%d%d%d",&year2,&month2,&day2);
jud2=judge(year2,month2,day2);
if(!jud2)
{
printf("查无此日!请重新输入!\n\n\n");
goto label2;
}
printf("\n\n");
printf("分析过程一:\n");
sum1=count_sum(year1,month1,day1);
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("分析过程二:\n");
sum2=count_sum(year2,month2,day2);
printf("\n\n\n\n\n\n\n\n");
printf("%d年%d月%d日是公元第%d天\n\n",year1,month1,day1,sum1);
printf("%d年%d月%d日是公元第%d天\n\n",year2,month2,day2,sum2);
printf("两天相差%d天!\n\n",abs(sum1-sum2));
return 0;
}
int judge(int y,int m,int d)
{
int leap_year(int year);
int bs_month(int month);
int leapyear,bsmonth;
leapyear=leap_year(y);
bsmonth=bs_month(m);
if((leapyear==1)&&(d>28)) return(0);
if((leapyear==0)&&(d>29)) return(0);
if(y<0) return(0);
else if(bsmonth==0) return(0);
else if(d<1||((bsmonth==1)&&(d>31))||((bsmonth==2)&&(d>30))) return(0);
return(1);
}
int leap_year(int year)
{
if(year%400==0) return(1);
else if((year%4==0)&&((year%100)!=0)) return(1);
else return(0);
}
int bs_month(int month)
{
int monthbs;
switch(month)
{
case 1: monthbs=1; break;
case 2: monthbs=2; break;
case 3: monthbs=1; break;
case 4: monthbs=2; break;
case 5: monthbs=1; break;
case 6: monthbs=2; break;
case 7: monthbs=1; break;
case 8: monthbs=1; break;
case 9: monthbs=2; break;
case 10: monthbs=1; break;
case 11: monthbs=2; break;
case 12: monthbs=1; break;
default: monthbs=0; break;
}
return(monthbs);
}
int count_sum(int y,int m,int d)
{
int sum_day=0,leapyear,bsmonth;
int i,j;
int leap_year(int year);
int bs_month(int month);
for(i=1;i<y;i++)
{
printf("\tyear: %d ",i);
leapyear=leap_year(i);
for(j=1;j<=12;j++)
{
printf("month: %d ",j);
if(j==2)
{
switch(leapyear)
{
case 0:sum_day=sum_day+29;break;
case 1:sum_day=sum_day+28;break;
}
}
if(j!=2)
{
bsmonth=bs_month(j);
if(bsmonth==1) sum_day=sum_day+31;
else sum_day=sum_day+30;
}
}
printf("\n\n");
}
printf("\tyear: %d ",y);
leapyear=leap_year(y);
for(j=1;j<m;j++)
{
printf("month: %d ",j);
if(j==2)
{
switch(leapyear)
{
case 0:sum_day=sum_day+29;break;
case 1:sum_day=sum_day+28;break;
}
}
if(j!=2)
{
bsmonth=bs_month(j);
if(bsmonth==1) sum_day=sum_day+31;
else sum_day=sum_day+30;
}
}
printf("month: %d ",m);
printf("\n\tday: %d\n",d);
sum_day=sum_day+d;
//printf("日期是公元第%d天\n\n",sum_day);
return(sum_day);
}
int passornot(char character)
{
if(character=='Y') return(1);
else return(0);
}
05-08
772
05-31
11-10
3744