天气与日历 切换到窄版

 找回密码
 立即注册
中国膜结构网
十大进口膜材评选 十大国产膜材评选 十大膜结构设计评选 十大膜结构公司评选
查看: 6|回复: 0

date日期类实现-C++

[复制链接]
  • TA的每日心情
    开心
    昨天 15:23
  • 签到天数: 69 天

    [LV.6]常住居民II

    410

    主题

    167

    回帖

    2704

    积分

    管理员

    积分
    2704
    发表于 2024-6-22 09:46:18 | 显示全部楼层 |阅读模式
    #include<iostream>
    #include<assert.h>
    #include<vector>
    using namespace std;


    class Date
    {
    public:
            //构造函数
            Date(int year = 1900, int month = 1, int day = 1){
                    this->_year = year;
                    this->_month = month;
                    this->_day = day;
            }

            //拷贝构造
            Date(const Date& d){
                    this->_year = d._year;
                    this->_month = d._month;
                    this->_day = d._day;
            }

            //赋值运算符重载
            Date& operator=(const Date& d){
                    if (this != &d){
                            this->_year = d._year;
                            this->_month = d._month;
                            this->_day = d._day;
                    }
                    return *this;
            }

            //+运算符重载
            Date operator+(int days){
                    Date tmp(*this);
                    tmp._day += days;
                    while (tmp._day > GetMonthDay(tmp._year, tmp._day)){
                            tmp._day -= GetMonthDay(tmp._year, tmp._day);
                            if (tmp._month == 12){
                                    tmp._year++;
                                    tmp._month = 1;
                            }
                            else{
                                    tmp._month++;
                            }
                    }
                    return tmp;
            }

            //-运算符重载
            Date operator-(int days){
                    Date tmp(*this);
                    if (days < 0){
                            return tmp + (-days);
                    }
                    tmp._day -= days;
                    while (tmp._day < 1){
                            if (tmp._month == 1){
                                    tmp._year--;
                                    tmp._month = 12;
                            }
                            else{
                                    tmp._month--;
                            }
                            tmp._day += GetMonthDay(tmp._year, tmp._month);
                    }
                    return tmp;
            }

            //两个日期相减
            int operator-(const Date& d){
                    Date Min(d);
                    Date Max(*this);
                    int flag = 1;
                    if (*this < d){
                            Max = d;
                            Min = *this;
                            flag = -1;
                    }
                    int day = 0;
                    while (Max != Min){
                            ++Min;
                            ++day;
                    }
                    return day*flag;
            }

            //++运算符重载,前置++
            Date& operator++(){
                    this->_day += 1;
                    while (this->_day > GetMonthDay(this->_year, this->_month)){
                            this->_day -= GetMonthDay(this->_year, this->_month);
                            if (this->_month == 12){
                                    this->_year++;
                                    this->_month = 1;
                            }
                            else{
                                    this->_month++;
                            }
                    }
                    return *this;
            }

            //后置++,返回创建的
            Date operator++(int){
                    Date tmp(*this);
                    this->_day += 1;
                    while (this->_day > GetMonthDay(this->_year, this->_month)){
                            this->_day -= GetMonthDay(this->_year, this->_month);
                            if (this->_month == 12){
                                    this->_year++;
                                    this->_month = 1;
                            }
                            else{
                                    this->_month++;
                            }
                    }
                    return tmp;
            }

            //前置--,--之后返回
            Date& operator--(){
                    this->_day--;
                    while (this->_day < 1){
                            if (this->_month == 1){
                                    this->_year--;
                                    this->_month = 12;
                            }
                            else{
                                    this->_month--;
                            }
                            this->_day += GetMonthDay(this->_year, this->_month);
                    }
                    return *this;
            }
           
            //后置--,返回原来的
            Date operator--(int){
                    Date tmp(*this);
                    this->_day--;
                    while (this->_day < 1){
                            if (this->_month == 1){
                                    this->_year--;
                                    this->_month = 12;
                            }
                            else{
                                    this->_month--;
                            }
                            this->_day += GetMonthDay(this->_year, this->_month);
                    }
                    return tmp;
            }

            bool operator>(const Date& d)const{
                    return (_year >= d._year) || (_year >= d._year &&_month >= d._month) || _year >= d._year &&_month >= d._month &&_day>d._day;
            }

            bool operator>=(const Date& d)const{
                    return (*this > d) || (*this == d);
            }

            bool operator<(const Date& d)const{
                    return (_year <= d._year) || (_year <= d._year &&_month <= d._month) || _year <= d._year &&_month <= d._month &&_day<d._day;
            }

            bool operator<=(const Date& d)const{
                    return (*this < d) || (*this == d);
            }

            bool operator==(const Date& d)const{
                    return (this->_day == d._day && this->_month == d._month && this->_year == d._year);
            }

            bool operator!=(const Date& d)const {
                    return !(*this == d);
            }

            //判断闰年
            bool IsLeapYear(int year){
                    assert(year >= 1900);
                    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
                            return true;
                    }
                    return false;
            }

            //获得月的天数
            int GetMonthDay(int year, int month){
                    assert(month>0 && month <= 12);
                    int day;
                    vector<int> monthday = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
                    if (IsLeapYear(year)){
                            monthday[1] = 29;
                    }
                    return monthday[month - 1];
            }


            //查看日期是否合法
            bool IsInvalid(){
                    if (this->_year>1900 && (this->_month >= 1 && this->_month <= 12) && this->_day <= GetMonthDay(this->_year, this->_month)){
                            return true;
                    }
                    return false;
            }
    private:
            int _year;
            int _month;
            int _day;
    };

    int main()
    {
            Date d(2018, 3, 30);
            Date d1(2018, 9, 1);
            Date d4;
            Date m(d);//拷贝函数

            system("pause");
    }

     

     

     

     

    date日期类实现-C++
    中国膜结构网打造全中国最好的膜结构综合平台 ,统一协调膜结构设计,膜结构施工,膜材采购,膜材定制,膜结构预算全方位服务。 中国空间膜结构协会合作单位。
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|中国膜结构网|中国膜结构协会|进口膜材|国产膜材|ETFE|PVDF|PTFE|设计|施工|安装|车棚|看台|污水池|

    GMT+8, 2024-7-1 05:32 , Processed in 0.057933 second(s), 22 queries .

    Powered by Discuz! X3.5

    © 2001-2024 Discuz! Team.

    快速回复 返回顶部 返回列表