/******************************************************************************************
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;
//};
//
//void TestDate()
//{}
#include<iostream>
using namespace std;
class Time
{
public:
Time(int hour)
{
_hour = hour;
}
Time(const Time& t)
{
_hour = t._hour;
}
Time& operator=(const Time& t)
{
_hour = t._hour;
return *this;
}
void Display()
{
cout << _hour << endl;
}
private:
int _hour;
};
int Tab[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
class Date
{
public:
// 初始化列表进行初始化。
Date(int year = 1900, int month = 1, int day = 1)
:_year(year),
_month(month),
_day(day),
_testConst(2222),
_testRef(_day),
_t(12)
{
Check(year,month,day);
}
void Check(int year,int month ,int day )
{
if (year < 1900 || month<1 || month>12 ||
day<1 || day>Tab[Getmonth(year, month)])
{
cout << "Invalid Date" << endl;
_year = 1900;
_month = 1;
_day = 1;
}
}
Date(const Date& d)
:_year ( d._year),
_month (d._month),
_day (d._day),
_testConst(2222),
_testRef(_day),
_t(12)
{}
Date& operator= (const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
void Display()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
public:
//
// 比较日期的运算符重载
//
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)
{
if (_month == d._month)
{
return (_day > d._day);
}
return (_month > d._month);
}
return (_year > d._year);
}
bool operator >= (const Date& d)
{
return (*this > d||*this == d);
}
bool operator < (const Date& d)
{
return !(*this > d || *this == d);
}
bool operator <= (const Date& d)
{
return !(*this > d);
}
//
// //
// 计算一个日期加减一个天数以后的日期。
//
int leap(int year)
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
return 1;
}
return 0;
}
int Getmonth(int year, int month)
{
if (leap(year))
{
Tab[2] = 29;
}
else
{
Tab[2] = 28;
}
return month;
}
Date operator+(int day)
{
Date tmp(*this);
if (day<0)
{
day = 0 - day;
return *this - day;
}
tmp._day += day;
while (tmp._day >Tab[Getmonth(tmp._year,tmp._month)])
{
tmp._day -= Tab[tmp._month];
tmp._month = tmp._month + 1;
if (tmp._month > 12)
{
tmp._year = tmp._year + 1;
tmp._month = 1;
}
}
return tmp;
}
Date operator-(int day)
{
Date tmp(*this);
if (day < 0)
{
day = 0 - day;
return *this + day;
}
tmp._day -= day;
while (tmp._day <=0)
{
if (tmp._month == 1)
{
tmp._year--;
tmp._month = 12;
}
else
{
tmp._month--;
}
tmp._day += Tab[Getmonth(tmp._year, tmp._month)];
}
return tmp;
}
Date& operator+=(int day)
{
*this=*this +day;
return *this;
}
Date& operator-=(int day)
{
*this = *this - day;
return *this;
}
const Date& operator++()// 前置++
{
return *this += 1;
}
Date operator++(int)// 后置++
{
Date tmp = *this;
*this += 1;
return tmp;
}
const Date& operator--()// 前置--
{
return *this -= 1;
}
Date operator--(int)// 后置--
{
Date tmp = *this;
*this -= 1;
return tmp;
}
// 两个日期相加没有意义
// 计算两个日期相减以后的差的天数
int operator-(const Date& d)
{
int count = 0;
Date x1 = *this;
Date x2 = d;
if (*this>d)
{
x1 = d;
x2 = *this;
}
while (x1 != x2)
{
x1 += 1;
count++;
}
return count;
}
friend istream &operator>>(istream &is, Date &d);
friend ostream &operator<<(ostream &is, const Date &d);
private:
int _year;
int _month;
int _day;
const int _testConst;
int& _testRef;
Time _t;
};
istream &operator>>(istream &is, Date &d)
{
is >> d._year;
is >> d._month;
is >> d._day;
d.Check(d._year, d._month, d._day);
return is;
}
ostream &operator<<(ostream &os, const Date &d)
{
os << d._year<<"-";
os << d._month<<"-";
os << d._day;
return os;
}
int main()
{
Date op1(2007, 2, 28);
Date op2(2007, 12, 9);
/*cout << (op1 == op2) << endl;
cout << (op1 != op2) << endl;
cout << (op1 <=op2) << endl;
cout << (op2 - op1) << endl;
op2.Display();*/
/*op1 = op1.operator-(650);
op1.Display();*/
op2 = op1.operator+(300);
op2.Display();
op2 = op2.operator-(300);
op2.Display();
cin >> op1;
cout << op1;
system("pause");
return 0;
}
【C++】万年历(时间计数器)
最新推荐文章于 2024-01-28 20:12:56 发布