C++【类:日期类】

实现日期类的一些功能:

<span style="font-size:18px;">#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)//构造函数
		:_year(year)
		, _month(month)
		, _day(day)
	{    //参数检测
		if ((year >= 1900)
			&& (month >= 1 && month <= 12)
			&& (day >= 1 && day <= _GetMonthDay(year, month)))
		{
			_year = year;
			_month = month;
			_day = day;
		}
		else
		{
			cout << "非法日期" << endl;
			_year = 1900;
			_month = 1;
			_day = 1;
		}
		//  cout << "Date(size_t year , size_t month , size_t day)" << endl;
	}
	//构造函数		
	Date(const Date& d)   //拷贝构造
	{
		 _year = d._year;
		 _month = d._month;
		 _day = d._day;
	}
	~Date()        //析构函数
	{
		cout << "~Date()" << endl;
	}
	操作符重载

	Date& 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);  // <
	
	bool operator<=(const Date& d);   //<=
	
	Date operator+(int days);  // 加天数
	
	Date& operator+=(int days);//   +=

	Date operator-(int days);  //减天数
	
	int operator-(const Date& d);  // 两个日期相隔天数

	Date& operator-=(int days);  //-=

	Date& operator++();  //自加 天数  //前置++

	Date operator++(int); //后置++

	Date& operator--(); //前置--
	
	Date operator--(int);  //后置
	
	bool IsLeap();//判断闰年?

	int _GetMonthDay(int year,int month);  //一个月有多少天

	friend ostream& operator<<(ostream& os, const Date& d);    //输出

	friend istream& operator>>(istream& is,  Date& d);         //输入

private:
	int _year;
	int _month;
	int _day;

};

/***********************************     类外     *************************************/


bool Date:: IsLeap()
{
	if ((_year % 4 == 0 && _year % 100 != 0) || _year % 400 == 0)
	{
		return true;
	}
	return false;
}
int Date:: _GetMonthDay(int year,int month)  //一个月有多少天
{
	assert(month > 0 && month <= 12);
	int arr_monthday[] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	int day;
	if ((!IsLeap()) && month == 2)
	{
		_day = arr_monthday[month] - 1;
	}
	day = arr_monthday[month];
	return day;
}

