C++两个日期之间,分出日期的单数和双数
时间: 2024-05-10 10:18:20 浏览: 9
以下是一个简单的C++程序,可以计算两个日期之间的天数,并将其分为单数和双数。
```c++
#include <iostream>
using namespace std;
int isLeap(int year) {
return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}
int getDays(int d, int m, int y) {
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeap(y)) days[1] = 29;
int total = 0;
for (int i = 0; i < m - 1; i++) {
total += days[i];
}
total += d;
return total;
}
int main() {
int d1, m1, y1, d2, m2, y2;
cout << "Enter first date (DD MM YYYY): ";
cin >> d1 >> m1 >> y1;
cout << "Enter second date (DD MM YYYY): ";
cin >> d2 >> m2 >> y2;
int days1 = getDays(d1, m1, y1);
int days2 = getDays(d2, m2, y2);
int diff = days2 - days1;
cout << "Days between dates: " << diff << endl;
if(diff%2==0){
cout<<"The number of days between the two dates is even."<<endl;
}
else{
cout<<"The number of days between the two dates is odd."<<endl;
}
return 0;
}
```
在程序中,`isLeap`函数用于检查给定年份是否为闰年。`getDays`函数计算给定日期的天数。最后,在主函数中,计算两个日期之间的天数,并检查它是否为偶数或奇数。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)