资源描述:
《c语言计算两个不同日期间的天数》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、#include#include#include#includestructdata{intyear,month,day;}d1,d2;intjudge(inta,intb,intc){intp[12]={31,28,31,30,31,30,31,31,30,31,30,31};inty[12]={31,29,31,30,31,30,31,31,30,31,30,31};if(b<=0
2、
3、b>12){printf("月数有错,请重新输入!");return0;}if((a
4、%4==0)&&(a%100!=0)
5、
6、(a%400==0)){if(c<=0
7、
8、c>y[b-1]){printf("日数有错,请重新输入!");return0;}}else{if(c<=0
9、
10、c>p[b-1]){printf("日数有错,请重新输入!");return0;}}return1;}intcount(inta,intb,intc){intp[12]={31,28,31,30,31,30,31,31,30,31,30,31};inty[12]={31,29,31,30,31,30,31,31,30,31,30,31};intsum=
11、0;if((a%4==0)&&(a%100!=0)
12、
13、(a%400==0))for(inti=0;i14、
15、ch=='Y'){sum=0;do{printf("请输入第一个日期(中间用空格隔开):");scanf("%d%d%d",&d1.year,&d1.month,&d1.day);
16、k=judge(d1.year,d1.month,d1.day);}while(k==0);do{printf("请输入第二个日期(中间用空格隔开):");scanf("%d%d%d",&d2.year,&d2.month,&d2.day);k=judge(d2.year,d2.month,d2.day);}while(k==0);a=d1.year>d2.year?d1:d2;b=d1.year>d2.year?d2:d1;if(a.year==b.year)//当年份一样应该判断月份{if(d1.month>d2.month){a=d1;b=d2
17、;}else{b=d1;a=d2;}}if(a.year==b.year&&a.month==b.month)//年份和月份一样应该判断天数大小{if(d1.day>d2.day){a=d1;b=d2;}else{a=d2;b=d1;}}for(p=b.year;p18、
19、(p%400==0))sum+=366;elsesum+=365;}m=count(a.year,a.month,a.day);n=count(b.year,b.month,b.day);if((b.year%4=
20、=0)&&(b.year%100!=0)
21、
22、(b.year%400==0))sum=sum+m-n;elsesum=sum+m-n;printf("%d",sum);printf("是否继续计算?(y/n):");getchar();//(接收无用的回车字符)ch=getchar();//ch=getch();}}