标签:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <math.h> 4 #define ISYEAR(year) (year%400==0) ||(year%4==0&&year%100!=0) 5 int DayofYear(int year, int month, int day); 6 7 8 int main(int argc, char *argv[]) 9 { 10 int year1, month1, day1; 11 int year2, month2, day2; 12 int syear = 0, sday = 0, deltdate = 0; 13 int date1, date2, date; 14 int mon1[] = {0,31,29,31,30,31,30,31,31,30,31,30,31}; //闰年二月29天 15 int mon2[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; 16 int i; 17 printf("输入第一个日期(例如:2010-1-17)"); 18 scanf("%d-%d-%d",&year1, &month1, &day1); 19 //检查输入的日期是否合法 20 if(year1<1){ 21 printf("年份非法,重新输入"); 22 scanf("%d-%d-%d",&year1, &month1, &day1); 23 } 24 else if(ISYEAR(year1)){ 25 if(month1<1||month1>12){ 26 printf("月份非法,重新输入"); 27 scanf("%d-%d-%d",&year1, &month1, &day1); 28 } 29 else{ 30 if(day1 > mon1[month1]||day1<1){ 31 printf("日期非法,重新输入"); 32 scanf("%d-%d-%d",&year1, &month1, &day1); 33 } 34 } 35 } 36 else { 37 if(month1<1||month1>12){ 38 printf("月份非法,重新输入"); 39 scanf("%d-%d-%d",&year1, &month1, &day1); 40 } 41 else{ 42 if(day1 > mon2[month1]||day1<1){ 43 printf("日期非法,重新输入"); 44 scanf("%d-%d-%d",&year1, &month1, &day1); 45 } 46 } 47 } 48 //getchar(); 49 printf("输入第二个日期(例如:2015-4-29)"); 50 scanf("%d-%d-%d",&year2, &month2, &day2); 51 if(year2<1){ 52 printf("年份不合法,请重新输入"); 53 scanf("%d-%d-%d",&year2, &month2, &day2); 54 } 55 if(month2<1 || month2>12){ 56 printf("月份不合法,请重新输入"); 57 scanf("%d-%d-%d",&year2, &month2, &day2); 58 } 59 if(ISYEAR(year2)){ 60 if(day2>mon1[month2]||day2<1){ 61 printf("日期非法,请重新输入"); 62 scanf("%d-%d-%d",&year2, &month2, &day2); 63 } 64 } 65 else{ 66 if(day2>mon2[month2]||day2<1){ 67 printf("日期非法,请重新输入"); 68 scanf("%d-%d-%d",&year2, &month2, &day2); 69 } 70 } 71 72 //分别计算两个日期距离当年1月1日的天数 73 date1 = DayofYear(year1,month1,day1); 74 date2 = DayofYear(year2,month2,day2); 75 //默认year1<year2 76 if(year1==year2){ 77 if(date1>date2) 78 deltdate = date1 - date2; 79 else 80 deltdate = date2 - date1; 81 } 82 else if(year1<year2){ 83 for(i=year1;i<year2;i++){ 84 if(ISYEAR(i)) 85 syear += 366; 86 else 87 syear += 365; 88 } 89 deltdate = syear - date1 + date2; 90 } 91 else{ 92 for(i=year1;i>year2;i--){ 93 if(ISYEAR(i)) 94 syear += 366; 95 else 96 syear += 365; 97 } 98 deltdate = syear - date1 + date2; 99 } 100 101 printf("\n%d年%d天%d日距离%d年%d天%d日相隔%d天.\n\n",year2,month2,day2,year1,month1,day1,deltdate); 102 system("pause"); 103 return 0; 104 } 105 106 107 #define ISYEAR(year) (year%400==0) ||(year%4==0&&year%100!=0) 108 109 int DayofYear(int year, int month, int day) 110 { 111 int sum = 0; 112 int index = 0; 113 for(index=1;index<month;index++){ 114 switch(index){ 115 case 1: 116 case 3: 117 case 5: 118 case 7: 119 case 8: 120 case 10: 121 case 12: 122 sum += 31; 123 break; 124 case 2: 125 if(ISYEAR(year)){ 126 sum += 29; 127 } 128 else 129 sum +=28; 130 break; 131 case 4: 132 case 6: 133 case 9: 134 case 11: 135 sum += 30; 136 break; 137 138 } 139 } 140 sum += day; 141 return sum; 142 }
2015年4月30日 计算两个日期天数,目前为止写的最长的代码
标签:
原文地址:http://www.cnblogs.com/luolizhi/p/4469017.html