C++ Date类的设计

设计一个日期类,要求重载其输入输出运算符,方便输入输出(输入和输出的格式为1970.09.25),并要求实现日期加和减运算(即(1)一个日期减另一个日期返回值为这两个日期之间相隔的天数;(2)一个日期减一个整数8则返回比这个日期早8天的日期;(3)一个日期加一个整数8则返回比这个日期晚8天的日期),并能比较两个日期的先后顺序,在main函数中首先要求用户输入两个日期,然后首先输出第一个日期加100天和减100天是哪一天?再输出第一个日期与第二个日期之间相隔多少天?最后将两个日期按先后次序输出。(考虑闰年,考虑月份不同)

#include<iostream>
#include<algorithm>
#include <ctime>
using namespace std;

class Date
{
private:
    int year;
    int month;
    int day;

public:
    Date()
    {
        year = 0;
        month = 0;
        day = 0;
    }

    Date(int a,int b,int c)
    {
        year = a;
        month = b;
        day = c;
    }

    ~Date()
    {

    }

    int getYear()
    {
        return this->year;
    }

    void setYear(int year)
    {
        this->year = year;
    }

    int getMonth()
    {
        return this->month;
    }

    void setMonth(int month)
    {
        this->month = month;
    }

    int getDay(int day)
    {
        return this->day;
    }

    void setDay()
    {
        this->day =day;
    }
    bool IsLeapYear(int year)
    {
        if(year % 400 || (year % 4 && year % 100))
            return true;
        return false;
    }
    int YearsOfMonth(int year, int month)
    {
        int day;
        int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        day = days[month];
        if (month == 2)
            day += IsLeapYear(year);
        return day;
    }

    Date operator+(int a)
    {
        Date b;
        b.year = this->year;
        b.month = this->month;
        b.day = this->day + a;
        while(b.day > YearsOfMonth(b.year,b.month))
        {
            if(b.month == 12)
            {
                b.month = 1;
                b.year++;
            }
            else
            {
                b.month++;
            }
            b.day = b.day - YearsOfMonth(b.year,b.month);
        }
        return b;
    }

    Date operator-(int a)
    {
        Date b;
        b.year = this->year;
        b.month = this->month;
        b.day = this->day - a;
        while(b.day <= 0)
        {
            if(b.month == 1)
            {
                b.month = 12;
                b.year--;
            }
            else
            {
                b.month--;
            }
            b.day = b.day + YearsOfMonth(b.year,b.month);
        }
        return b;
    }

    int daysOfDate(Date d)//计算一共的天数
    {
        int days=d.day;
        for(int y=1; y<d.year; y++) //计算年
            days= days + 365+IsLeapYear(d.year);
        for(int m=1; m<d.month; m++) //计算月
            days= days + YearsOfMonth(d.year,d.month);
        return days;
    }

    int operator-(Date a)
    {
        Date b;
        b.year = this->year;
        b.month = this->month;
        b.day = this->day ;
        int days1=daysOfDate(b);
        int days2=daysOfDate(a);
        return days1 - days2;
    }

    bool operator>(Date a)
    {
        Date b;
        b.year = this->year;
        b.month = this->month;
        b.day = this->day ;
        int days1=daysOfDate(b);
        int days2=daysOfDate(a);
        if(days1 >= days2)
        {
            return true;
        }
        return false;
    }

    friend    ostream& operator<<(ostream& out, const Date &a)
    {
        out<<a.year<<"."<<a.month<<"."<<a.day;
        return out;
    }


    friend    istream& operator>>(istream& in, Date &a)
    {
        in >>a.year;
        cin.get();
        in>>a.month;
        cin.get();
        in>>a.day;
        return in;
    }
};

int main()
{
    Date a;
    Date b;
    cin>>a;
    cin>>b;
    cout<<a+100<<endl;
    cout<<a-100<<endl;
    cout<<a-b<<endl;
    if(a>b)
    {
        cout<<b<<endl;
        cout<<a<<endl;
    }
    else
    {
        cout<<a<<endl;
        cout<<b<<endl;
    }
}

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注