C++练习 | 计算两日期之间天数差
1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 using namespace std; 5 6 class Date 7 { 8 private: 9 int year; 10 int month; 11 int day; 12 public: 13 void get() 14 { 15 int a,b,c; 16 cin>>a; 17 getchar(); 18 cin>>b; 19 getchar(); 20 cin>>c; 21 year=a; 22 month=b; 23 day=c; 24 return; 25 } 26 friend int days(Date a,Date b); 27 }; 28 29 int days(Date a,Date b) 30 { 31 int day1=0; 32 Date temp; 33 if((a.year<b.year)||((a.year==b.year)&&(a.month<b.month))||((a.year==b.year)&&(a.month==b.month)&&(a.day<b.day))) 34 { 35 temp=a; 36 a=b; 37 b=temp;//保证a比b日期靠后 38 } 39 if(a.year!=b.year) 40 { 41 day1+=(a.year-b.year-1)*365;//不考虑闰年和不完整年的整年天数 42 day1+=((((a.year-1)/4) - ((a.year-1)/100) + ((a.year-1)/400))-(((b.year-1)/4) - ((b.year-1)/100) + ((b.year-1)/400)));//期间闰年天数 43 for(int i=b.month;i<=12;i++) 44 { 45 switch(i) 46 { 47 case 1: 48 { 49 day1+=31; 50 break; 51 } 52 case 2: 53 { 54 day1+=28; 55 break; 56 } 57 case 3: 58 { 59 day1+=31; 60 break; 61 } 62 case 4: 63 { 64 day1+=30; 65 break; 66 } 67 case 5: 68 { 69 day1+=31; 70 break; 71 } 72 case 6: 73 { 74 day1+=30; 75 break; 76 } 77 case 7: 78 { 79 day1+=31; 80 break; 81 } 82 case 8: 83 { 84 day1+=31; 85 break; 86 } 87 case 9: 88 { 89 day1+=30; 90 break; 91 } 92 case 10: 93 { 94 day1+=31; 95 break; 96 } 97 case 11: 98 { 99 day1+=30; 100 break; 101 } 102 case 12: 103 { 104 day1+=31; 105 break; 106 } 107 } 108 } 109 day1-=b.day; 110 for(int i=a.month-1;i>=1;i--) 111 { 112 switch(i) 113 { 114 case 1: 115 { 116 day1+=31; 117 break; 118 } 119 case 2: 120 { 121 day1+=28; 122 break; 123 } 124 case 3: 125 { 126 day1+=31; 127 break; 128 } 129 case 4: 130 { 131 day1+=30; 132 break; 133 } 134 case 5: 135 { 136 day1+=31; 137 break; 138 } 139 case 6: 140 { 141 day1+=30; 142 break; 143 } 144 case 7: 145 { 146 day1+=31; 147 break; 148 } 149 case 8: 150 { 151 day1+=31; 152 break; 153 } 154 case 9: 155 { 156 day1+=30; 157 break; 158 } 159 case 10: 160 { 161 day1+=31; 162 break; 163 } 164 case 11: 165 { 166 day1+=30; 167 break; 168 } 169 case 12: 170 { 171 day1+=31; 172 break; 173 } 174 } 175 } 176 day1+=a.day; 177 if(((b.year%4==0&&b.year%100!=0)||(b.year%400==0))&&(b.month>2)) 178 day1-=1; 179 if(((a.year%4==0&&a.year%100!=0)||(a.year%400==0))&&(a.month>2)) 180 day1+=1; 181 } 182 else 183 { 184 for(int i=b.month;i<a.month;i++) 185 { 186 switch(i) 187 { 188 case 1: 189 { 190 day1+=31; 191 break; 192 } 193 case 2: 194 { 195 day1+=28; 196 break; 197 } 198 case 3: 199 { 200 day1+=31; 201 break; 202 } 203 case 4: 204 { 205 day1+=30; 206 break; 207 } 208 case 5: 209 { 210 day1+=31; 211 break; 212 } 213 case 6: 214 { 215 day1+=30; 216 break; 217 } 218 case 7: 219 { 220 day1+=31; 221 break; 222 } 223 case 8: 224 { 225 day1+=31; 226 break; 227 } 228 case 9: 229 { 230 day1+=30; 231 break; 232 } 233 case 10: 234 { 235 day1+=31; 236 break; 237 } 238 case 11: 239 { 240 day1+=30; 241 break; 242 } 243 case 12: 244 { 245 day1+=31; 246 break; 247 } 248 } 249 } 250 day1+=a.day; 251 day1-=b.day; 252 if(b.month==2&&b.day==29) 253 day1++; 254 if(((a.year%4==0&&a.year%100!=0)||(a.year%400==0))&&(b.month<=2&&b.day!=29&&a.month>2)) 255 { 256 day1++; 257 } 258 } 259 return day1; 260 } 261 262 int main() 263 { 264 Date y1,y2; 265 y1.get(); 266 y2.get(); 267 cout<<days(y1,y2); 268 return 0; 269 }