题解 | #日期类# 类+运算符重载

日期类

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();
    }

}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-16 14:00
机械打工仔:来挂自己了,经典巨婴从校园投入职场
点赞 评论 收藏
分享
点赞 评论 收藏
分享
06-28 22:48
已编辑
广东金融学院 Java
小浪_Coding:学院本+这俩项目不是buff叠满了嘛
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务