c++日期类

来源:互联网 发布:淘宝实名认证小号购买 编辑:程序博客网 时间:2024/06/09 17:28

下面的日期类主要实现的功能有
1.日期类的四大默认成员函数:构造函数,拷贝构造函数,赋值操作符重载,析构函数
2.输入操作符重载,输出操作符重载
3.日期的比较
4.日期+天数
5.日期-天数
6.日期-日期

#define _CRT_SECURE_NO_WARNINGS#include<stdlib.h>#include<stdio.h>#include<iostream>using namespace std;class Date{    friend ostream& operator<<(ostream &io, const Date& d);    friend istream& operator>>(istream &is, Date& d);public:    //4个默认的成员函数     //构造、拷贝构造、赋值语句重载、析构函数     Date(int year, int month, int day);    Date(const Date& d);    ~Date();    Date& operator=(const Date& d);public:    //日期比较     bool operator==(const Date& d);    bool operator!=(const Date& d);    bool operator>(const Date& d);    bool operator>=(const Date& d);    bool operator<(const Date& d);    bool operator<=(const Date& d);public:    Date& operator++();    Date operator++(int);    Date& operator--();    Date operator--(int);    //日期+天数     Date& operator+(int day);    //日期-天数     Date& operator-(int day);    //日期-日期     int operator-(Date& d);public:    bool isLeapYear(const Date&d)    {        if ((d._year % 4 == 0) || (d._year % 400 == 0 && d._year % 100 == 0))        {            return true;        }        else        {            return false;        }    }public:    int GetMonthDay(const Date& d);  //得到本月的天数    Date GetNextMonthDay(Date& d);  //得到下一个月的天数    Date GetUpMonthDay(Date& d);    //得到上一个月的天数private:    int _year;//年     int _month;//月     int _day;//日 };Date Date::GetNextMonthDay(Date& d){    if (d._month + 1 == 13)    {        d._month = 1;    }    else    {        d._month += 1;    }    return d;}Date Date::GetUpMonthDay(Date& d){    if (d._month - 1 == 0)    {        d._month = 12;    }    else    {        d._month -= 1;    }    return d;}int Date::GetMonthDay(const Date& d){    switch (d._month)    {    case 1:        return 31;        break;    case 2:    {              if (isLeapYear(d))              {                  return 29;              }              else              {                  return 28;              }    }        break;    case 3:        return 31;        break;    case 4:        return 30;        break;    case 5:        return 31;        break;    case 6:        return 30;        break;    case 7:        return 31;        break;    case 8:        return 31;        break;    case 9:        return 30;        break;    case 10:        return 31;        break;    case 11:        return 30;        break;    case 12:        return 31;        break;    default:        break;    }}ostream &operator<<(ostream &io, const Date& d){    io << d._year << "-" << d._month << "-" << d._day;    return io;}istream &operator>>(istream& is, Date&d){    is >> d._year >> d._month >> d._day;    return is;}Date::Date(int year,int month,int day):_year(year), _month(month), _day(day){    //cout << "Date(int year,int month,int day)" << endl;}Date::Date(const Date& d){    this->_year = d._year;    this->_month = d._month;    this->_day = d._day;    //cout << "Date(const Date& d)" << endl;}Date::~Date(){    //cout << "~Date()" << endl;}Date& Date::operator=(const Date& d){    if (*this != d)    {        this->_year = d._year;        this->_month = d._month;        this->_day = d._day;    }    //cout << "operator=(const Date& d)" << endl;    return *this;}bool Date::operator==(const Date& d){    if ((this->_year == d._year) && (this->_month == d._month) && (this->_day == d._day))    {        return true;    }    return false;}bool Date::operator!=(const Date& d){    if (*this == d)    {        return false;    }    return true;}bool Date::operator>(const Date& d){    if (this->_year == d._year)    {        if (this->_month == d._month)        {            if (this->_day == d._day)            {                return false;            }            else if (this->_day > d._day)            {                return true;            }            else            {                return false;            }        }        else if (this->_month > d._month)        {            return true;        }        else        {            return false;        }    }    else if (this->_year > d._year)    {        return true;    }    else    {        return false;    }}bool Date::operator>=(const Date& d){    if ((*this == d) || (*this>d))    {        return true;    }    else    {        return false;    }}bool Date::operator<(const Date& d){    if (*this==d)    {        return false;    }    else if (*this > d)    {        return false;    }    else    {        return true;    }}bool Date::operator<=(const Date& d){    if ((*this < d) || (*this == d))    {        return true;    }    else    {        return false;    }}Date& Date::operator++(){    int ret=GetMonthDay(*this);    if (this->_day + 1 > ret)    {        this->_day = 1;        this->_month += 1;        if (this->_month > 12)        {            this->_month = 1;            this->_year += 1;        }        return *this;    }    else    {        this->_day += 1;        return *this;    }}Date Date::operator++(int){    Date tmp(*this);    int ret = GetMonthDay(*this);    if (this->_day + 1 > ret)    {        this->_day = 1;        if ((this->_month += 1) > 12)        {            this->_month = 1;            this->_year += 1;        }    }    else    {        this->_day += 1;    }    return tmp;}Date& Date::operator--(){    int ret = GetMonthDay(*this);    if ((this->_month == 1) && (this->_day == 1))  //1月1号    {        this->_day = 31;        this->_month = 12;        this->_year -= 1;    }    else if (this->_day == 1)           //除12月的某个月的1号    {        Date tmp = GetUpMonthDay(*this);        this->_day = GetMonthDay(tmp);    }    else    {        this->_day -= 1;    }    return *this;}Date Date::operator--(int)         //不对{    Date tmp(*this);    --(*this);    return tmp;}//日期+天数 Date& Date::operator+(int day){    int i;    for (i = day; i > 0; i--)    {        ++(*this);    }    return *this;}//日期-天数 Date& Date::operator-(int day){    int i;    for (i = day; i > 0; i--)    {        --(*this);    }    return *this;}// //日期-日期 int Date::operator-(Date& d){    int count = 0;    while (*this != d)    {        count++;        if (*this > d)        {            d++;        }        else        {            (*this)++;        }    }    return count;}//测试是个默认成员函数的重载void test1(){    Date d1(2000, 4, 5);    Date d2(1994, 10, 24);    Date d3=d1;    d1 = d2;    cout << d1 << endl;    cout << d2 << endl;    cout << d3 << endl;    cout << d1.isLeapYear(d1) << endl;}//比较两个日期的是否相等void test2(){    Date d1(1994, 4, 5);    Date d2(1994, 10, 24);    Date d3(1994, 4, 5);    if (d1 != d2)    {        cout << "1" << endl;    }    else    {        cout << "0" << endl;    }}//比较日期的大小void test3(){    Date d1(1994, 4, 5);    Date d2(1994, 4, 6);    Date d3(1995, 4, 6);    Date d4(1995, 5, 6);    if (d2 < d1)    {        cout << "1" << endl;    }    else    {        cout << "0" << endl;    }}//时间自增自减void test4(){    Date tmp(0, 0, 0);    Date d1(2001, 1, 2);    tmp=++d1;    //++d1;    //tmp=d1++;       //这块有错误    //--d1;    //Date tmp=d1--;    cout << d1 << endl;    cout << tmp << endl;}void test5(){    Date d1(2017, 7, 10);    Date d2(2016, 6, 3);    int ret = d2 - d1;    cout << ret << endl;//  d1 - 95;//  cout << d1 << endl;//  d1.GetUpMonthDay(d1);//  d1.GetNextMonthDay(d1);//  cout << d1 << endl;}int main(){    //test1();    //test2();    //test3();    //test4();    test5();    return 0;}