c++日期类的实现(基础篇)

本文介绍了一个日期类的设计及其运算符重载的方法,包括比较运算符和算术运算符的实现。通过具体的代码示例展示了如何进行日期之间的比较和加减操作,并提供了完整的类定义和主函数测试样例。

基本功能实现:

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

Date operator+(int day);
Date operator+=(int day);
Date operator-(int day);
Date operator-=(int day);
Date operator++();
Date operator++(int);
Date operator--();
Date operator--(int); 

#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;
	}
	void Display()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	bool Isvalue()
	{
		if (_month > 0 && _month<13 &&
			_day<=GetMonthday() && _day>0
			&& _year>0)
			return true;
		else
			return false;
	}
	
	bool operator<(const Date &d2)
	{
		assert(Isvalue());
		if (_year < d2._year)
		{
			return true;
		}
		else if (_month < d2._month)
		{
			return true;
		}
		else if (_day < d2._day)
		{
			return true;
		}
		else
			return false;
	}
	bool operator>(const Date &d2)
	{
		assert(Isvalue());
		if (_year > d2._year)
		{
			return true;
		}
		else if (_month > d2._month)
		{
			return true;
		}
		else if (_day > d2._day)
		{
			return true;
		}
		else
			return false;
	}
	bool operator<=(const Date &d2)
	{
		return !(*this > d2);
	}
	bool operator>=(const Date &d2)
	{
		return !(*this<d2);
	}
	bool operator!=(const Date &d2)
	{
		return ((*this<d2)||(*this>d2));
	}
	bool operator==(const Date &d2)
	{
		return !((*this<d2)||(*this>d2));
	}
	int GetMonthday()
	{
		int monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if ((_month == 2)&&((_year % 4 == 0 && _year % 100 != 0) || (_year % 400 == 0)))
			return 29;
		else
			return monthDays[_month];
	}
	Date operator+(int day)
	{
		if (day<0)
		{
			return *this - (-day);
		}
		Date tmp(*this);
		tmp._day += day;
		while (tmp._day >tmp.GetMonthday())
		{
			tmp._day -= tmp.GetMonthday();
			tmp._month++;
			if (tmp._month > 12)
			{
				tmp._year++;
				tmp._month = 1;
			}
		}
		return tmp;

	}
	Date operator-(int day)
	{
		if (day<0)
		{
			return *this + (-day);
		}
		Date tmp(*this);
		tmp._day -= day;
		while (tmp._day <= 0)
		{
			tmp._month--;
			if (tmp._month == 0)
			{
				tmp._year--;
				tmp._month = 12;
			}

			tmp._day += tmp.GetMonthday();

		}
		return tmp;

	}
	Date &operator=(const Date &d)
	{
		if ((*this != d))
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}
	Date& operator++()
	{
		*this += 1;
		return *this;
	}
	Date operator++(int)
	{
		Date tmp(*this);
		*this += 1;
		return tmp;
	}
	Date& operator--()
	{
		*this -= 1;
		return *this;
	}
	Date operator--(int)
	{
		Date tmp(*this);
		*this -= 1;
		return tmp;
	}
	Date &operator+=(int day)
	{
		if (day < 0)
		{
			return (*this = *this - day);
		}
		*this = *this + day;
		return *this;
	}
	Date &operator-=(int day)
	{
		if (day < 0)
		{
			return (*this = *this +(-day));
		}
		*this = *this - day;
		return *this;
	}
	int operator-(const Date &d)
	{
		int tag = 1;
		Date big(*this);
		Date less(d);
		if (*this < d)
		{
			big = d;
			less = *this;
			tag = -1;
		}
		int count = 0;
		while (big>less)
		{
			++count;
			less++;
		}
		return count*tag;
	}
		
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1(2017, 10, 26);
	Date d2(2017, 2, 14);
	d1.Display();
	d2.Display();
	cout << (d1.operator<(d2)) << endl;
	cout << (d1.operator>(d2)) << endl;
	cout << (d1.operator<=(d2)) << endl;
	cout << (d1.operator>=(d2)) << endl;
	cout << (d1.operator!=(d2)) << endl;
	cout << (d1.operator==(d2)) << endl;
	Date d3 = d1 + 2000;
	d3.Display();
	Date d4 = d1 - 52000;
	d4.Display();
	d1 += 6;
	d1.Display();
	d1 -= 6;
	d1.Display();
	d1++;
	d1.Display();
	++d1;
	d1.Display();
	d1--;
	d1.Display();
	--d1;
	d1.Display();

	d2 = d1 - 100;

	cout<<(d1-d2)<<endl;

}
运行结果:






