1. 程式人生 > >C++Date類(日期類)

C++Date類(日期類)

Date.h

#pragma once
#include <iostream>
using namespace std;



class Date
{
public:
	Date(int year, int month, int day);
	/*Date(const Date& d);*/
	Date& operator+=(const Date& d);
	Date operator+(int day);//加上多少天,不改變初始日期的值
	Date operator+=(int day);//加上多少天,改變初始日期的值
	Date operator-(int day);//減去多少天,不改變初始日期的值
	Date& operator-=(int day);//減去多少天,改變初始日期的值

	int operator-(const Date& d);//兩個日期相減相差多少天
	Date operator++(); //data++
	Date operator++(int);//++date
	Date operator--();//date--
	Date operator--(int);//--date
	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);
	int GetMonthDay(int year, int month);
	void Print();
private:
	int _year;
	int _month;
	int _day;
};

Date.cpp

#include "Date.h"


int Date::GetMonthDay(int year, int month)
{
	static const int monthDay[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;
	}
	return monthDay[month];
}
Date Date :: operator+(int day)
{
	Date ret(*this);
	ret += day;
	return ret;
}
Date Date::operator+=(int day)

{
	if (day < 0)
	{
		return *this -= (-day);
	}
	_day += day;
	while (_day>GetMonthDay(_year,_month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month > 12)
		{
			_month = 1;
			_year++;
		}
	}
}


Date Date :: operator-(int day)
{
	Date ret(*this);
	ret -= day;
	return ret;
}
Date& Date :: operator-=(int day)
{
	if (day < 0)
	{
		return *this += (-day);
	}
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
		_month--;
	}
	return *this;

}
//++d
Date Date::operator++()
{
	*this += 1;
	return *this;
}
//d++
Date Date::operator++(int)
{
	Date ret(*this);
	*this += 1;
	return ret;
}
//--d
Date Date::operator--()
{
	*this -= 1;
	return *this;

}
//d--
Date Date::operator--(int)
{
	Date ret(*this);
	*this -= 1;
	return ret;
}

int Date::operator-(const Date& d)//兩個日期相減相差多少天
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (min < max)
	{
		++min;
		++n;
	}
	return n*flag;
}




Date::Date(int year=2018, int month=10, int day=27)
{
	if (year >= 0
		&& month > 0 && month<13
		&& day>0 && day <= GetMonthDay(year, month))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		cout << "Date invalid" << endl;
		//assert(false);//終止程式
	}
}

bool Date::operator>(const Date& d)
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year)
	{
		if (_month > d._month)
		{
			return true;
		}
		else if (_month == d._month)
		{
			if (_day > d._day)
			{
				
				return true;
			}
		}
	}
	return false;
}
bool Date::operator>=(const Date& d)
{
	return *this > d || *this == d;
}
bool Date::operator<(const Date& d)
{
	return !(*this >= d);
}
bool Date::operator<=(const Date& d)
{
	return !(*this > d);
}
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);
}

void Date::Print()
{
	cout << _year << "-" << _month << "-" << _day << endl;
}

Main.cpp

#include"Date.h"

int main()
{
	Date d(2018,10,28);
	Date d1 = d + 100;
	d.Print();
	d1.Print();
	Date d2(2018, 5, 8);
	d2 += 100;
	d2.Print();
	Date d3 = d2 - (-100);
	d3.Print();
	Date d4 = d3 + (-100);
	d4.Print();
	int k = d3 > d4;
	cout << k << endl;
	Date d5 = d4;
	(d5++).Print();
	(++d5).Print();
	int a = d3 - d4;
	cout << a << endl;

	return 0;


}