1. 程式人生 > >【C++】日期類Date的定義

【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;
}