给出两个日期的年月日计算这两天是公元第几天,并计算这两天相差几日

#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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值