题解 | #日期类# 类+运算符重载
日期类
https://www.nowcoder.com/practice/130aa2d7d1f5436b920229dca253893b
#include <iostream>
using namespace std;
int m[13] = {0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
class Date {
private:
int year;
int month;
int day;
public:
void input() {
cin >> year >> month >> day;
}
void display() {
cout << year << "-";
if (month < 10) cout << 0;
cout << month << "-";
if (day < 10) cout << 0;
cout << day << endl;
}
void operator ++() {
this->day++;
if (this->day > m[this->month]) {
this->day -= m[this->month];
this->month++;
if (this->month > 12) {
this->month -= 12;
this->year++;
}
}
}
};
int main() {
int n;
cin >> n;
Date d[n];
for (int i = 0; i < n; i++) {
d[i].input();
++d[i];
d[i].display();
}
}
美的集团公司福利 781人发布
