C++ 練習 簡易日曆

龍威昊發表於2019-05-12

簡易日曆

程式碼

// Author : Weihao Long
// Created : 2018/03/20

#include <bits/stdc++.h>

using namespace std;
using namespace rel_ops;

int table[] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };

class Calendar {
public:
    Calendar(int y = 1, int m = 1, int d = 1) :year(y), month(m), day(d) {}
    Calendar(const Calendar& source) :year(source.year), month(source.month), day(source.day) {}
    void set_date(int y, int m, int d) { year = y; month = m, day = d; }
    Calendar& operator ++() {
        if (is_e_o_m()) {
            if (month == 12) { year += 1; month = 1; day = 1; }
            else { month += 1; day = 1; }
        }
        else { day += 1; }
        return *this;
    }
    Calendar operator ++(int) {
        Calendar temp = *this;
        if (is_e_o_m()) {
            if (month == 12) { year += 1; month = 1; day = 1; }
            else { month += 1; day = 1; }
        }
        else { day += 1; }
        return temp;
    }
    Calendar& operator --() {
        if (month == 1 && day == 1) { year -= 1; month = 12; day = 31; }
        else if (day == 1) {
            month -= 1; day = table[month];
            if (is_leap() && month == 2) day += 1;
        }
        else { day -= 1; }
        return *this;
    }
    Calendar operator --(int) {
        Calendar temp = *this;
        if (month == 1 && day == 1) { year -= 1; month = 12; day = 31; }
        else if (day == 1) {
            month -= 1; day = table[month];
            if (is_leap() && month == 2) day += 1;
        }
        else { day -= 1; }
        return temp;
    }
    Calendar& operator =(const Calendar& c) {
        year = c.year; month = c.month; day = c.day;
        return *this;
    }
    Calendar operator +=(Calendar&) = delete;
    Calendar& operator ()(const int& n) {
        for (int i = 0; i < n; ++i)
            ++(*this);
        return *this;
    }
    bool operator ==(const Calendar& c) const { return year == c.year && month == c.month && day == c.day; }
    bool operator <(const Calendar& c) const {
        if (year != c.year) return year < c.year;
        if (month != c.month) return month < c.month;
        return day < c.day;
    }
    friend istream& operator >>(istream& input, Calendar& c);
    friend ostream& operator <<(ostream& output, const Calendar& c);
    friend int operator -(const Calendar& c1, const Calendar& c2);
private:
    int year, month, day;
    int is_leap() { return year % 4 == 0 && year % 100 || year % 400 == 0; }
    int is_e_o_m() {
        if (is_leap() && month == 2)
            return 29 == day;
        return table[month] == day;
    }
    int total_days() const {
        int days_before_month[] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 };
        int years = year - 1;
        int days = years * 365 + years / 4 - years / 100 + years / 400 + days_before_month[month - 1] + day;
        if (year % 4 == 0 && year % 100 || year % 400 == 0 && month > 2)
            days += 1;
        return days;
    }
};

int operator -(const Calendar& c1, const Calendar& c2) { return c1.total_days() - c2.total_days(); }

istream& operator >>(istream& input, Calendar& c) {
    input >> c.year >> c.month >> c.day;
    return input;
}

ostream& operator <<(ostream& output, const Calendar& c) {
    output << c.year << `-` << c.month << `-` << c.day;
    return output;
}

class ArrayOfCalendar {
public:
    ArrayOfCalendar(int count = 0) {
        size = count;
        pcal = new Calendar[size];
    }
    ArrayOfCalendar(const ArrayOfCalendar& source) {
        size = source.size;
        pcal = new Calendar[size];
        for (int i = 0; i < size; ++i)
            pcal[i] = source.pcal[i];
    }
    ~ArrayOfCalendar() { delete[] pcal; }
    ArrayOfCalendar& operator =(const ArrayOfCalendar& c) {
        if (this == &c)
            return *this;
        delete[] pcal;
        pcal = new Calendar[c.size];
        for (int i = 0; i < size; ++i)
            pcal[i] = c.pcal[i];
        return *this;
    }
    Calendar& operator [](int index) { return pcal[index]; }
    friend istream& operator >>(istream& input, ArrayOfCalendar& c);
    friend ostream& operator <<(ostream& output, const ArrayOfCalendar& c);
private:
    Calendar* pcal;
    int size;
};

istream& operator >>(istream& input, ArrayOfCalendar& c) {
    for (int i = 0; i < c.size; ++i)
        input >> c.pcal[i];
    return input;
}

ostream& operator <<(ostream& output, const ArrayOfCalendar& c) {
    for (int i = 0; i < c.size; ++i)
        output << c.pcal[i] << endl;
    return output;
}

int main() {
    Calendar my1(2017, 2, 28), my2(2019, 1, 1);
    cout << "my1 : " << my1 << endl;
    cout << "my2 : " << my2 << endl;
    cout << "my1-my2 : " << my1 - my2 << endl;
    ++my1;
    cout << "++my1 : " << my1 << endl;
    my2--;
    cout << "my2-- : " << my2 << endl;
    if (my1 == my2)
        cout << "my1 == my2" << endl;
    if (my1 != my2)
        cout << "my1 != my2" << endl;
    if (my1 < my2)
        cout << "my1 < my2" << endl;
    if (my1 > my2)
        cout << "my1 > my2" << endl;
    my1(100);
    cout << "my1 : " << my1 << endl;
    int count;
    cout << "Input the count of calender : ";
    cin >> count;
    ArrayOfCalendar c1(count), c2(c1);
    cout << "Input the calenders : " << endl;
    cin >> c1;
    cout << "The " << count << " calendars are : " << endl;
    cout << c1;
    c2 = c1;
    cout << "(c2=c1) The " << count << " calendars are : " << endl;
    cout << c2;
    cout << "c2[0] : " << c2[0] << endl;
    return 0;
}

/**************************************************

my1 : 2017-2-28
my2 : 2019-1-1
my1-my2 : -672
++my1 : 2017-3-1
my2-- : 2018-12-31
my1 != my2
my1 < my2
my1 : 2017-6-9
Input the count of calender : 4
Input the calenders :
1111 1 1
2222 2 2
3333 3 3
4444 4 4
The 4 calendars are :
1111-1-1
2222-2-2
3333-3-3
4444-4-4
(c2=c1) The 4 calendars are :
1111-1-1
2222-2-2
3333-3-3
4444-4-4
c2[0] : 1111-1-1

Process returned 0 (0x0)   execution time : 0.220 s
Press any key to continue.

**************************************************/

相關文章