C++初阶Day23

日期类的实现

#include<iostream>
#include<assert.h>
#include<vld.h>
#include<time.h>
#include<windows.h>
using namespace std;

//日期类
class Date
{
	friend ostream& operator<<(ostream& out, const Date& dt);
public:
	Date() : m_year(0), m_month(1), m_day(1)//默认构造函数
	{}

	Date(int year, int month, int day) :m_year(year), m_month(month), m_day(day)//重载构造函数
	{}

	Date(const Date& d)//拷贝构造,参数只能引用                        
	{
		m_year = d.m_year;
		m_month = d.m_month;
		m_day = d.m_day;
	}

	Date& operator=(const Date& d)//赋值函数,操作符“=”重载
	{
		if (this != &d)
		{
			m_year = d.m_year;
			m_month = d.m_month;
			m_day = d.m_day;
		}
		return *this;
	}

	~Date()
	{}

public:
	bool operator>(const Date& d)
	{
		return (m_year > d.m_year ||
			(m_year == d.m_year && m_month > d.m_month) ||
			(m_year == d.m_year && m_month == d.m_month && m_day > d.m_day));
	}

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

	bool operator < (const Date& d)
	{
		return (m_year < d.m_year ||
			(m_year == d.m_year && m_month < d.m_month) ||
			(m_year == d.m_year && m_month == d.m_month && m_day < d.m_day));
	}

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

	bool operator==(const Date& d)
	{
		return (m_day == d.m_day && m_month == d.m_month && m_year == d.m_year);
	}

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

public:
	int GetDayByYM(int year, int month)//获取某年某个月的天数
	{
		//0   1   2   3   4   5   6   7   8   9   10  11  12
		int days[] = { 29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if (month == 2)
		{
			if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
				month = 0;
		}
		return  days[month];
	}
	Date operator+(int n)
	{
		Date tmp = *this;
		int days = GetDayByYM(tmp.m_year, tmp.m_month);//当前这个月的天数
		int day = n + tmp.m_day;//天数:n+当月天数
while (day > days)//天数不能超过该月的天数
		{
			tmp.m_month++;
			if (tmp.m_month > 12)
			{
				tmp.m_year++;//超过12个月,年份+1
				tmp.m_month = 1;//注意:要重新设置月份,从1月份开始
			}

			n -= days;//每增加一个月,n要减去上个月的天数
			days = GetDayByYM(tmp.m_year, tmp.m_month);//重新获取当月的天数
			day = n + tmp.m_day;//重新计算天数
		}

		tmp.m_day += n;
		return tmp;
	}public:
		Date& operator++()
		{
			*this = *this + 1;//调用Date operator+(int n);
			return *this;
		}
		Date operator++(int)
		{
			Date tmp = *this;
			++* this;//调用Date& operator++();
			return tmp;
		}
public:
	// 日期-日期 返回天数
	int operator-(const Date& d);
public:
	int GetWeek(int year, int month, int day);
public:
	void PrintMonthCalc(int year, int month);
public:
	void  GetCurDateTime()
	{
		time_t te;
		while (1)
		{
			time(&te);

			struct tm* ptm = localtime(&te);
			m_year = ptm->tm_year + 1900;
			m_month = ptm->tm_mon + 1;
			m_day = ptm->tm_mday;

			printf("%d年%d月%d日   %d:%d:%d", m_year, m_month, m_day, ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
			Sleep(1000);
			system("cls");
		}
	}
private:
	int m_year;
	int m_month;
	int m_day;
};

ostream& operator<<(ostream& out, const Date& dt)//日期类的输出符重载
{
	out << dt.m_year << "年" << dt.m_month << "月" << dt.m_day << "日";
	return out;
}

void main()
{
	Date dt1(2022, 5, 22);
	cout << dt1 << endl;

	//Date dt = dt1 + 100;
	dt1++;
	cout << dt1 << endl;
}


/*
void main()
{
	Date dt(2022, 5, 22);
	cout<<dt<<endl;

	Date dt1;
	dt1.GetCurDateTime();
}
*/
		
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值