【C++】万年暦類【テンセント面接問題】を声明し実現


一、カレンダー類に含まれる関数、および機能
/******************************************************************************************
Date.hpp: Copyright (c) Bit Software, Inc.(2013), All rights reserved. Purpose:万年暦類【テンセント面接問題】Author:xxx Created Time:2015-4-26*************************************************************************************************************/
class Time
{ public: Time(int hour); Time(const Time& t); Time& operator=(const Time& t); private: int _hour; } class Date{public://初期化リストを初期化します.Date(int year=1900、int month=1、int day=1);Date(const Date&d);Date&operator=(const Date&d);void Display();public:////日付を比較する演算子リロード//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); bool operator <= (const Date& d); ////1日のプラスマイナス日数以降の日付を計算します.//Date operator+(int day); Date operator-(int day); Date& operator-=(int day); Date& operator+=(int day); const Date& operator++();//前++Date operator++(int);//後置++const Date&operator--();//前--Date operator--(int);//後置--////////////2つの日付を加算しても意味がありません//2つの日付の減算後の差を計算する日数//int operator-(const Date&d);private: int _year; int _month; int _day; const int _testConst; int& _testRef; Time _t; };
二、コード
#include <iostream>
using namespace std;

class Time
{
public:
	Time(int hour=0,int minute=0,int second=0)
	{
		//cout<<"   "<<endl;
		_hour=hour;
		_minute=minute;
		_second=second;
	}

	friend class Date;

	Time(const Time& t)
	{
		//cout<<"copy    "<<endl;
	}

	Time& operator=(const Time& t)
	{
		_hour = t._hour;
		_minute=t._minute;
		_second=t._second;

		return *this;
	}


private:
	int _hour;
	int _minute;
	int _second;
};

class Date
{
public:
	//           
	//Date (int year = 1900, int month = 1, int day = 1,int hour=0):_t(hour)
	//{
	////	cout<<"    "<<endl;
	//	_year = year;
	//	_month = month;
	//	_day = day;

	//	if (CheckDate() == false)
 //        {
 //           Date();
 //        }

	//}
	Date (int year = 1900, int month = 1, int day = 1)
		:_year(year)
		,_month(month)
		,_day(day)
	{
		//              ,     1900-1-1
		if (CheckDate())
		{
			year = 1900;
			month = 1;
			day = 1;
		}
	}

	Date (const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
		//cout<<"copy    "<<endl;
	}

	Date& operator= (const Date& d)

	{
		_year=d._year;
		_month=d._month;
		_day=d._day;
		return *this;
	}

	void Display (Time& t)
	{
		cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
		cout<<t._hour<<":"<<t._minute<<":"<<t._second<<endl;
	}

	
public:
	
	//    
	bool CheckDate ()
    {
          if (_year < 1
             || ( _month < 1 || _month > 12)
             || ( _day < 1   || _day > MonthDay(_year, _month )))
         { 
              return true ;
         }

          return false ;
    }

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

	bool operator != (const Date& d)
	{
		return !(*this == d);
	}

	bool operator > (const Date& d)
	{
		if(_year > d._year)
		{
			return true;
		}
		else if((_year == d._year)&&(_month > d._month))
		{
			return true;
		}
		else if((_year == d._year)&&(_month == d._month)&&(_day >d._day))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

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

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

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

	//            
	//           ,        ,        !!!
	void _ToCrrectDate()
	{
		while(_day < 0)
		{
			_day += MonthDay (_year, _month);

			if (_month == 1)
			{
				_year--;
				_month = 12;
			}
			else
			{
				_month--;
			}
		}

		while (_day > MonthDay(_year, _month))
		{
			_day -= MonthDay (_year, _month);

			if (_month == 12)
			{
				_year++;
				_month = 1;
			}
			else
			{
				_month++;
			}
		}
	}


	//
	//                  。
	//
	Date operator+(int day)
	{
		Date tmp(*this);
		tmp._day+=day;
		tmp._ToCrrectDate();
									
		
		return tmp;
	}
	


	Date operator-(int day)
	{
		
			Date tmp(*this);
			tmp._day=tmp._day-day;
			tmp._ToCrrectDate();
			return tmp;
	}

	Date& operator-=(int day)
	{
		this->_day -= day;
		this->_ToCrrectDate();
		return *this;
	}

	Date& operator+=(int day)
	{
		this->_day += day;
		this->_ToCrrectDate();
		return *this;
	}

	const Date& operator++()	//   ++
	{
		*this += 1;
		return *this;
		return *this;
	}

	Date operator++(int)		//   ++		     (int),         
	{
		Date tmp(*this);
		*this += 1;
		return tmp;
	}

	const Date& operator--()	//   --
	{
		*this -= 1;
		return *this;
	}

	Date operator--(int)		//   --
	{
		Date tmp(*this);
		*this -= 1;
		return tmp;
	}
	//
	//           
	//                
	//
	int operator-(const Date& d)
	{
		int flag = 1;			//   
		Date x1 = *this, x2 = d;
		if (x1 < x2)
		{
			flag = -1;
			x1 = d;
			x2 = *this;
		}
		
		int Days = 0;		//      
		while (x1 != x2)
		{
			++x2;
			Days++;
		}

		return Days*flag;
	}

	int MonthDay(int year,int month)
	{
		if (year < 1 || month < 1 || month > 12)
		{
              return 0;
        }
		static int array[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};	//  static   
		if(((year%4)&&(year%100)!=0)||(year%400==0))
		{
			array[2]=28;
		}
		return array[month];
	}
	
	friend ostream& operator<<( ostream& os , const Date& d );
	
    friend istream& operator>>( istream& is , Date& d);
 

private:
	int _year;
	int _month;
	int _day;

	/*const int _testConst;
	int& _testRef;*/
	Time _t;
};


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

     return os ;
}


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

     return is ;
}

void PromptInfo()
{
	cout<<"========     ======="<<endl;
	cout<<"0.  "<<endl;
	cout<<"1.       "<<endl;
	cout<<"2.     "<<endl;
	cout<<"3.  "<<endl;
	cout<<"========================="<<endl;
}


void main()
{
	Date d1, d2;
	int in, days;
	do {
		PromptInfo();
		cin>>in;

		switch(in)
		{
		case 0:
			break;
		case 1:
			cin>>d1;
			if (d1.CheckDate())
			{
				cout<<"      !"<<endl;
				break;
			}
			cout<<"          "<<endl;
			cin>>days;
			d1+=days;
			cout<<d1<<endl;
			break;
		case 2:
			cin>>d1;
			if (d1.CheckDate())
			{
				cout<<"      !"<<endl;
				break;
			}
			cin>>d2;
			if (d2.CheckDate())
			{
				cout<<"      !"<<endl;
				break;
			}
			days = d1 - d2;
			cout<<"     :"<<days<<endl;
			break;
		case 3:
			 system("CLS");
			 break ;
		default:
			cout<<"    ,     "<<endl;
			break;
		}
	}while(in != 0);
	getchar();
}