老师要求我们制作一个能够计算任意两个日期之间天数的程序,但是一直找不到分析思路,求大神提点。
这一个更好 #include <stdio.h> #include <math.h> int Year1 ; int Month1 ; int Day1 ; int Year2 ; int Month2 ; int Day2 ; int MonthDays[] = {31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31} ; char IsLeapYear(int Year) { if((Year % 4 == 0 && Year % 100 != 0) || Year % 400 == 0) { return 1 ; } else { return 0 ; } } int GetMidMonthDays(int m1 , int m2, int y) { int i ; int ds ; ds = 0 ; for(i = m1 + 1 ; i < m2 ; i++) { ds += MonthDays[i-1] ; if(i == 2) { if(IsLeapYear(y)) { ds++ ; } } } return ds ; } int GetSameYearDays(int y1 , int m1 , int d1 , int y2 , int m2 , int d2) { int Days ; //如果月份相同 if(m1 == m2) { Days =abs(d2 - d1) ; } else { //计算第一个月份的天数 Days = abs(MonthDays[m1 - 1] - d1) ; if(m1 == 2) { if(IsLeapYear(y1)) { Days++ ; } } if((m1 + 1) != m2) { //计算中间月份的天数 Days += GetMidMonthDays(m1 , m2, y1); } Days += d2 ; } return Days; } int GetMidYearDays(int y1,int y2) { int days=0; int i=0; for (i=y1+1;i<y2;i++) { if (IsLeapYear(i-1)) { days+=366; } else days+=365; } return days; } int GetDifferentYearDays(int y1 , int m1 , int d1 , int y2 , int m2 , int d2) { int days; if (y1==y2) { days=GetSameYearDays( y1 ,m1 ,d1 , y2 , m2 ,d2); } else { days=MonthDays[m1+1]-d1; if((y1+1) != y2) { days+=GetMidYearDays(y1,y2); } days+=GetMidMonthDays(m1-1,13,y1); if(m2==1) { days+=d2; } else { days+=GetMidMonthDays(1,m2,y2); days+=d2; } } return days; } void main(void) { int Days ; for(; ;) { printf("请输入第一个日期(年 月 日):\n"); scanf("%d%d%d" , &Year1 , &Month1 , &Day1); printf("请输入第二个日期(年 月 日):\n"); scanf("%d%d%d" , &Year2 , &Month2 , &Day2); //假定第一个日期小于第二个日期 Days = GetDifferentYearDays(Year1 , Month1 , Day1 , Year2 , Month2 , Day2); printf("两个日期相差的天数为:%d\n" , Days); } }
#include <stdio.h> /*求所在月份的天数*/ int daysMonth(int year,int month,int day) { int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int i,sum=0; for(i=0;i<month;i++) sum+=days[i]; if(month>2) /*如果是闰年则2月加一天*/ if((year%4==0)&&(year%100!=0)||(year%400==0)) sum+=1; sum+=day; return sum; } /*交换位置,避免负值*/ void swap(int x1,int x2) { int tmp=x1; x1=x2; x2=tmp; } long countDate(int y1,int m1,int d1,int y2,int m2,int d2) { int daysyear1,daysyear2; long totalDays=0; int total_day1; int tmpYear; int tmpDays; printf("The first Date is %ld-%ld-%ld/n",y1,m1,d1); printf("The second Date is %ld-%ld-%ld/n",y2,m2,d2); if(y1>y2) { swap(y1,y2); swap(m1,m2); swap(d1,d2); } if(y1==y2) { daysyear1=daysMonth(y1,m1,d1); daysyear2=daysMonth(y2,m2,d2); totalDays=abs(daysyear1-daysyear2)+1; printf("totalDays is %ld/n",totalDays-1); } else { daysyear1=daysMonth(y1,m1,d1); total_day1=365-daysyear1+1; if(m1<=2) if(y1%4==0&&(y1%100!=0)||(y1%400==0)) total_day1+=1; totalDays+=total_day1; tmpYear=y1; while(++tmpYear<y2) { tmpDays=365; if((tmpYear%4==0)&&(tmpYear%100!=0)||(tmpYear%400==0)) tmpDays+=1; totalDays+=tmpDays; } daysyear2=daysMonth(y2,m2,d2); totalDays+=daysyear2; } return totalDays-1; printf("totalDays is %ld/n",totalDays-1); } main() { int y1,m1,d1,y2,m2,d2; printf("Plsase input Date,for example 2008-8-1,2008-8-8:"); scanf("%ld-%ld-%ld,%ld-%ld-%ld",&y1,&m1,&d1,&y2,&m2,&d2); countDate(y1,m1,d1,y2,m2,d2); getch(); }