#include <iostream> using namespace std; #include "MyDate.h" #include <iomanip> bool MYDATE::dowFlag = false; void MYDATE::Input() { int year, month, day; char c1, c2; while(!(cin >> year >> c1 >> month >> c2 >> day) || ! IsValid(year, month, day) || c1 != '-' || c2 != '-') { cout << "不正确的日期! 请重新输入: "; if(! cin.good()) cin.clear(); while(cin.get() != '\n') ; } this->year = year; this->month = month; this->day = day; } void MYDATE::Output() { char *adow[] = {"日", "一", "二", "三", "四", "五", "六"}; int w = Dow(); cout << setfill('0') << setw(2) << year << '-' << setw(2) << month << '-' << setw(2) << day << setfill(' '); if(dowFlag) { cout << "(星期" << adow[w] << ")"; } } void MYDATE::Set(int year, int month, int day) { if(IsValid(year, month, day)) { this->year = year; this->month = month; this->day = day; } else { cout << "不正确的时间,设置失败!\n"; } } void MYDATE::Get(int &year, int &month, int &day) { year = this->year; month = this->month; day = this->day; } int MYDATE::Year() { return year; } void MYDATE::Year(int year) { if(year > 0) { this->year = year; int n = Dom(year, month); if(day > n) day = n; } } int MYDATE::Month() { return month; } void MYDATE::Month(int month) { if(month > 0 && month <= 12) { this->month = month; int n = Dom(year, month); if(day > n) day = n; } } int MYDATE::Day() { return day; } void MYDATE::Day(int day) { int n = Dom(year, month); if(day <= 0) day = 1; else if(day > n) day = n; this->day = day; } MYDATE MYDATE::Add(int x) { int y = 1,i = 1,m = 1; MYDATE c; double s = this->Tod() + x; if(s >= 0) { y = 1; while(s >= 365 + IsLeap(y)) { s = s - (365 + IsLeap(y++)); i ++; } c.year = i; while(s >= Dom(c.year,m)) { s = s - Dom(c.year,m); m ++; } c.month = m; if(s == 0) c.day = 1; else c.day = s; } else cout << "Error!\n"; return c; } MYDATE MYDATE::Sub(int x) { MYDATE c; c.Set(year, month, day); c = c.Add(-x); return c; } bool MYDATE::IsValid(int year, int month, int day) { return year > 0 && month > 0 && month <= 12 && day > 0 && day <= Dom(year, month); } int MYDATE::Dom(int year, int month) { int n = 0; const static char adom[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if(month > 0 && month <= 12) { n = adom[month - 1]; if(month == 2 && IsLeap(year)) n ++; } return n; } int MYDATE::Doy() { const static short adoy[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; int n = adoy[month - 1] + day; if(month > 2 && IsLeap(year)) n ++; return n; } bool MYDATE::IsLeap(int year) { return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; } int MYDATE::Nol(int year) { return year / 4 - year / 100 + year / 400; } int MYDATE::Tod() { int t = year - 1; return t * 365 + Nol(t) + Doy(); } int MYDATE::Dow() { return Tod() % 7; } int MYDATE::Sub(MYDATE &x) { return this->Tod() - x.Tod(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值