<span style="font-size:18px;">/****************************************************************************************** Purpose: 声明并实现一个万年历类【腾讯面试题】 Author: 周蒙蒙 Created Time: 2015-8-19 ******************************************************************************************/ #include<iostream> using namespace std; class Date { public: // 初始化列表进行初始化。 //带缺省值的构造函数 Date(int year = 1900, int month = 1, int day = 1) :_year(year), _month(month), _day(day) { if (CheakDate()==0) { _year = 1900; _month = 1; _day = 1; } } //检查日期是否合法 bool CheakDate() { if ((_year < 1) || (_month<0) || (_month)>12 || (_day)<0 || (_day)>GetMonthDay(_year, _month)) { cout << "invaled date" << endl; return 0; } return 1; } //拷贝构造函数 Date(const Date& d) : _year(d._year), _month(d._month), _day(d._day) {} //赋值操作符的重载 Date& operator= (const Date& d) { if (*this != 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) { return 1; } if (_year == d._year) { if (_month > d._month) { return 1; } if (_month == d._month) { if (_day > d._day) { return 1; } if (_day == d._day) { return 0; } } } return 0; } 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); } //判断是否是闰年 static int Leap_Year(int year) { if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) return 1; else return 0; } //用数组比switch case 语句更简洁 static int GetMonthDay(int year, int month) { static int Months[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (Leap_Year(year) == 1) { Months[2] = 29; } else { Months[2] = 28; } return Months[month]; } // 计算一个日期加减一个天数以后的日期。注意:加的天数有可能是负数 Date operator+(int day) { Date d(*this); d._day += day; while (d._day<0) { if (d._month == 1) { d._year--; d._month = 12; } else { d._month--; } d._day += GetMonthDay(d._year, d._month); } while (d._day > GetMonthDay(d._year, d._month)) { d._day -= GetMonthDay(d._year, d._month); if (d._month == 12) { d._year++; d._month = 1; } else { d._month++; } } return d; } Date operator-(int day) { Date d(*this); if (day < 0) { day = 0 - day; return *this + day; } d._day -= day; if (d._day < 0) { while (d._day<0) { if (d._month == 1) { d._year--; d._month = 12; } else { d._month--; } d._day += GetMonthDay(d._year, d._month); } } return d; } Date& operator-=(int day) { (*this) = (*this - day); return *this; } Date& operator+=(int day) { (*this) = (*this + day); return *this; } const Date& operator++()// 前置++ { (*this) = (*this + 1); return *this; } Date operator++(int) // 后置++ { Date tmp(*this); (*this) = (*this + 1); return tmp; } const Date& operator--() // 前置-- { (*this) = (*this - 1); return *this; } Date operator--(int) // 后置-- { Date tmp(*this); (*this) = (*this - 1); return tmp; } // 两个日期相加没有意义 // 计算两个日期相减以后的差的天数 int operator-(const Date& d) { Date d1(*this); Date d2(d); if (d1 > d2) { d1 = d; d2 = *this; } int count = 0; while (d1 != d2) { d1++; ++count; } return count; } friend istream& operator>>(istream& is, Date& d); friend ostream& operator<<(ostream& os, Date& d); private: int _year; int _month; int _day; }; //输入运算符的重载 istream& operator>>(istream& is, Date& d) { cout << "请依次输入年-月-日" << endl; is >> d._year; is >> d._month; is >> d._day; return is; } //输出运算符的重载 ostream& operator<<(ostream& os, Date& d) { os << d._year << "-"; os << d._month << "-"; os << d._day; return os; } void PromptInfo() { cout << "|**********日期计算器************|"<<endl; cout << "|********1.退后几天的日期 *******|"<< endl; cout << "|********2.计算日期差************|" << endl; cout << "|********0.退出******************|" << endl; cout << "|********************************|" << endl; } int main() { Date d1, d2; while (1) { PromptInfo(); int in = 0, day = 0; cout << "请选择:" << endl; cin >> in; switch (in) { case 0: cout << "Exit!" << endl; return 0; case 1: cin >> d1; if (d1.CheakDate()==0) { cout << "非法日期,请重新输入!" << endl; break; } cout << "请输入退后的天数" << endl; cin >> day; cout << d1 + day << endl; break; case 2: cin >> d1; if (d1.CheakDate()==0) { cout << "非法日期,请重新输入!" << endl; break; } cin >> d2; if (d2.CheakDate()==0) { cout << "非法日期,请重新输入!" << endl; break; } cout << d1 - d2 << endl; break; default: cout << "输入选项错误,请重新输入!" << endl; } } return 0; }</span>
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/sunshine552/article/details/48133233