【C++】日期类Date的定义

一、类的描述

      类描述了具有共同特征的一组对象,这组对象的属性和行为相同,只不过具体对象的属性值等有所区别。C++中类的定义一般分为类的说明部分和类的实现部分。其中类说明的格式如下:

class<ClassName>

{

private:

私有数据和函数

public:

受保护数据和函数

};   

  其中,class是关键字。<ClassName>是用户自定义的C++标识符,Visual C++中类名的风格是所有类的名字都以大写字母C开头,表示这个类的名字,例如CBOOKCStudent等。被花括号括起来的部分称作类体。注意,类说明是以分号作为结束的。

二、类Date的定义

#include<iostream>
#include<Windows.h>
using namespace std;
//定义日期类
class Date
{
public:
	Date(int year = 2017, int month = 9, int day = 10)
		: _year(year)
		, _month(month)
		, _day(day)
	{}
	void Display();    //显示函数
	void SetDate();    //获取日期函数
	void AddDate();    //日期加一函数
	void SubDate();    //日期减一函数
	

	Date& operator=(const Date& d);    //赋值
	Date& operator++();      // 前置++ 
	Date operator++(int);    // 后置++ 
	Date& operator--();      //前置--
	Date operator--(int);    //后置-- 
	Date operator+(int days);  //days天之后的日期  
	Date operator-(int days);  // days天之前的日期

	int operator-( Date& d);                    // 两个日期之间的距离 (方式一)
	friend int SubDateDays(Date &x,Date &y);    // (方式二)两个日期之间的距离 

