#include <iostream> #include <string> #include <assert.h> using namespace std; //在此处补充Date类的定义 class Date { private: int year, month, day; static const int monthDays[13]; public: Date(int y = 1, int m = 1, int d = 1) { year = y; month = m; day = d; } Date(const Date& date) { year = date.year; month = date.month; day = date.day; } Date& operator=(const Date& date) { year = date.year; month = date.month; day = date.day; return *this; } bool isLeap() const { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } int monthDaysNum() const { if (month == 2 && isLeap()) { return 29; } return monthDays[month]; } void addOneDay() { day++; if (day > monthDaysNum()) { day = 1; month++; if (month > 12) { month = 1; year++; } } } void minusOneDay() { day--; if (day == 0) { month--; if (month == 0) { month = 12; year--; } day = monthDaysNum(); } } string toText() const { string res = ""; res += to_string(year) + "-"; if (month < 10) { res += "0"; } res += to_string(month) + "-"; if (day < 10) { res +=“0” ; } res += to_string(day); return res; } Date operator+(int n) const { Date res = *this; while (n--) { res.addOneDay(); } return res; } Date operator-(int n) const { Date res = *this; while (n--) { res.minusOneDay(); } return res; } }; const int Date::monthDays[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; int main() { int y, m, d; cin >> y >> m >> d; Date d1(y,m,d); int n; cin >> n; cout << d1.toText() << " + " << n << " = " << (d1 + n).toText() << endl; cout << d1.toText() << " - " << n << " = " << (d1 - n).toText() << endl; return 0; }}纠正这个代码使得云行不出现错误输出: 2022-08-31 + 2 = 2022-09-02 2022-08-31 - 2 = 2022-08-29 期望输出: 2022-8-31 + 2 = 2022-9-2 2022-8-31 - 2 = 2022-8-29
时间: 2024-01-07 15:03:36 浏览: 212
使用include <iostream>时一定要加using namespace std
纠正后的代码如下:
```c++
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
class Date {
private:
int year, month, day;
static const int monthDays[13];
public:
Date(int y = 1, int m = 1, int d = 1) {
year = y;
month = m;
day = d;
}
Date(const Date& date) {
year = date.year;
month = date.month;
day = date.day;
}
Date& operator=(const Date& date) {
year = date.year;
month = date.month;
day = date.day;
return *this;
}
bool isLeap() const {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int monthDaysNum() const {
if (month == 2 && isLeap()) {
return 29;
}
return monthDays[month];
}
void addOneDay() {
day++;
if (day > monthDaysNum()) {
day = 1;
month++;
if (month > 12) {
month = 1;
year++;
}
}
}
void minusOneDay() {
day--;
if (day == 0) {
month--;
if (month == 0) {
month = 12;
year--;
}
day = monthDaysNum();
}
}
string toText() const {
string res = "";
res += to_string(year) + "-";
if (month < 10) {
res += "0";
}
res += to_string(month) + "-";
if (day < 10) {
res += "0";
}
res += to_string(day);
return res;
}
Date operator+(int n) const {
Date res = *this;
while (n--) {
res.addOneDay();
}
return res;
}
Date operator-(int n) const {
Date res = *this;
while (n--) {
res.minusOneDay();
}
return res;
}
};
const int Date::monthDays[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int main() {
int y, m, d;
cin >> y >> m >> d;
Date d1(y,m,d);
int n;
cin >> n;
cout << d1.toText() << " + " << n << " = " << (d1 + n).toText() << endl;
cout << d1.toText() << " - " << n << " = " << (d1 - n).toText() << endl;
return 0;
}
```
主要是修改了 `toText()` 函数中输出日期月份和日期天数不足两位时的处理方式,将原本的双引号修改为单引号。
阅读全文