初学c++完成的第一个类--日期类

#include <iostream>
using namespace std;
class Date {

public:
    
    bool isleapyear(int year) { 
        //初学时必须得写的题目
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            return true;
        } else {
            return false;
        }
    }


    int Getmonthday(int year, int month) {
        //static 节省空间和时间
        static int arr[13] = {0, 31, 28, 31, 30, 31, 30, 31, 30, 31, 30};
        if (month == 2 && isleapyear(year)) {
            return arr[month] + 1;
        } else {
            return arr[month];
        }
    }

    Date (int year = 0, int month = 0, int day = 0) {
        // 加入闰年判断,是否是闰年
        if (year >= 0 && month > 0 && month < 12 && day > 0 && day < Getmonthday(year, month)) {
            _year = year;
            _month = month;
            _day = day;
        } else {
            cout << "input date error" << endl;
        }
    }
    Date& operator=(const Date& d) {
        if (this != &d) {
            _year = d._year;
            _month = d._month;
            _day = d._day;
        }

        return *this;
    }

    Date (const Date& d)
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }
    void printdate() {
        cout << _year << "-" << _month << "-" << _day << endl;
    }

    bool operator==(const Date& d) {
        return _year == d._year && _month == d._month 
        && _day == d._day;
    }
    
    
    // 返回右值,所以不能直接用this的地址,要新建一个Date
    // 注意这里有两个Date类,如果是_date的话特指this,不要搞混了
    Date operator+(int day)
    {
        Date ret(*this);
        //ret += day;
        if (day < 0) {
            ret._day = ret._day + (-day);
        } else {
            ret._day = ret._day + day;
        }
        while (ret._day > Getmonthday(ret._year, ret._month)) {
            ret._day = ret._day - Getmonthday(ret._year, ret._month);
            ret._month++;
            if (ret._month == 13) {
                ret._month = 1;
                ret._year++;
            }
        }
        return ret;
    }
    //返回左值,只有一个Date类,所以放心使用_day, _month, _year.
    Date& operator+=(int day) {
        if (day < 0) {
            _day = _day + (-day);
        } else {
            _day = _day + day;
        }
        while (_day > Getmonthday(_year, _month)) {
            _day = _day - Getmonthday(_year, _month);
            _month++;
            if (_month == 13) {
                _month = 1;
                _year++;
            }
        }
        return *this;
    }

    //同加法一样
    Date operator-(int day)
	{
		Date ret(*this);
        // ret -= day
        if (day < 0) {
            ret._day = ret._day + day;
        } else {
            ret._day = ret._day - day;
        }
        while (ret._day < 0) {
            if (ret._month == 1) {
                ret._year--;
                ret._month = 12;
            } else {
                ret._month--;
            }
            ret._day += Getmonthday(ret._year, ret._month);
        }

        return ret;
	}
  
  // 日期类减日期类很常见,所以这里要重载一个,返回值int
  // 创建 Date min 和 max, 判断这两个哪个大哪个小, 再定义一个自增的数, 用差值计算即可
    int operator-(Date d1)
	{
		Date min(d1);
        Date max(*this);
        int flag = 1;
        if (*this < d1) {
            flag = -1;
            max = d1;
            min = *this;
        }
        int count = 0;
        while (1) {
            if ((min + count) == max) {
                break;
            }
            count++;
        }
        return flag * count;
	}


	// 日期-=天数
  //同加法一样
	Date& operator-=(int day)
	{
		if (day < 0)
		{
			_day = _day- (-day);
		}
		else {
            _day = _day - day;
        }
		while (_day <= 0)
		{
			if (_month == 1)
			{
				_month = 12;
				_year--;
			} else {
                _month--;
            }
			_day += Getmonthday(_year, _month);
		}
		return *this;
	}
    // 后置++

	Date operator++(int)
	{
		Date ret(*this);
		*this += 1;
		return ret;
	}



	// 后置--

	Date operator--(int)
	{
		*this -= 1;
		return *this;
	}



	// 前置--

	Date& operator--()
	{
		Date ret(*this);
		ret -= 1;
		return *this;
	}



  // 判断> < ==, 只需实现 其中两个 < , == 或者 > , == 即可,详情如下
	// >运算符重载

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

	}





	// >=运算符重载

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



	// <运算符重载

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



	// <=运算符重载

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



	// !=运算符重载

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


private:
    int _year;
    int _month;
    int _day;
};


// 测试代码, 只是小测了一下,应该没太大问题
void test1() {
    Date d1(2022, 10, 16);
    d1 += 10000;
    d1.printdate();
    d1 -= 10000;
    d1.printdate();

    Date d2(d1);
    d2.printdate();
    d1 = d2 + 10000;
    d2.printdate();
    d1.printdate();
    int count = d1 - d2;
    cout << count << endl;
}


int main() {
    
    test1();

    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值