一、萬年曆類中所包含函數,以及功能
/******************************************************************************************
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);
//
// 計算一個日期加減一個天數以後的日期。
//
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);// 後置--
//
// 兩個日期相加沒有意義
// 計算兩個日期相減以後的差的天數
//
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();
}