Date& Date:: operator=(const Date& d)  //赋值
{
	if (this == &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}

bool Date:: operator==(const Date& d)
{
	return (_year == d._year) && (_month == d._month) && (_day == d._day);
}
bool Date:: operator!=(const Date& d)
{
	return(!(*this == d));
}

bool Date:: operator>(const Date& d)
{
	return (_year >d._year) //年
		|| (_year == d._year) && (_month > d._month)// 年-月
		|| (_year == d._year) && (_month == d._month) && (_day > d._day);
	//年-月-日
}
bool Date:: operator>=(const Date& d)
{
	return (*this == d) || (*this > d);
}

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

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

Date& Date::operator++()  //自加 天数  //前置++
{
	//Date tmp(*this);
	
	_day++;
	if (_day > _GetMonthDay(_year, _month))
	{
		_day -= _GetMonthDay(_year, _month);
		_month++;
		if (_month > 12)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}

Date Date::operator++(int)  //后置++
{
	Date temp(*this) ;
	_day++;
	if (_day > _GetMonthDay(_year,_month))
	{
		_day = _GetMonthDay(_year, _month)-_day ;
		_month++;
		if (_month > 12)
		{
			_year++;
			_month = 1;
		}
	}
	return temp;
}

Date& Date::operator--()  //前置--
{
	_day--;
	if (_day < 1)
	{
		_month--;
		if (_month < 1)
		{
			_year--;
			_month = 12;
		}
		_day += _GetMonthDay(_year, _month) ;
	}
	return *this;
}

Date Date::operator--(int)        //后置--
{
	Date temp(*this);
	_day--;
	if (_day < 1)
	{
		_month--;
		if (_month < 1)
		{
			_year--;
			_month = 12;
		}
		_day += _GetMonthDay(_year, _month);	
	}
	return temp;
}

Date Date::operator+(int days)         /*****************加天数*****************/
{
	Date tmp(*this);
	if (days < 0)
	{
		return  tmp - (-days);
	}
	tmp._day += days;
	while (tmp._day> _GetMonthDay(tmp._year, tmp._month))
	{
		tmp._day -= _GetMonthDay(tmp._year, tmp._month);
		if (tmp._month == 12)
		{
			tmp._year++;
			tmp._month = 1;
		}
		else
		{
			tmp._month++;
		}
	}
	return tmp;
}

Date& Date:: operator +=(int days)//   +=
{
	*this = *this + days;
	return *this;   //返回*this,所以用 引用做且返回值
}

Date Date::operator-(int days)    /***************减天数******************/
{
	Date tmp(*this);
	if (days < 0)
	{
		return  tmp + (-days);
	}
	tmp._day -= days;
	while (tmp._day <= 0)  //注意没有1月0号。。。
	{  
		if (tmp._month==1)
		{
			tmp._year--;
			tmp._month = 12;
		}//校正月数
		else
		{
			--tmp._month;
		}
		tmp._day += _GetMonthDay(tmp._year, tmp._month);//上一个月的天数!!!
	}
	return tmp;
}

Date& Date:: operator-=(int days)  //-=
{
	*this = *this - days;
	return *this;
}


int Date::operator-(const Date& d)  //两个日期相隔天数
{//思路:比较两个日期的大小,找出最大的(最小的)
	//通过最大的--(最小的++),days++
	//(最大的==最小的)就可以了
	Date Max(*this);
	Date Min(d);
	int flag = 1;
	if ( *this < d)
	{
		//使用库函数    std::swap(Max,Min);
		Max = d;
		Min= *this;
		flag = -1;
	}
	int days = 0;
	while (Max != Min)
	{
		--Max;
		++days;
	}
	return days*flag;
}

ostream& operator<<(ostream& os, const Date& d)
{
	os << d._year << "-" << d._month << "-" << d._day << endl;
	//os ;
	//os ;
	return os;
}

istream& operator>>(istream& is,  Date& d)
{
	cout << "输入日期:";
	is >> d._year;
	is >> d._month;
	is>> d._day;

	return is;
}</span>


/********************************* 测试 ***************************************/


int main()
{
	Date d1(2016, 5, 31);
	Date  d2(2016,12,1);
	cout << d1;
	int ret= d2-d1 ;
	Date  d3 = d2 + 1;
	Date d4(d2);
	Date d5 = d1 - 1;
	Date d6 = d1++;
	Date d7;
	cin >> d7;
	cout << d7;
	d1 += 1;
	return 0;
}




  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ 中,可以通过定义一个来实现日期类的功能。下面是一个简单的日期类的定义示例: ```c++ class Date { private: int year, month, day; public: // 构造函数 Date(int y, int m, int d) { year = y; month = m; day = d; } // 获取年份 int getYear() const { return year; } // 获取月份 int getMonth() const { return month; } // 获取日期 int getDay() const { return day; } // 设置日期 void setDate(int y, int m, int d) { year = y; month = m; day = d; } // 判断是否为闰年 bool isLeapYear() const { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) return true; else return false; } // 计算该月份的天数 int getDaysInMonth() const { if (month == 2) { if (isLeapYear()) return 29; else return 28; } else if (month == 4 || month == 6 || month == 9 || month == 11) { return 30; } else { return 31; } } // 计算该日期是星期几 int getWeekday() const { int y = year; int m = month; if (m == 1 || m == 2) { m += 12; y--; } int d = day; int w = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7; return w; } // 输出日期 void print() const { cout << year << "-" << month << "-" << day; } }; ``` 这个日期类包含了以下功能: - 构造函数:用于初始化日期的年份、月份和日期。 - 获取年份、月份和日期:用于获取日期的年份、月份和日期。 - 设置日期:用于修改日期的年份、月份和日期。 - 判断是否为闰年:用于判断该日期所在的年份是否为闰年。 - 计算该月份的天数:用于计算该日期所在月份的天数。 - 计算该日期是星期几:用于计算该日期是星期几。 - 输出日期:用于将日期输出到屏幕上。 可以根据实际需求对日期类进行扩展,例如添加日期加减、日期比较等功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值