	bool operator==(const Date& d);
	bool operator!=(const Date& d);
	bool operator>(const Date& d);
	bool operator<(const Date& d);

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

void Date::Display()      //显示函数
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
void Date::SetDate()           //获取日期函数
{
	int year, month, day;
	cout << "请输入年月日:";
	cin >> year >> month >> day;
	start:     //goto语句的标记点
	if (month<1 || month>12)
	{
		MessageBeep(0);    //该函数是在输入错误时发出声音提示
		cout << "输入错误!请按年月日的格式重新输入:";
		cin >> year >> month >> day;
		goto start;
	}
	if (day<1 || day>31)
	{
		MessageBeep(0);
		cout << "输入错误!请按年月日的格式重新输入:";
		cin >> year >> month >> day;
		goto start;
	}
	if (month == 2)
	{
		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
		{
			if (day > 29)
			{
				MessageBeep(0);
				cout << "输入错误!请按年月日的格式重新输入:";
				cin >> year >> month >> day;
				goto start;
			}
		}
		else
		{
			if (day > 28)
			{
				MessageBeep(0);
				cout << "输入错误!请按年月日的格式重新输入:";
				cin >> year >> month >> day;
				goto start;
			}
		}
	}
	_year = year;
	_month = month;
	_day = day;
}

void Date::AddDate()      //日期加一函数
{
	switch (_month)
	{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
		if (_day < 31)
			_day += 1;
		else
		{
			_month += 1;
			_day = 1;     //.........
		}
		break;
	case 12:
		if (_day < 31)
			_day += 1;
		else
		{
			_year += 1;
			_month = 1;
			_day = 1;
		}
		break;
	case 2:
		if ((_year % 4 == 0 && _year % 100 != 0) || _year % 400 == 0)
		{
			if (_day < 29)
				_day += 1;
			else
			{
				_month += 1;
				_day = 1;
			}
		}
		else
		{
			if (_day < 28)
				_day += 1;
			else
			{
				_month += 1;
				_day = 1;
			}
		}
		break;

	case 4:
	case 6:
	case 9:
	case 11:
		if (_day < 30)
			_day += 1;
		else
		{
			_month += 1;
			_day = 1;
		}
		break;
	}
}

void Date::SubDate()      //日期减一函数
{
	switch (_month)
	{
	case 1:
		if (_day == 1)
		{
			_year -= 1;
			_month = 12;
			_day = 31;
		}
		else
			_day -= 1;
		break;
		
	case 3:
		if ((_year % 4 == 0 && _year % 100 != 0) || _year % 400 == 0)
		{
			if (_day == 1)
			{
				_month -= 1;
				_day = 29;
			}
			else
			{
				_day -= 1;
			}
		}
		else
		{
			if (_day == 1)
			{
				_month -= 1;
				_day = 28;
			}
			else
			{
				_day -= 1;
			}
		}
		break;

	case 2:
	case 4:
	case 6:
	case 9:
	case 11:
		if (_day == 1)
		{
			_month -= 1;
			_day = 31;
		}
		else
			_day -= 1;
		break;
	case 8:
		if (_day == 1)
		{
			_month -= 1;
			_day = 31;
		}
		else
			_day -= 1;
		break;
	case 5:
	case 7:
	case 10:
	case 12:
		if (_day == 1)
		{
			_month -= 1;
			_day = 30;
		}
		else
			_day -= 1;
		break;
	}
}


//

Date& Date::operator=(const Date& d)
{
	this->_year = d._year;
	this->_month = d._month;
	this->_day = d._day;
	return Date(_year, _month, _day);
}
Date& Date::operator++()      //前置++
{
	this->AddDate();
	return *this;
}
Date Date::operator++(int)    //后置++
{
	Date temp(*this);
	this->AddDate();
	return temp;
}
Date& Date::operator--()      //前置--
{
	this->SubDate();
	return *this;
}
Date Date::operator--(int)    //后置--
{
	Date temp(*this);
	this->SubDate();
	return temp;
}
Date Date::operator+(int days)    //days天之后的日期
{
	Date temp(*this);
	for (int i = 0; i < days; i++)
	{
		temp.AddDate();
	}
	return temp;
}
Date Date::operator-(int days)    //days天之前的日期
{
	Date temp(*this);
	for (int i = 0; i < days; i++)
	{
		temp.SubDate();
	}
	return temp;
}

//两个日期相隔的天数

int Date::operator-(Date & y)        //两个日期相隔的天数(方式一)
{
	Date x = *this;
	Date d;
	//把较大的日期给x较小的给y
	if (y._year>x._year)
	{
		d = x;
		x = y;
		y = d;
	}
	if (y._year==x._year)
	if (y._month > x._month)
	{
		d = x;
		x = y;
		y = d;
	}
	if (y._year==x._year)
	if (y._month==x._month)
	if (y._day > x._day)
	{
		d = x;
		x = y;
		y = d;
	}
	int days = 0;    //天数计数器
	//把从y那一年到x的前一年所有年的天数累加
	for (int i = y._year; i < x._year; i++)
	{
		if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
			days += 366;
		else
			days += 365;
	}
	//去掉y那年已过的天数,即从一月到y._month前一个月的天数和y._day的和
	for (int j = 1; j < y._month; j++)
	{
		if (j == 1 || j == 3 || j == 5 || j == 7 || j == 8 || j == 10 || j == 12)
			days -= 31;
		else if (j == 2)
		{
			if ((y._year % 4 == 0 && y._year % 100 != 0) || (y._year % 400 == 0))
				days -= 29;
			else
				days -= 28;
		}
		else
			days -= 30;	
	}
	days -= y._day;

	//加上x那年已过的天数
	for (int k = 1; k < x._month; k++)
	{
		if (k == 1 || k == 3 || k == 5 || k == 7 || k == 8 || k == 10 || k == 12)
			days += 31;
		else if (k == 2)
		{
			if ((x._year % 4 == 0 && x._year % 100 != 0) || (x._year % 400 == 0))
				days += 29;
			else
				days += 28;
		}
		else
			days += 30;
	}
	days += x._day;
	return days;
}

int SubDateDays(Date &x, Date &y)  //日期相减函数(方式二)
{
	Date d;
	//把较大的日期给x较小的给y
	if (y._year>x._year)
	{
		d = x;
		x = y;
		y = d;
	}
	if (y._year==x._year)
	if (y._month > x._month)
	{
		d = x;
		x = y;
		y = d;
	}
	if (y._year==x._year)
	if (y._month==x._month)
	if (y._day > x._day)
	{
		d = x;
		x = y;
		y = d;
	}
	int days = 0;    //天数计数器
	//把从y那一年到x的前一年所有年的天数累加
	for (int i = y._year; i < x._year; i++)
	{
		if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
			days += 366;
		else
			days += 365;
	}
	//去掉y那年已过的天数,即从一月到y._month前一个月的天数和y._day的和
	for (int j = 1; j < y._month; j++)
	{
		if (j == 1 || j == 3 || j == 5 || j == 7 || j == 8 || j == 10 || j == 12)
			days -= 31;
		else if (j == 2)
		{
			if ((y._year % 4 == 0 && y._year % 100 != 0) || (y._year % 400 == 0))
				days -= 29;
			else
				days -= 28;
		}
		else
			days -= 30;	
	}
	days -= y._day;

	//加上x那年已过的天数
	for (int k = 1; k < x._month; k++)
	{
		if (k == 1 || k == 3 || k == 5 || k == 7 || k == 8 || k == 10 || k == 12)
			days += 31;
		else if (k == 2)
		{
			if ((y._year % 4 == 0 && y._year % 100 != 0) || (y._year % 400 == 0))
				days += 29;
			else
				days += 28;
		}
		else
			days += 30;
	}
	days += x._day;
	return days;
}

bool型的重载

bool Date::operator==(const Date&d)
{
	Date x(*this);
	if (x._year == d._year&&x._month == d._month&&x._day == d._day)
		return 1;
	else
		return 0;
}
bool Date::operator!=(const Date& d)
{
	Date x(*this);
	if (x._year != d._year)
		return 1;
	else
	{
		if (x._month != d._month)
			return 1;
		else
		{
			if (x._day != d._day)
				return 1;
			else
				return 0;
		}
	}
}
bool Date::operator>(const Date& d)
{
	Date x(*this);
	if (d._year > x._year)
		return 1;
	else if (x._year == d._year)
	{
		if (d._month > x._month)
			return 1;
		else if (x._month == d._month)
		{
			if (d._day > x._day)
				return 1;
			else
				return 0;
		}
		else
			return 0;
	}
	else
		return 0;
}
bool Date::operator<(const Date& d)
{
	Date x(*this);
	if (d._year < x._year)
		return 1;
	else if (x._year == d._year)
	{
		if (d._month < x._month)
			return 1;
		else if (x._month == d._month)
		{
			if (d._day < x._day)
				return 1;
			else
				return 0;
		}
		else
			return 0;
	}
	else
		return 0;
}

主函数部分

void test1()
{
	Date d1,d2,d3;
	cout << "d1 : ";
	d1.Display();

	d2 = d1 + 45;
	cout << "d2 : ";
	d2.Display();

	d3 = d1 - 45;
	cout << "d3 : ";
	d3.Display();
}
void test2()
{
	Date d1,d2,d3,d4;
	
	d2.SetDate();
	d3 = d2;

	cout << "d1 : ";
	d1.Display();
	cout << "d2 : ";
	d2.Display();
	cout << "d3 : ";
	d3.Display();

	int count = d3 - d1;    //日期d3与日期d1相隔的天数(方式一)
	//int count = SubDateDays(d3, d1);       //日期d3与日期d1相隔的天数(方式二)  
	cout <<"days="<< count << endl;
	cout << "d2 : ";
	d2.AddDate();   //日期d4后一天的日期
	d2.Display();
}
void test3()
{
	Date d1, d2, d3;
	d2.SetDate();

	cout << "d1 : ";
	d1.Display();
	cout << "d2 : ";
	d2.Display();
	cout << endl;

	d3 = d1++;
	cout << "d3=d1++ : ";
	d3.Display();
	cout << endl;
	d3 = ++d2;
	cout << "d3=++d2 : ";
	d3.Display();
}
void test4()
{
	Date d1, d2, d3;
	d2.SetDate();

	cout << "d1 : ";
	d1.Display();
	cout << "d2 : ";
	d2.Display();
	cout << endl;

	d3 = d1--;
	cout << "d3=d1-- : ";
	d3.Display();
	cout << endl;
	d3 = --d2;
	cout << "d3=--d2 : ";
	d3.Display();
}
void test5()
{
	Date d1, d2;
	d2.SetDate();

	cout << "d1 : ";
	d1.Display();
	cout << "d2 : ";
	d2.Display();
	bool i = (d2 == d1);
	cout << "(==)i = " << i << endl;
	bool a = (d2 != d1);
	cout << "(!=)a = " << a << endl;
	bool b = (d2 > d1);
	cout << "(>)b = " << b << endl;
	bool c = (d2 < d1);
	cout << "(<)c = " << c << endl;
}
int main()
{
	//test1();       //加days天之后的日期,减days天之后的日期
	//test2();       //计算相隔天数,AddDate函数,
	//test3();       //前置++,后置++
	//test4();       //前置--,后置--
	test5();       //判断两个日期大小

	system("pause");
	return 0;
}


  • 15
    点赞
  • 80
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值