qt c++ 实现每一天调用qDebug()<<"day";考虑闰年平年和大小月,输入的参数为Qstring,范围为某年某月某日到某年某月某日,并计算总的天数
时间: 2024-03-26 11:34:07 浏览: 63
好的,我理解了你的问题。为了实现这个功能,我们可以编写一个函数来计算从起始日期到结束日期的天数,并在每天调用qDebug()<<"day"。下面是一个可能的实现:
```cpp
#include <QtCore>
int daysInMonth(int year, int month) {
if (month == 2) {
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
return 29;
} else {
return 28;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
} else {
return 31;
}
}
int daysBetween(QDate startDate, QDate endDate) {
int days = 0;
for (int year = startDate.year(); year <= endDate.year(); ++year) {
int startMonth = (year == startDate.year() ? startDate.month() : 1);
int endMonth = (year == endDate.year() ? endDate.month() : 12);
for (int month = startMonth; month <= endMonth; ++month) {
int startDay = (year == startDate.year() && month == startDate.month() ? startDate.day() : 1);
int endDay = (year == endDate.year() && month == endDate.month() ? endDate.day() : daysInMonth(year, month));
for (int day = startDay; day <= endDay; ++day) {
qDebug() << "day";
++days;
}
}
}
return days;
}
int main(int argc, char *argv[]) {
QCoreApplication app(argc, argv);
QDate startDate = QDate::fromString("2020-01-01", "yyyy-MM-dd");
QDate endDate = QDate::fromString("2020-01-31", "yyyy-MM-dd");
int totalDays = daysBetween(startDate, endDate);
qDebug() << "Total days:" << totalDays;
return app.exec();
}
```
这个程序中,我们使用了QDate类来表示日期,daysInMonth函数用于计算每个月的天数,daysBetween函数用于计算从起始日期到结束日期的天数并在每个日期上调用qDebug()<<"day"。在main函数中,我们可以设置起始日期和结束日期,并计算总天数。
阅读全文